javascript - Algorithm for testing angle equality -


Many of the mandals in my application have been rotated at arbitrary degrees and I need to check whether their rotation is equal (some Regarding the error margin). A grand implementation goes like this:

  function angle (first, second, error margin) {return mathematics.base (first - second) & lt; = ErrorMargin; }   

But it fails because rotation can increase to infinity. Thus, we need to make both angles normal:

  function normal value angle (angle) {angle = angle% (2 * monastery; pi); // normalize the angle gap [-2 * pi, 2 * pi] if (angle and lieutenant; 0) 2 * monastery. PI + angle will return; // interval [0, 2 * pi] normalize the angle of return angle; }   

It always works about but fails in some corner cases, for example angleEquals (0, 2 * Math.PI - 1e-4, 1e-3) is false .

Any suggestions?

You need to normalize the gap between the angles, not the angle itself. Calculate diff = angle1-angle2 // difference diff = diff% 2 * PI // [0, 2 * PI] if the (diff> PI) diff- = 2 * PI // Find the full difference in the category (-PI, PI) diff = abs (diff) // boundary [0, PI] returns (difference and LT; error margin)

There are things that you can do

Edit: For future generation, here is a strict version.

  diff = eb ((abs (exit 1-angle 2) + pi)% (2 * pi) - pi) return (df & lt; error margin)   

(He does not rely on one's behavior, there are modulus on negative numbers, and fewer branches.)

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -