raw snippet

The perp product can be used to test if a point P\mathbf{P} is on the left or right of a line given by the points A\mathbf{A} and B\mathbf{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\vec{\mathbf{AB}} and on the right when the area is negative. The area is 0 when the point is on the line.

Now let a=BA\mathbf{a}=\mathbf{B}-\mathbf{A} and b=PA\mathbf{b}=\mathbf{P}-\mathbf{A}, which gives

ab=0a and b are collinear (all 3 points are on a line), i.e θ=0° or 180°ab>0P is on left of AB, i.e. 0<θ<180ab<0P is on right of AB, i.e. 180<θ<0\begin{array}{rl} \mathbf{a}\perp\mathbf{b}=0&\Leftrightarrow\mathbf{a}\text{ and }\mathbf{b}\text{ are collinear (all 3 points are on a line), i.e } |\theta|=\text{0° or 180°}\\ \mathbf{a}\perp\mathbf{b}>0&\Leftrightarrow \mathbf{P}\text{ is on left of } \vec{\mathbf{AB}}\text{, i.e. }0<\theta<180^\circ\\ \mathbf{a}\perp\mathbf{b}<0&\Leftrightarrow \mathbf{P}\text{ is on right of } \vec{\mathbf{AB}}\text{, i.e. }-180^\circ<\theta<0\\ \end{array}

JavaScript Implementation

function pointOrientation(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"
}