22. Convex Hull
Find the tightest boundary around a set of points
Imagine you have a handful of nails hammered into a board and you stretch a rubber band around all of them. When you let go, the band snaps tight around the outermost nails, forming a convex polygon. The nails on the boundary are the convex hull — the smallest convex shape that contains every point.
Computing convex hulls is one of the fundamental problems in computational geometry. It shows up in collision detection, image processing, geographic information systems, and as a subroutine in dozens of other algorithms.
The Problem
Given a set of n points in the plane, find the convex hull — the smallest convex polygon that contains all the points. The output is an ordered list of the points that form the polygon's vertices, typically in counterclockwise order.
What does "convex" mean?
A shape is convex if for any two points inside it, the entire line segment between them is also inside. No dents, no concavities. A convex polygon's interior angles are all less than 180°.
The Cross Product Test
Before we dive into algorithms, we need one geometric primitive: the cross product of two 2D vectors. Given three points P, Q, R, we can determine whether the path P → Q → R makes a left turn, a right turn, or goes straight:
Cross product for turn direction
cross(P, Q, R) = (Q.x - P.x) × (R.y - P.y) - (Q.y - P.y) × (R.x - P.x)
If cross > 0: left turn (counterclockwise)
If cross < 0: right turn (clockwise)
If cross = 0: collinear (straight line)
This single test is the workhorse of every convex hull algorithm. When building the hull counterclockwise, we want only left turns. A right turn means the middle point is inside the hull and should be removed.
Graham Scan
Graham scan was one of the first efficient convex hull algorithms, published by Ronald Graham in 1972. It sorts points by angle and then processes them in order, maintaining a stack of hull candidates.
-
Find the lowest point
Pick the point with the smallest y-coordinate (break ties by smallest x). This point is guaranteed to be on the hull. Call it P₀.
-
Sort by polar angle
Sort all other points by their polar angle relative to P₀. Points at the same angle are sorted by distance from P₀ (closest first).
-
Process points using a stack
Push P₀ and the first sorted point onto a stack. For each subsequent point, check the turn direction formed by the stack's second-to-top, top, and new point. While it's a right turn (or collinear), pop the top. Then push the new point.
-
The stack is the hull
After processing all points, the stack contains the hull vertices in counterclockwise order.
Worked Example
Points: A(0,0), B(4,0), C(4,4), D(2,5), E(0,4), F(2,2)
Step 1: Lowest point = A(0,0)
Step 2: Sort by polar angle from A:
B(4,0) at 0°, F(2,2) at 45°, C(4,4) at 45°, E(0,4) at 90°, D(2,5) at 68°
Sorted: B, F, C, D, E
(F before C — same angle, F is closer)
Step 3: Process:
Push A, B Stack: [A, B]
Process F(2,2): A→B→F is left turn. Push. Stack: [A, B, F]
Process C(4,4): B→F→C is left turn. Push. Stack: [A, B, F, C]
Wait — F is between B and C on angle 45°.
Actually: B→F→C is left. Stack: [A, B, F, C]
Process D(2,5): F→C→D is left turn. Push. Stack: [A, B, F, C, D]
Process E(0,4): C→D→E is left turn. Push. Stack: [A, B, F, C, D, E]
But D→E, check: F→C→D→E...
Recheck: C→D→E is left. Stack: [A, B, F, C, D, E]
Hmm, but F is an interior point. Let's re-trace more carefully:
Push A, B Stack: [A, B]
Process F(2,2): A→B→F = cross(A,B,F) > 0 (left turn). Push F.
Stack: [A, B, F]
Process C(4,4): B→F→C = cross(B,F,C) > 0 (left turn). Push C.
Stack: [A, B, F, C]
Process D(2,5): F→C→D = cross(F,C,D) > 0 (left turn). Push D.
Stack: [A, B, F, C, D]
Process E(0,4): C→D→E = cross(C,D,E) > 0 (left turn). Push E.
Stack: [A, B, F, C, D, E]
Final hull: A, B, F, C, D, E
In this example, F actually lies on the hull edge from B to C. In practice, many implementations exclude collinear points — keeping only the corners of the hull.
Graham Scan Pseudocode
function grahamScan(points):
p0 = point with lowest y (then lowest x)
sort remaining points by polar angle from p0
stack = [p0, points[0]]
for i = 1 to n-1:
while stack.size >= 2
and cross(stack[-2], stack[-1], points[i]) <= 0:
stack.pop()
stack.push(points[i])
return stack
Complexity: O(n log n) — dominated by the sorting step. The stack operations are O(n) total because each point is pushed and popped at most once.
Andrew's Monotone Chain
Andrew's algorithm (1979) is often preferred over Graham scan because it avoids trigonometric functions entirely. Instead of sorting by angle, it sorts by x-coordinate and builds the hull in two passes: the lower hull (left to right) and the upper hull (right to left).
-
Sort points by x-coordinate
Break ties by y-coordinate. This is a simple lexicographic sort.
-
Build the lower hull
Scan points left to right. Maintain a stack. For each point, while the last two points on the stack and the new point make a right turn (or are collinear), pop the stack. Then push the new point.
-
Build the upper hull
Scan points right to left. Same logic — pop on right turns, push the point.
-
Concatenate
Join the lower and upper hulls (removing the duplicate endpoints) to get the full convex hull in counterclockwise order.
Andrew's Pseudocode
function monotoneChain(points):
sort points by (x, y) ascending
// Lower hull
lower = []
for p in points:
while lower.size >= 2
and cross(lower[-2], lower[-1], p) <= 0:
lower.pop()
lower.push(p)
// Upper hull
upper = []
for p in reverse(points):
while upper.size >= 2
and cross(upper[-2], upper[-1], p) <= 0:
upper.pop()
upper.push(p)
// Remove last point of each half (it's the first of the other)
return lower[:-1] + upper[:-1]
Complexity: O(n log n) — same as Graham scan, but typically faster in practice because comparing coordinates is cheaper than computing angles.
Graham Scan vs. Andrew's Monotone Chain
| Property | Graham Scan | Andrew's |
|---|---|---|
| Sort key | Polar angle from lowest point | x-coordinate (lexicographic) |
| Passes | One pass (counterclockwise) | Two passes (lower + upper hull) |
| Trig functions? | Yes (atan2 or equivalent) | No — only cross products |
| Numerical stability | Angle ties can cause issues | More robust |
| Complexity | O(n log n) | O(n log n) |
The Lower Bound
Computing the convex hull requires O(n log n) time in the worst case. This can be proved by reduction from sorting: given n numbers to sort, create points (xᵢ, xᵢ²). These points lie on a parabola, and their convex hull — read in order — gives the sorted sequence. Since comparison-based sorting has an Ω(n log n) lower bound, so does convex hull.
Output-Sensitive Algorithms
If the hull has only h vertices (where h ≪ n), algorithms like Chan's algorithm achieve O(n log h) time. When most points are interior, this is significantly faster than O(n log n).
Where Convex Hulls Are Used
| Application | How it uses convex hulls |
|---|---|
| Collision detection | Two convex shapes don't overlap if and only if a separating axis exists — convex hulls enable fast GJK/SAT tests |
| Geographic Information Systems | Compute the bounding region of a set of locations (e.g., "area covered by cell towers") |
| Image processing | Find the outline of a set of feature points; defect detection in manufacturing |
| Path planning | Convex hull of obstacles defines forbidden regions for robot navigation |
| Statistics | Identify outliers — points on the hull are the "extreme" data points |
| Linear programming | The feasible region of a 2D LP is a convex polygon; vertices are the candidates for the optimum |
Key Takeaways
- The convex hull is the smallest convex polygon containing all points — the "rubber band" boundary
- The cross product test determines turn direction and is the core primitive for all hull algorithms
- Graham scan sorts by polar angle and processes points with a stack in one pass — O(n log n)
- Andrew's monotone chain sorts by x-coordinate and builds lower + upper hulls — O(n log n) with no trig
- O(n log n) is optimal in the worst case (proven by reduction from sorting)
- Output-sensitive algorithms like Chan's achieve O(n log h) when the hull has few vertices
Try It Yourself — Convex Hull
Click on the canvas to place points (or generate a random set), then step through Graham scan or Andrew's monotone chain. Watch the algorithm test turn directions and build the hull one vertex at a time.
The convex hull is computational geometry's "Hello World" — simple to state, satisfying to visualize, and deeply connected to the rest of the field. The cross product test that drives it shows up everywhere in geometry: point-in-polygon tests, line segment intersection, triangulation, and more. If you enjoyed the visual, spatial reasoning here, the Quadtrees article explores another way to impose structure on points in the plane.