[Math] Find the Angle ( as Measured in Counter Clock Wise Direction) Between Two Edges

geometrytrigonometry

This is a similar question to this one, but slightly different.

The question is given two edges ($e_1$ and $e_2$, with the vertex coordinates known), how to find the angles from $e_1$ to $e_2$, with the angles measured in anti clock wise direction?

A diagram is shown below:

diagram

One way I can think of is to compute the cross and dot product of the two edge's unit vectors:

$$\sin\,\theta=\frac{|e_1\times e_2|}{|e_1||e_2|}$$
$$\cos\,\theta=\frac{e_1\cdot e_2}{||e_1|| ||e_2||}$$

And try to find the $\theta$, taken into account of whether $\sin\theta$ and $\cos\theta$ is $>0$ or $<0$. But this is very, very tedious and error-prone. Not to mention I'm not too sure whether the angle I get is always measured in counterclockwise direction or not.

Is there a single, clean formula that allows me to do what I want to do?

Best Answer

The way to get the smaller angle spanned by $\mathbf e_1=(x_1,y_1)$ and $\mathbf e_2=(x_2,y_2)$ is through the expression

$\min(|\arctan(x_1,y_1)-\arctan(x_2,y_2)|,2\pi-|\arctan(x_1,y_1)-\arctan(x_2,y_2)|)$

where $\arctan(x,y)$ is the two-argument arctangent.

Related Question