Convex Hull Simulator: Algorithm Visualization & Comparison

simulator intermediate ~10 min
Loading simulation...
~8 hull vertices from 30 random points

30 uniformly distributed points typically produce a convex hull with about 8 vertices — most points are interior, demonstrating the O(log n) expected hull size.

Formula

A = 0.5 * |sum_i(xi*yi+1 - xi+1*yi)| — shoelace formula for area
Gift-wrapping: O(nh), Graham scan: O(n log n)
E[h] = O(log n) for uniform random points in convex region

The Rubber Band Problem

Imagine pushing pins into a board at random positions, then stretching a rubber band around all of them. The shape it forms is the convex hull — the tightest convex boundary enclosing every point. This intuitive operation is one of the most fundamental problems in computational geometry, appearing as a subroutine in collision detection, image processing, and geographic analysis.

Gift-Wrapping: The Intuitive Algorithm

The Jarvis march (1973) mimics wrapping a gift: start at the leftmost point, then repeatedly pivot to the point making the smallest angle. Each step finds one hull vertex by scanning all points, taking O(n) time per vertex and O(nh) total. When h is small (few hull vertices), this is efficient; when all points are on the hull, it degrades to O(n²).

Graham Scan: Sort and Stack

Ronald Graham's 1972 algorithm takes a different approach: sort all points by polar angle, then process them in order using a stack. Each point either extends the hull (left turn) or triggers backtracking (right turn pops the stack). The stack ensures each point is processed at most twice, giving O(n log n) total — dominated by the initial sort.

Output-Sensitive Optimality

Neither gift-wrapping nor Graham scan is optimal in all cases. Timothy Chan's 1996 algorithm achieves O(n log h) — optimal for every input. It partitions points into groups, runs Graham scan on each group, then gift-wraps the group hulls. This elegant combination exploits the best properties of both predecessors.

FAQ

What is a convex hull?

The convex hull of a point set is the smallest convex polygon containing all points — imagine stretching a rubber band around pins on a board. It is a fundamental structure in computational geometry, serving as a preprocessing step for many algorithms.

What is the gift-wrapping algorithm?

Gift-wrapping (Jarvis march) starts at the leftmost point and repeatedly selects the point making the smallest counterclockwise angle. It 'wraps' around the hull in O(nh) time, where h is the hull size. Simple to implement but slow for large interior-heavy sets.

What is Graham scan?

Graham scan sorts points by polar angle from the lowest point, then processes them in order, using a stack to maintain the hull. Left turns extend the hull; right turns pop the stack. It runs in O(n log n) time regardless of hull size, dominated by the initial sort.

What is the optimal convex hull algorithm?

Chan's algorithm (1996) achieves O(n log h) time by running Graham scan on small groups and then gift-wrapping the group hulls. This is optimal — no algorithm can do better in the comparison-based model, as convex hull computation reduces to sorting.

Sources

Embed

<iframe src="https://homo-deus.com/lab/computational-geometry/convex-hull/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub