Calculate the angle between two vectors I come from Stack Overflow and I thought my question was more related to this forum. The problem is I'm not a mathematician, so please excuse me if my question is dumb.
I'm trying to get the angle between two vectors. As numbers of posts says, here or here, I tried this solution.
But my angle must be "oriented": If th angle between u⃗ and v⃗ is θ, the angle between v⃗ and u⃗ must be -θ.
Is there a mathematical solution to this?
Edit :
Here's the formula I implemented for the points $a = (x_1, y_1)$ and $b = (x_2, y_2)$ representing the vectors:
$$
\mathrm{angle} = \arccos \left(\frac{x_1 \cdot x_2 + y_1 \cdot y_2}{\sqrt{x_1^2+y_1^2} \cdot \sqrt{x_2^2+y_2^2}} \right)
$$
 A: I assume $u$ and $v$ are both nonzero.
Let $\theta\in (-\pi,\pi]$ modulo $2\pi$ be the oriented angle between $u$ and $v$.
Using 
$$
\cos\theta=\frac{(u,v)}{\|u\|\|v\|}
$$
you can find the value of $\cos\theta$.
Taking $\arccos$ of the latter will, you get $\theta_0$ in  $[0,\pi]$ such that
$$
\theta=\theta_0 \quad\mbox{mod} \;2\pi\quad\mbox{or}\quad \theta=-\theta_0 \quad\mbox{mod} \;2\pi.
$$
Now to determine the orientation of $(u,v)$, you must compute the $2\times 2$ determinant of the matrix whose first column is $u$, and second column is $v$.
If this is $0$, this means $u$ and $v$ are parallel. Write $u=\lambda v$. If $\lambda >0$, then $\theta=0$ mod $2\pi$. If $\lambda<0$, then $\theta=\pi$ mod $2\pi$.
If the determinant is positive, this means $\theta=\theta_0$ modulo $2\pi$.
If the determinant is negative, you have $\theta=-\theta_0$ modulo $2\pi$.
A: If you come from Stack Overflow, using atan2 might be a simpler solution for you.
Let $a = (x_1,y_1)$, $b = (x_2,\;y_2)$. If $\theta$ is the "oriented" angle from $a$ to $b$ (that is, rotating $\hat{a}$ by $\theta$ gives $\hat{b}$), then:
$$
\theta = \mathrm{atan2}\left(x_1y_2-y_1x_2,\;x_1x_2+y_1y_2\right)
$$
In Matlab, this is equivalent to wrapToPi(angle(x2+i*y2) - angle(x1+i*y1)).
A: Find the cross product of the two vectors, then divide by the magnitudes of each vector, and take the inverse sine.
For example, say the vectors were $a = (4,3)$ and $b = (6,8)$. The cross product length is $a_1b_2-a_2b_1 = 4 \times 8 - 6 \times 3 = 14$. The length of the cross product is $|a||b|\sin(\theta) = 14$, and since $|a| = 5$ and $|b| = 10$, $\sin(\theta) = 14/50 = 0.28$. Taking the inverse sine, one obtains the angle from $a$ to $b$ as being $16.26 ^\circ$. Notice that if we had reversed $a$ and $b$, the cross product vector length would have been $-14$, leading to an angle of $-16.26 ^\circ$.
