# Two 2d vector angle clockwise predicate

I've got three vectors v1[0,1], v2[-1,1], v3[1,1].

I calculated the angles beetween v1-v2 and v1-v3 using the forumla dot(vec1, vec2) / ||vec1|| * ||vec2|| and the both angles are the same (45 degrees).

How to determine that the vector v2 lies on the countercloclwise side (CCW) form v1 (CW angle is 335 degress), and v3 lies on the clockwise (CW) side form v1 (CW angle is 45 degrees).

-

For 2 vectors $a = (a_x, a_y), b = (b_x, b_y)$ compute $$c_z = a_x b_y - a_y b_x$$ If $c_z > 0$ then $b$ is on the CCW side of $a$. If $c_z < 0$ then $b$ is on the CW side of $a$. If $c_z = 0$ then $a$ and $b$ are parallel.

The reasoning behind this formula is that you can consider the 2D vectors as 3D vectors in the x/y plane: $a = (a_x, a_y, 0), b = (b_x, b_y, 0)$. The $c_z$ component of the vector product

$$c = a \times b = (0, 0, a_x b_y - a_y b_x)$$

characterizes the orientation of the vectors $a,b$ with respect to each other.

Applied to your case: $$v_1 \times v_2 = (0, 0, 1), v_2 \text{ is left (CCW) to } v_1$$ $$v_1 \times v_3 = (0, 0, -1), v_3 \text{ is right (CW) to } v_1$$

-

You can use the two dimensional arctangent to get the angle of a vector in the range $(-\pi,\pi]$ or you can use the regular arctangent and sort out the quadrants by hand. Then you still need to worry about crossing the boundary: you probably want to regard a vector at $-0.9\pi$ as counterclockwise from one at $0.9\pi$, so you can subtract one angle from the other, add or subtract $2\pi$ until you get into the range $(-\pi,\pi]$, and regard results greater than $0$ as CCW.

-

Let's observe angles of the vectors in the range $[0.\pi]$ , (see picture bellow). We may define vector $\vec v_4=(1,0)$ and then calculate angles between $\vec v_4$ and other of three vectors:

$\cos \measuredangle (\vec v_4,\vec v_1)=\frac{\vec v_4\vec v_1}{|\vec v_4||\vec v_1|}=0\Rightarrow \measuredangle (\vec v_4,\vec v_1)=\frac{\pi}{2}$

$\cos \measuredangle (\vec v_4,\vec v_2)=\frac{\vec v_4\vec v_2}{|\vec v_4||\vec v_2|}=\frac{-1}{\sqrt{2}}\Rightarrow\measuredangle (\vec v_4,\vec v_2)>\frac{\pi}{2}$

$\cos \measuredangle (\vec v_4,\vec v_3)=\frac{\vec v_4\vec v_3}{|\vec v_4||\vec v_3|}=\frac{1}{\sqrt{2}}\Rightarrow \measuredangle (\vec v_4,\vec v_3)<\frac{\pi}{2}$

So we may conclude that vector $\vec v_2$ lies on the countercloclwise side from $\vec v_1$ , and vector $\vec v_3$ lies on the clockwise side from $\vec v_1$

-