Linear Algebra – Calculate the Angle Between Two Vectors

linear algebravector-spaces

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

Best Answer

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

Related Question