The perp product can be used to test if a point P is on the left or right of a line given by the points A and B, which basically calculates the area of the parallelogram spanned by two vectors. The area is positive when the point is on the left of line AB and on the right when the area is negative. The area is 0 when the point is on the line.
Now let a=B−A and b=P−A, which gives
a⊥b=0a⊥b>0a⊥b<0⇔a and b are collinear (all 3 points are on a line), i.e ∣θ∣=0° or 180°⇔P is on left of AB, i.e. 0<θ<180∘⇔P is on right of AB, i.e. −180∘<θ<0
JavaScript Implementation
functionpointOrientation(A, B, P) {
var a = {x: B.x - A.x, y: B.y - A.y};
var b = {x: P.x - A.x, y: P.y - A.y};
var perp = a.x * b.y - a.y * b.x;
if (perp > 0) return"left";
if (perp < 0) return"right";
return"collinear"
}