I am getting as an input 4 points.
p1=(x1, y1), p2=(x2, y2), p3=(x3, y3), p4=(x4, y4).
Now I have calculated the distances between p1p2, p2p3, p3p4 and p4p1.
Mathematically, the angle is acos(dot(||p2p1||, ||p2p3||))
. Try
u21 = (p1 - p2) / norm(p1 - p2)
u23 = (p3 - p2) / norm(p3 - p2)
angle = acos(sum(u21 .* u23))
The prefix u
indicates unit vectors.
To get the angle between two vectors, you simply use their inner product:
v1 = p1-p2;
v2 = p3-p2;
Normalize to unit vectors and take inner product:
n1 = v1/norm(v1);
n2 = v2/norm(v2);
cos_p2 = dot(n1,n2);
And the resulting angle is
acos(cos_p2)