Snippet: Normal vector of a line segment

The vector \(\mathbf{v}=\mathbf{B}-\mathbf{A}\) between two points \(\mathbf{A}\) and \(\mathbf{B}\) has two normals, being the negation of each other. Rotating the vector \(\mathbf{v}\) counterclockwise is the definition of the perp opererator and gives one normal vector:

\[\mathbf{v}^\perp = (-v_y, v_x)\]

JavaScript implementation

function normals(A, B) {
  var dx = B.x - A.x;
  var dy = B.y - A.y;
  return [{x: -dy, y: dx}, {x: dy, y: -dx}];