# 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)$$

• vectors in 2D or 3D? Mar 1, 2013 at 14:05
• Sorry, it's 2d vectors. Mar 1, 2013 at 14:14
• What you did used the dot product, which is usually the standard way to find angles. However, because of your requirement for angles to be oriented, the dot product will not work. I'll elaborate a bit more in my answer where I can more freely LaTeX. Mar 1, 2013 at 14:20
• For some basic information about writing math at this site see e.g. here, here, here and here. Mar 1, 2013 at 14:25
• This formula does not take orientation into account. Mar 1, 2013 at 14:35

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)).

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$.

• Yes, there it is! Calculating the determinant was the solution. Thanks! Mar 1, 2013 at 14:47

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$.

• The cross product is a vector. If you divide by magnitudes, it remains a vector. So it does not make sense to take the inverse sine. You simply forgot the word length, or magnitude. Mar 1, 2013 at 14:19
• The length of a vector is always nonnegative. So you will never find $-14$. Mar 1, 2013 at 14:21
• Thanks for the reply, but now θ is evoluating between [-π/2, π/2] instead of [-π, π]. Mar 1, 2013 at 14:33