B · Legendary · 13 min

Orientation, Cross Products, Hulls

The 2-D cross product (b-a)×(c-a) is the signed area of the parallelogram. Its sign is the turn: left, right, or collinear. Convex hull is 'keep left turns' while scanning.

Almost every computational-geometry primitive is the cross product in the plane:

(b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)

- > 0 — c is to the left of the directed line a→b (CCW) - < 0 — right (CW) - = 0 — collinear

CSES Point Location Test is this sign. Segment intersection is four orientations (each endpoint vs the other segment) plus a collinear overlap check. Convex hull (Andrew / monotone chain): sort points, scan, pop while the turn is not left.

Integer coordinates. Do not use atan2 for hulls if a cross product will do — floats WA on ties.

Signed turn, then a monotone-chain sketchjs
function cross(ax, ay, bx, by, cx, cy) {
  return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
}

function orientation(ax, ay, bx, by, cx, cy) {
  const v = cross(ax, ay, bx, by, cx, cy);
  if (v > 0) return 1;
  if (v < 0) return -1;
  return 0;
}

TRACE

a=(0,0), b=(2,0). Where is c?

c=(1,1)

cross = 2*1 - 0*1 = 2 > 0. Left / CCW.

+1

1 / 3

CHECK

Why can the cross product overflow in C++?

CHECK

Point in polygon, n ≤ 1e5, q ≤ 1e5. What is legal?

Checks 0/2

Next lesson