# How can one understand the ATAN2() function for calculating the angle between 2 vectors?

It took me long time to find a good formula to calculate the confined angle between two $2D$-vectors for example $(u,v)$. I have found the following formula:

$θ=2\operatorname{atan2}\left(\bigl\| \|v\|u−\|u\|v \bigr\|, \bigl\| \|v\|u+\|u\|v \bigr\|\right)$

where $\|u\|$ and $\|v\|$ are the length of the vector $u$ and $v$ respectively. AS we know:

$\cos(x)= \text{adjacent/hypotenuse}$

$\sin(x) = \text{opposite/hypotenuse}$

$\tan(x) = \text{adjacent/adjacent}$

So how can one interpret $\operatorname{atan2}\left(\bigl\| \|v\|u−\|u\|v \bigr\|, \bigl\| \|v\|u+\|u\|v \bigr\|\right)$ function in calculating the confined angle?

• What is ATAN2()? Apr 2, 2018 at 23:10
• ATAN2() = ATAN() but it takes into account the 4 quadrant of the Cartesian coordinates.
– AAEM
Apr 2, 2018 at 23:14
• Looks like you’ve got some misplaced norm symbols. That’s an awful lot of vertical bars before $v$.
– amd
Apr 2, 2018 at 23:24
• || (||v||u−||u||v) ||
– AAEM
Apr 2, 2018 at 23:25
• Which part don’t you understand: the ATAN2 part, the geometric meaning of the arguments that are being passed to it, or the whole thing?
– amd
Apr 2, 2018 at 23:28

That looks horribly complicated, and all those norms can't be cheap to compute. What I'd do given vectors $u=(x_1,y_1)$ and $v=(x_2,y_2)$ is first consider the matrix $$\begin{pmatrix} x_1 & y_1 \\ -y_1 & x_1 \end{pmatrix}$$ which is a combination of a scaling by $|u|$ (which for the purpose of angles we can ignore) and a rotation matrix that rotates $u$ down to the positive $x$-axis. Therefore, $$\begin{pmatrix} x_1 & y_1 \\ -y_1 & x_1 \end{pmatrix} \begin{pmatrix} x_2 \\ y_2 \end{pmatrix} = \begin{pmatrix} x_1x_2 + y_1y_2 \\ x_1y_2 - x_2y_1 \end{pmatrix}$$ has the same angle with the $x$-axis as $v$ has to $u$, and you can then compute this angle as $$\operatorname{atan2}(x_1x_2 + y_1y_2, x_1y_2 - x_2y_1 )$$ (or the other way around, depending on your programming languages's conventions for the order of the arguments to atan2).

• ok, it is another method for finding that angle. good explanation
– AAEM
Apr 3, 2018 at 1:51

It’s easier to understand if you rearrange things a bit. Assuming that neither $u$ nor $v$ is zero, consider the unit vectors $u' = {u\over\|u\|}$ and $v' = {v\over\|v\|}$. $u'+v'$ and $u'-v'$ are the diagonals of the rhombus formed by these unit vectors. These diagonals bisect the vertex angles and are perpendicular to and bisect each other, so it should be fairly clear that if $\theta$ is the angle between $u'$ and $v'$, then $\tan\frac\theta2 = {\|u'-v'\|/2 \over \|u'+v'\|/2} = {\|u'-v'\| \over \|u'+v'\|}$. Multiply both the numerator and denominator by $\|u\|\|v\|$ to obtain $$\tan\frac\theta2 = {\bigl\|\|v\|u-\|u\|v\bigr\| \over \bigl\|\|v\|u+\|u\|v\bigr\|},$$ and solve for $\theta$ to get the formula in your question. This equation also gives you expressions for the angle bisectors of two arbitrary nonzero vectors $u$ and $v$: they are $\|v\|u\pm\|u\|v$.

That said, this requires computing four vector norms so doesn’t look to me like a particularly efficient way to compute this angle. As a much more efficient alternative, we know that $$u\cdot v=u_xv_x+u_yv_y = \|u\|\|v\|\cos\theta \\ \det\begin{bmatrix}u&v\end{bmatrix} = u_xv_y-u_yv_x = \|u\|\|v\|\sin\theta$$ therefore $$\tan\theta = {u_xv_y-u_yv_x \over u_xv_x+u_yv_y}.$$

• thanks for your very good explanation. one more thing, i have read that in case of small angles, the first formula is better than the second one , is this true if yes,why?
– AAEM
Apr 3, 2018 at 0:39
• and you if we solve the above equation for theta, we will use the ATAN() Not ATAN2()?? i were asking about the ATAN2()
– AAEM
Apr 3, 2018 at 1:12
• @Ahmed ATAN2 is just a version of ATAN that accepts the numerator and denominator separately so that it can select the correct quadrant for the angle.
– amd
Apr 3, 2018 at 1:48