I have two two-dimensional unit vectors a
and b
. I'm trying to get their angle related to their order. arc cosine of the dot product returns the absolute value of the angle. if b
is before a
I want to get negative angle.
e.g. if a
is on 1 o'clock and b
is on 2 o'clock I want to get answer of 30 degrees but if a
is on 2 o'clock and b
is on 1 o'clock I want to get answer of -30 degrees. answers should be in radians, I used degrees for simpler explanation.
How can I do this? a method for finding who is before who will be good enough. this is for an application, so an answer of "just look at them" won't work, but a mathematical relation will be good (e.g. some equation that is negative only if b
is before a
).
-
2$\begingroup$ You might be interested in Determinants (en.wikipedia.org/wiki/Determinant): the determinant of two 2d vectors is the product of the norms and the sine between the two vectors; the sine can give you the sign. $\endgroup$– Najib IdrissiJun 13, 2011 at 12:23
-
1$\begingroup$ If this is for a computer program, then your programming language might have a function called atan2(x,y). Check for its documentation to make sure, but IIRC that returns the angle (with sign!) between the vectors $xi+yj$ and the positive $x$-axis. If $a=(x_1,y_1)$ and $b=(x_2,y_2)$ then you get your result by carefully comparing the outputs of atan2($x_1,y_1$) and atan2($x_2,y_2$). Emphasis on CAREFUL. The angles wrap around the circle, so you need to make up your mind, on how you treat the case, when one angle is -175 degrees and the other 175. Add multiples of $2\pi$ when appropriate! $\endgroup$– Jyrki LahtonenJun 13, 2011 at 12:26
1 Answer
Treat them as 3D vectors with $z=0$ and find the cross product.
-
-
$\begingroup$ @Dani, it boils down to two multiplications and a comparison. You're not going to get much easier than that. $\endgroup$ Jun 13, 2011 at 12:25
-
$\begingroup$ yes, I've been able it to that. didn't know cross product is such an easy operation. $\endgroup$– DanielJun 13, 2011 at 12:28