Convex Hulls and the Sweep-Line Paradigm
Learning Objectives
- Compute the orientation predicate as the sign of a determinant and use it to decide left turns, right turns, and collinearity without division or angles.
- Run Andrew's monotone chain algorithm to build a convex hull in and prove the matching lower bound by reduction from sorting.
- Describe the sweep-line paradigm (event queue plus status structure) and apply it to Bentley-Ottmann segment intersection in .
- Explain why geometric code needs exact or robust predicates and explicit handling of degenerate inputs.
Context & Motivation
Computational geometry studies algorithms on points, segments, and polygons, and it underlies GIS map overlays, collision detection in games and robotics, mesh generation for simulation, and the rasterization in specialization/computer-graphics. Its problems look elementary when drawn by hand and become surprisingly subtle in code, where "is this point to the left of that line?" must be answered correctly for millions of points with finite-precision numbers.
Two ideas dominate the introductory theory, as presented in de Berg, Cheong, van Kreveld, and Overmars's standard textbook and in courses such as CMU's 15-456 and Stanford's CS268. First, almost every algorithm reduces to a handful of exact predicates, of which the orientation test is the most important. Second, many problems in the plane are solved by a sweep line: imagine a vertical line moving left to right, maintain what it currently intersects, and do work only at discrete events. This concept develops both through two classic problems, the convex hull and segment intersection.
Core Theory
The orientation predicate
For points in the plane:
It equals twice the signed area of triangle . Positive means turns left (counterclockwise), negative means a right turn, zero means the three points are collinear. With integer coordinates it uses only subtraction and multiplication, so it can be computed exactly. Segment intersection tests, point-in-polygon tests, and hull algorithms all reduce to signs of this expression.
Convex hull: Andrew's monotone chain
The convex hull of a point set is the smallest convex polygon containing it, like a rubber band snapped around nails. Andrew's algorithm (1979):
- Sort the points by , breaking ties by .
- Lower hull: scan left to right, maintaining a stack. Before pushing each point, pop while the last two stack points and the new point do not make a left turn ().
- Upper hull: the same scan from right to left.
- Concatenate, dropping the duplicated endpoints.
Each point is pushed and popped at most once per pass, so after sorting the scan is linear: total. If the output has only vertices, Chan's algorithm achieves , which is better when the hull is small.
A lower bound by reduction from sorting
No comparison-based hull algorithm can beat . Given numbers to sort, map each to the point on the parabola . Every such point is a hull vertex (the parabola is convex), and the hull, reported in counterclockwise order, lists them in sorted order of . A hull algorithm faster than would therefore sort faster than the comparison sort lower bound from algorithms-software/algorithms allows (in the algebraic decision tree model used for geometric algorithms).
The sweep-line paradigm
Move a vertical line from left to right across the plane. Maintain two structures:
- an event queue, a priority queue of -coordinates where something changes;
- a status structure, a balanced BST of the objects currently crossing the sweep line, ordered by where they cross it.
Between events nothing combinatorial changes, so the algorithm only works at events, turning a continuous problem into a discrete sequence of updates.
Bentley-Ottmann segment intersection
Report all intersections among line segments. Checking every pair costs even when is tiny. The sweep relies on one observation: just before two segments intersect, they are adjacent in the vertical order along the sweep line. So:
- At a left endpoint, insert the segment into the status structure and test it against its new neighbors above and below.
- At a right endpoint, delete it and test its former neighbors against each other, since they just became adjacent.
- At an intersection, report it, swap the two segments' order in the status, and test each against its new neighbor.
Every discovered intersection is inserted as a future event. Each event costs for the queue and status operations, and there are events, giving , which is output-sensitive: fast when intersections are few.
Robustness and degeneracies
Textbook pseudocode assumes general position: no three collinear points, no two points with the same , no three segments through one point. Real data violates all of these, and floating-point evaluation of can return the wrong sign for nearly collinear points, making a hull algorithm output a non-convex polygon or crash. Production libraries use exact integer arithmetic or adaptive-precision predicates (Shewchuk's are standard) and handle ties explicitly, for example deciding whether collinear boundary points belong to the hull.
Worked Examples
Example 1: building a hull by hand
Problem: Compute the convex hull of with the monotone chain.
Trace: Sorted: . Lower hull: push , . For : , pop , push . For : , push. For : , pop; , pop; push. For : , push. Lower: . The upper scan from the right gives . Combined counterclockwise: . The point is interior and was popped in both passes.
Example 2: why only neighbors need checking
Problem: Segments from to , from to , and from to . Trace the first events of the sweep and find which pairs are tested.
Trace: Events by : starts at 0, at 1, at 2. At : status . At : is at , above (at ); status bottom to top ; test against : they cross at , added as an event. At : is at , at 2, at 7, so is inserted between them: ; test with (crossing at ) and with (crossing at ), both added. Next event at : and swap, giving , and is now tested against its new neighbor , already known. The algorithm never compares segments that are not adjacent at some moment, which is why it scales with rather than .
Common Misconceptions & Pitfalls
- "Use angles to decide turns." Computing angles with
atan2and comparing them is slower and less accurate than the orientation determinant, and it introduces wraparound special cases. Signs of determinants are the standard. - "Floating point is fine for geometry." For nearly degenerate inputs, rounding flips predicate signs, and algorithms that assume consistent answers can loop, crash, or return invalid output. Exact or robust predicates are a correctness requirement, not a nicety.
- "Bentley-Ottmann is always better than checking all pairs." When is close to , its is worse than the brute force. The sweep pays off when intersections are sparse, which is common in practice.
Summary
Computational geometry reduces to exact predicates, chiefly the orientation test, the sign of a determinant that distinguishes left turns, right turns, and collinearity. Andrew's monotone chain sorts the points and builds lower and upper hulls with a stack in , optimal because points on a parabola turn hull construction into sorting. The sweep-line paradigm processes discrete events with a priority queue and keeps the objects crossing the line in a balanced tree; Bentley-Ottmann uses it to report segment intersections in by testing only segments that become adjacent. Exact predicates and explicit handling of degeneracies make these algorithms correct on real data. The next concept builds the two most important structures on point sets: Voronoi diagrams and Delaunay triangulations.