Joel Gibson

Convex Hull

A set of points is called convex if any two points in the set can be connected by a straight line that does not leave the set. For example, all regular polygons have convex interiors, but “bent” shapes or shapes with “holes” in them may not.

The convex hull of a set of points is the smallest convex set containing all of the points. In mathematical terms, the convex hull of any set S is the intersection of every convex set containing S. In the image below, if S is taken to be the set of points in the plane shown on the left, the convex hull of S is the filled set shown in the middle. Because we’re starting here from a finite set of points, the convex hull is always going to be a polygon whose vertices are a subset of our initial set S, so we also refer to the convex hull interchangably as just the boundary of that polygon, shown on the right.

Intuitively, the points are pegs sticking out of the plane, and the convex hull is the result of shrinking a giant rubber band around that set of points until it is tight.

Computing the convex hull

In a computational context, we’re given a list of n points in the plane as (x, y) pairs, and from this we need to calculate the convex hull: a polygon consisting of some of those points. We would also like to do it quickly, so we would prefer something better than O(n^2), or “for each point, examine every other point…”

By looking at the convex polygon above, we can quickly see some properties which will be true for all convex polygons:

These two observations are the core of the following algorithm for computing the convex hull:

This constructs the lower half of the hull. We can construct the upper half of the hull following the same idea, but going from right to left instead of from left to right. Then we join the two halves together (remembering to remove the duplicate end points).

The interactive example below uses the algorithm just described to compute the convex hull of any set of input points. While animating, large dots indicate dots which are part of the convex hull list being built, red indicates a right turn, and green indicates a left turn.

Click the visualisation to add more points.

The time complexity analysis for this algorithm is straightforward. Sorting points from left to right takes O(n \log n) time. Testing three points for whether or not they make a left or right turn takes constant time, and we do this a maximum of n times when building the bottom or halves of the hull. So the running time is dominated by the initial sort, and the time complexity of this algorithm is O(n \log n).

Further reading