Convex hull algorithms explained
Hammer nails into a board, stretch a rubber band round all of them and let go: the band snaps into the convex hull. Computing that shape is the hello world of computational geometry, and it has been solved in a surprising number of ways, each one a small lesson in algorithm design. This page explains the one test they all share, how the classic algorithms organise it, what they cost, and why getting the arithmetic exactly right matters.
The rubber band
A set of points is convex when the straight segment between any two of its points stays inside it. The convex hull of a finite set of points is the smallest convex set that contains them all: a convex polygon whose corners are some of the points. The rubber band is the best picture of it. Stretch a band round nails in a board and let go: it tightens until it touches only the outermost nails, and between them it runs in straight lines. Nails inside the band, or on a straight stretch of it, do not change its shape. Unless all the points lie on one line, the hull of n points has h corners, from 3 when most points lie inside a triangle up to n when they all lie on a circle.
One test: left or right
Walk from point a to point b and look at point c. The cross product of b − a and c − a, (bx − ax)(cy − ay) − (by − ay)(cx − ax), is positive when c is on your left (the path a → b → c turns counterclockwise), negative when it is on your right, and zero when the three points lie on one line. Its value is twice the signed area of the triangle abc. Walking round a convex polygon counterclockwise, every turn is a left turn, and a point lies inside it exactly when it is on the left of every edge. Every hull algorithm is a way of arranging these orientation tests, and counting them is a fair measure of the work.
Gift wrapping (Jarvis march)
R. A. Jarvis published the gift wrapping algorithm in 1973. Start at the leftmost point, which is certainly on the hull. Take any other point as the candidate for the next vertex and compare it with every remaining point: whenever a point lies to the right of the line from the current vertex to the candidate, it becomes the candidate. After one pass every point is on the left of the line, so the candidate is the next hull vertex; move there and repeat until you are back at the start. Each vertex costs n − 2 tests, so the whole run costs about n·h. That makes it output-sensitive: very fast when the hull is small, but n² when every point is on the hull, as on a circle.
Graham scan and the monotone chain
Ronald Graham's scan (1972) was the first O(n log n) algorithm. Pick the lowest point as the pivot and sort the others by the angle they make with it, which can be done with orientation tests instead of trigonometry. Then walk through them in that order with a stack: before pushing a new point, look at the top two points of the stack; if they and the new point make a right turn or go straight on, the top point cannot be a corner, so pop it and look again. Each point is pushed once and popped at most once, so after the sort the scan needs fewer than 2n tests. A. M. Andrew's monotone chain (1979) sorts by x instead of by angle, which avoids angles altogether, and runs the same stack twice: left to right for the lower hull and right to left for the upper hull.
QuickHull and the incremental algorithm
QuickHull, named after quicksort, starts with the leftmost and rightmost points, which are both on the hull, and splits the other points by the line through them. On each side, the point farthest from the line is on the hull too; together with the line it forms a triangle, and every point inside that triangle can be thrown away at once. The two new edges are handled in the same way with the points outside them. On random points most points disappear in the first few triangles, so QuickHull is often the fastest in practice, O(n log n) on average, but like quicksort it has a worst case of O(n²). The incremental algorithm adds the points one at a time: a point inside the hull so far is dropped, and a point outside finds the edges it can see, removes them and connects itself to their two ends. In random order and with a little bookkeeping it runs in expected O(n log n); the simple version in the app tests every edge, O(nh).
How fast can it be?
Sorting can be turned into a hull problem: put the numbers x₁ … xₙ on a parabola as the points (xᵢ, xᵢ²); all of them are on the hull, and reading the hull in order sorts the numbers. So no algorithm that works by comparisons can find the hull in fewer than about n log n steps in the worst case, and Graham scan is optimal in that sense. But when the hull is small, n log n is not the end of the story. Timothy Chan's algorithm (1996) splits the points into groups of size m, finds each group's hull with Graham scan and then wraps the whole set like Jarvis, finding the tangent to each small hull by binary search; guessing m = h by repeated squaring gives O(n log h), the best possible, matching the earlier but more complicated algorithm of Kirkpatrick and Seidel (1986).
Collinear points and exact arithmetic
The orientation test is simple, but computed in floating point it can be wrong: when three points are nearly on one line, the rounding error of the products can be larger than the true value, and the sign flips. A hull program that trusts such signs can produce polygons that are not convex, lose points or loop forever. Robust programs evaluate the predicate exactly, for example with integer coordinates or with Jonathan Shewchuk's adaptive precision arithmetic, which is fast when the answer is clear and exact when it is not. Collinear points also need a rule: does a point in the middle of a hull edge count as a corner? The app gives every point whole-number coordinates small enough for the cross product to be exact and treats a zero as not a left turn everywhere, so every algorithm returns only the true corners. The collinear trap, with points along the edges of a square, on a line through the pivot and repeated, tests exactly that.
Onion layers and uses of the hull
Remove the hull's vertices and take the hull of what is left, again and again: the nested polygons are the convex layers, and finding them is called onion peeling. Bernard Chazelle showed in 1985 that all the layers can be found in O(n log n). The layer a point belongs to is its depth, a robust way to find the centre of a cloud of data that ignores outliers. Hulls are everywhere in computing: collision detection in games and robotics tests convex shapes first, because two convex polygons are apart exactly when a straight line separates them; image analysis uses the hull of a shape to measure how convex it is; maps use it for the extent of a set of places; and the hull of points lifted onto a paraboloid gives the Delaunay triangulation and the Voronoi diagram.
Frequently asked questions
Why do the algorithms start from the leftmost or the lowest point?
Because an extreme point is always a corner of the hull: nothing lies further to the left than the leftmost point (the lowest of them when several share it), so a line through it can have every other point on one side. Starting from a point known to be on the hull gives the algorithm a safe place to begin, and for Graham scan the lowest point puts every other point within half a turn of it, so sorting by angle has no wrap-around.
What does a zero cross product mean?
That the three points lie on one straight line. An algorithm then has to decide whether the middle point counts as a hull vertex. Most implementations, like this app, leave it out and keep only the corners, so the hull of points along the edges of a square is just its four corners. Some applications want every boundary point instead; then the rule flips, and the collinear points must be kept in order along each edge.
Is there a convex hull in three dimensions?
Yes: a convex polyhedron whose faces are triangles. Gift wrapping, QuickHull and the incremental algorithm all extend to 3D, with the orientation test becoming the sign of a 4 × 4 determinant (is a point above or below the plane through three others), and the best algorithms still run in O(n log n). The popular Qhull library computes hulls in any dimension with QuickHull.
How do I compute the area of the hull?
With the shoelace formula: list the vertices (x₁, y₁) … (xₕ, yₕ) in counterclockwise order, add up xᵢ·yᵢ₊₁ − xᵢ₊₁·yᵢ round the polygon (the last vertex pairs with the first) and halve the sum. Each term is the cross product of two position vectors, so the area comes from the same arithmetic as the orientation test. The app shows the area and the perimeter of the hull in units of a field 160 × 100 units in size.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Open the convex hull app