Angle between 2 vectors using the determinant

2.7k Views Asked by At

I have a polygon like this:

enter image description here

I basically want to find the angles $\alpha$, inside the polygon, between the vectors. I'm using the determinant to calculate the angle alpha:

$det(\vec V2, \vec V2 ) = ||V2|| * ||V1|| * \sin \theta $

So $\theta$ will be the arcsin. But my problem is that in this particular case, the angle that is calculated is not $\alpha$ but it's 180- $\alpha$ and I don't understand why.

This is my code in C:

TSMV_Point2D vecteur1, vecteur2;
vecteur1.x = p1.x - pC.x;
vecteur1.y = p1.y - pC.y;
vecteur2.x = p2.x - pC.x;
vecteur2.y = p2.y - pC.y;

float determinant = vecteur2.x * vecteur1.y - (vecteur1.x * vecteur2.y);
float normeVec1 = sqrt(vecteur1.x * vecteur1.x + vecteur1.y * vecteur1.y);
float normeVec2 = sqrt(vecteur2.x * vecteur2.x + vecteur2.y * vecteur2.y);

float division = determinant / (normeVec1 * normeVec2);
float angle = asin(division) * 180 / M_PI;

Can someone help me understand?

2

There are 2 best solutions below

3
On

Because the formula you have is not correct. The dot product of two vectors equals the product of the norms of the vectors times cosine of the angle.

$$\mathbf{V_1} \cdot \mathbf{V_2}=||\mathbf{V_1}||\cdot ||\mathbf{V_2}|| \cdot \cos\theta$$

0
On

Well, usually angles between two vectors are considered to be lying in $[0,\pi]$ and so choosing a formula with $\arccos$ in it is a better choice, as @Badoe mentioned.

However, in your particular case, keep in mind that $\arcsin$ is defined over $\left[-\frac{\pi}{2},\frac{\pi}{2}\right]$, so, it is very possible that the angle you would like to get is actually larger than $90^\circ$ and, hence, $\arcsin$ moves is back into $\left[-\frac{\pi}{2},\frac{\pi}{2}\right]$, resulting to giving you the complementary angle of what you've been looking for.

But, the above is just a speculation, it would be more helpful if someone could enlight us on how is $\arcsin$ implemented in $C$. Till then, you can always use the formula: $$\theta=\arccos\frac{\vec{v}\cdot\vec{u}}{\lVert \vec{v}\rVert\lVert \vec{u}\rVert}$$