raw snippet

The perp product can be used to test if a point \(\mathbf{P}\) is on the left or right of a line given by the points \(\mathbf{A}\) and \(\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 \(\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 \(\mathbf{a}=\mathbf{B}-\mathbf{A}\) and \(\mathbf{b}=\mathbf{P}-\mathbf{A}\), which gives

\[\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"
}