Convex Hull Algorithm
Explore convex hulls in computational geometry - understand the Graham scan algorithm, cross products for orientation testing, and how hull size relates to point distributions
Try these examples:
Convex Hull Algorithm
The convex hull of a set of points is the smallest convex polygon that contains all the points -- imagine stretching a rubber band around nails on a board. Formally, for a set of points:
The boundary vertices are called extreme points. How many extreme points you get -- and how fast you can find them -- depends on the distribution and the algorithm.
Start with
Convex hull of the point set
The shaded polygon is the hull of exactly these points, computed on this page:
Hull Size and Distribution
Random Distribution
For points drawn uniformly from a square, the expected number of hull vertices grows only logarithmically (Rényi and Sulanke, 1963):
where is Euler's constant; the approximation is close from about 10 points on. This sample has , leaving
Drag
The hull has perimeter
The Orientation Test
Every convex hull algorithm relies on the same geometric primitive: the cross product orientation test. For three points :
Positive means counterclockwise (left turn), zero means collinear, negative means clockwise (right turn). For , , :
Since
Graham Scan
The Graham scan (1972) builds the hull in . Set the algorithm view to
- Anchor: Find the lowest point (guaranteed on the hull)
- Sort: Order all other points by polar angle , nearer points first when angles tie -- this sort is the bottleneck
- Scan: Maintain a stack. For each point in that order, pop the stack while the top two points and the new point fail to make a counterclockwise (left) turn, using the cross product test, then push the new point
Step through the scan on the points above. After
Graham scan, step by step
The dotted rays from show the angular order in which points are processed; the thick chain is the stack. Step onto a point that makes a clockwise turn and the top of the stack drops off. At the last step the chain, closed back to , is the hull above.
Algorithm Race
How do the algorithms compare on your
Operations against n (log scale)
- Brute force : ~
8,000 operations (every ordered pair of points is a candidate edge, checked against every point) - Graham scan : ~
86 operations - Gift wrapping : ~
160 operations ( wrapping steps, each checking all points)
The counts drop constant factors, and the vertical axis is logarithmic: each step of 1 is a factor of 10.
Here the hull has vertices among points. Since , Graham scan does less work. Gift wrapping degrades toward as approaches .
Gift Wrapping (Jarvis March) walks the hull boundary by always choosing the point with the smallest counterclockwise angle -- intuitive but . Quickhull partitions points recursively like Quicksort, achieving expected time. Chan's algorithm combines the best of both for the optimal .
Switch to
Point Classification
Every point falls into one of three categories relative to the hull: vertex (extreme point on a corner), boundary (on a hull edge between vertices), or interior (strictly inside). To test whether a query point is interior, check that it lies on the counterclockwise side of every directed hull edge -- the "half-plane intersection" method, running in or with binary search.
Try adjusting
Try it in Code
# Convex Hull (Graham Scan)
import numpy as np
from scipy.spatial import ConvexHull
n = 20
np.random.seed(42)
points = np.random.rand(n, 2)
hull = ConvexHull(points)
print(f"Points: {n}")
print(f"Hull vertices: {len(hull.vertices)}")
print(f"Interior points: {n - len(hull.vertices)}")
print(f"Hull area: {hull.volume:.4f}")What to explore next
Binary Numbers
Explore how decimal numbers are represented in binary using powers of 2
Game Theory Decision Trees
Explore extensive form games and learn backward induction to find optimal strategies
Graph Coloring and Chromatic Number
Explore proper vertex coloring, the chromatic number, greedy coloring, and the Four Color Theorem through interactive graph examples
Graph Theory Basics
Learn the fundamentals of graph theory through interactive visualizations - vertices, edges, degrees, paths, and special graph types
You might also like
Graph Coloring and Chromatic Number
ExplorationExplore proper vertex coloring, the chromatic number, greedy coloring, and the Four Color Theorem through interactive graph examples
Graph Theory Basics
ExplorationLearn the fundamentals of graph theory through interactive visualizations - vertices, edges, degrees, paths, and special graph types
Binary Numbers
ExplorationExplore how decimal numbers are represented in binary using powers of 2
Binary Representation Calculator
Explore binary representation of numbers by showing individual bit positions and their place values