Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Signed Angle Between Vectors

How would you find the signed angle theta from vector a to b?

And yes, I know that theta = arccos((a.b)/(|a||b|)).

However, this does not contain a sign (i.e. it doesn't distinguish between a clockwise or counterclockwise rotation).

I need something that can tell me the minimum angle to rotate from a to b. A positive sign indicates a rotation from +x-axis towards +y-axis. Conversely, a negative sign indicates a rotation from +x-axis towards -y-axis.

assert angle((1,0),(0,1)) == pi/2. assert angle((0,1),(1,0)) == -pi/2. 
like image 835
Cerin Avatar asked Jan 27 '10 20:01

Cerin


People also ask

What is signed angle?

Calculates the signed angle between vectors from and to in relation to axis . The angle returned is the angle of rotation from the first vector to the second, when treating these first two vector inputs as directions. These two vectors also define the plane of rotation, meaning they are parallel to the plane.


2 Answers

What you want to use is often called the “perp dot product”, that is, find the vector perpendicular to one of the vectors, and then find the dot product with the other vector.

if(a.x*b.y - a.y*b.x < 0)     angle = -angle; 

You can also do this:

angle = atan2( a.x*b.y - a.y*b.x, a.x*b.x + a.y*b.y ); 
like image 54
Derek Ledbetter Avatar answered Sep 20 '22 11:09

Derek Ledbetter


If you have an atan2() function in your math library of choice:

signed_angle = atan2(b.y,b.x) - atan2(a.y,a.x) 
like image 23
Sparr Avatar answered Sep 20 '22 11:09

Sparr