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


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