AlgoScope

Computational Geometry

Points, vectors, and one signed quantity, the cross product, that decides most questions about a plane.

13 topics1 lessons2 families

Geometry in code starts with two objects: a point, which is a position, and a vector, which is the difference between two points. Almost everything after that reduces to one of two products. The dot product is positive when two vectors point the same general way, zero when they are perpendicular, negative when they oppose. The 2D cross product of (b - a) and (c - a) is twice the signed area of triangle abc, and its sign says whether the turn at b goes left, right or straight on.

That sign is the orientation test, and it carries the whole category. Two segments cross when each straddles the other's line, which is four orientation tests and no division. A polygon's area is half the absolute value of the summed cross products of consecutive vertices. A convex hull is built by sorting the points once and then walking them with a stack, popping any vertex where the turn goes the wrong way: monotone chain sorts by x and builds a lower and an upper chain, Graham scan sorts by angle around the lowest point, and both cost O(n log n) with the sort as the dominant term.

Two patterns cover the rest. Divide and conquer splits the points by x, solves each half, and then compares only the points inside a narrow strip around the divider, which brings closest pair down to O(n log n) from the n^2 of all pairs. A sweep line moves across the plane, keeps an active set of whatever it currently crosses, and does work only at event points, which finds k segment intersections in O((n + k) log n) instead of testing every pair.

After this you can

  • Compute a dot product and a cross product and say what each sign means
  • Decide whether two segments intersect using orientation tests alone
  • Build a convex hull by sorting the points and popping the wrong turns
  • Compare squared distances so that no square root enters the comparison
  • Recognise when a sweep line replaces a comparison of every pair
AB

Distance: the segment is the hypotenuse of a right triangle with legs 4 and 3, so the squared length is 4^2 + 3^2 = 25 and the length is sqrt(25), about 5. Comparing squared distances avoids the square root and stays exact in integers.

Open in the player →or start at step 2

In this order

  1. Points and VectorsA point is a position; a vector is a displacement between points, drawn as an arrow.
  2. DistanceEuclidean distance sqrt(dx^2 + dy^2); compare squared distances to avoid the root.
  3. Dot Producta . b = |a||b| cos theta; the sign tells whether vectors point the same way.
  4. Cross ProductThe 2D cross product is twice the signed area of the triangle; its sign is the turn direction.
  5. OrientationClockwise, counter-clockwise or collinear from the sign of the cross product of (b - a) and (c - a).
  6. Segment IntersectionTwo segments cross when each straddles the other's line, decided by four orientation tests.
  7. Line IntersectionWhere two segments meet is decided by orientations, then the point by cross-product ratios.
  8. Polygon AreaHalf the absolute sum of cross products of consecutive vertices, the shoelace formula.
  9. Convex HullThe smallest convex polygon containing all points, built by monotone chain from sorted points.
  10. Monotone ChainSort by x, then build the lower and upper chains, popping while the turn is not a left turn.
  11. Graham ScanSort points by angle around the lowest point; keep a stack, popping on right turns.
  12. Closest Pair of PointsDivide by x, recurse, then check the strip around the divider; O(n log n).
  13. Sweep LineMove a line across the plane, processing events in order and maintaining an active set.

Where people go wrong

Floating point inside an exact test

Orientation is a sign, and a sign computed from doubles can come out wrong when three points are nearly collinear, which makes a hull algorithm pop a vertex it should have kept. With integer input, compute the cross product in integers and in a type wide enough for the products: coordinates up to 10^9 need 64 bits.

Collinear points on the hull boundary

Three points in a line along the boundary are a valid hull either way. Popping whenever the turn is not a left turn removes them; popping only on a strict right turn keeps them. Decide which the problem wants before assuming the output is wrong.

Degenerate segments and the straddle test

The four-orientation test reports no intersection when two segments merely touch at an endpoint, or when they are collinear and overlap, because neither case is a strict straddle. Those need an explicit on-segment check, and they account for most segment intersection bugs.

Or a different category

Mathematics

The quantity you want is numeric rather than positional: modular arithmetic, primes, combinatorics or exact fractions.

Divide and Conquer

You want the splitting pattern and its recurrence in general, rather than one plane problem that happens to use it.

Lessons that teach these