[Math] Calculate the angle between two vectors when direction is important

linear algebravector-spaces

Calculate the angle between two vectors
is very similar but I don't manage to adapt to my case. obvious mathematics aren't my strong area.

enter image description here

First I define my vectors like:

V1 = (x1, y1, x2, y2) 
V2 = (x2, y2, x3, y3) 

For A:

V1 = (-3, -3, 2, 2)
V2 = (2, 2, 6, -3) 

So need to convert to:

V1 = (x1, y1) 
V2 = (x2, y2) 

And second I need the counter clockwise angle to be between 0 and 360 instead of (-180, 180).

So I need the angle but depend on where they join to indicate if I have to add 180 or not.

If I use A.B = |A||B| cos(angle) always get the smaller angle.

Best Answer

For the two vectors $v_1 = (x_1,y_1)$, $v_2 = (x_2,\;y_2)$ the oriented angle between $v_1$ and $v_2$ is $$\angle(v_1, v_2) = \mathrm{atan2}\left(x_1y_2-y_1x_2,\;x_1x_2+y_1y_2\right)$$ where $\mathrm{atan2(y,x)}$ is the two argument $\arctan$ from https://en.wikipedia.org/wiki/Atan2. You can find the formula in this answer to the linked question. For more accurate (and more complicated) formulas in extreme cases, see "A uniformly accurate arctan(…) formula for the angle between two vectors" by W. Kahan, page 15, Kahan's formulas are valid for higher dimensions.

Example: For your red vector $v_1=(1,2)$ and the green $v_2=(1,-2)$ you compute $$\angle(v_1, v_2) = -1.107 = -63.44°,$$ this is roughly your left picture and for you second case $$\angle(v_1, -v_2)= 0.5880 = 33.68°.$$