1
votes

I need to calculate the angle between two vectors. The vectors may be pointing in any direction, and have been normalized. I want the angle to be measured clockwise from vectorA to vectorB in some cases, and anticlockwise from vectorA to vectorB in other cases (in other words, I don't just want to know the smallest angle).

Here's what I have

if (clockwise) angle = Math.atan2(vectorA.y, vectorA.x) - Math.atan2(vectorB.y, vectorB.x);
else angle =  -1*(Math.atan2(vectorA.y, -vectorA.x) - Math.atan2(vectorB.y, -vectorB.x));

I guess this will never work for reflex angles? So how do I calculate an angle on the range 0->2pi?

1

1 Answers

0
votes

Calculate it consistently for the clockwise direction, and subtract from 360 (or 2*pi) when you need it counterclockwise.

If you need to normalize to a particular degree range, then you can do so by directly normalizing the output of your code. So calculate the clockwise angle, then add 2*pi until it is above zero, then take the result mod 2*pi, and you will get a result in the range [0, 2*pi).