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 SS of nn points:

CH(S)={∑i=1nλipi  |  λi≥0,  ∑i=1nλi=1}\text{CH}(S) = \left\{ \sum_{i=1}^{n} \lambda_i p_i \;\middle|\; \lambda_i \geq 0, \; \sum_{i=1}^{n} \lambda_i = 1 \right\}

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 20 points arranged in a distribution.

Convex hull of the point set

050100020406080100xypointhull vertex

The shaded polygon is the hull of exactly these points, computed on this page: 8 of the 20 points are hull vertices.

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):

E[h]≈83(ln⁡n+γ−ln⁡2)≈0E[h] \approx \tfrac{8}{3}\left(\ln n + \gamma - \ln 2\right) \approx {\cmv{computed}{expectedHullRandom}}

where γ≈0.5772\gamma \approx 0.5772 is Euler's constant; the approximation is close from about 10 points on. This sample has h=0h = {\cmv{computed}{hullVertices}}, leaving 12 interior points. Draw another sample (1) and hh scatters around the expected value. The square's corners matter: for points spread uniformly over a disk, E[h]E[h] grows faster, like 3.38 n1/33.38\,n^{1/3}.

Drag 20 from 3 to 50 and watch the hull change. Then switch the distribution (): the circle puts every point on the hull, while random points leave only a handful of vertices.

The hull has perimeter 230.9 and area 3,688, in the units of the plot. 40% of the points are vertices, and 12 are strictly inside, invisible to the rubber band.

The Orientation Test

Every convex hull algorithm relies on the same O(1)O(1) geometric primitive: the cross product orientation test. For three points P1,P2,P3P_1, P_2, P_3:

cross(P1,P2,P3)=(x2−x1)(y3−y1)−(y2−y1)(x3−x1)\text{cross}(P_1, P_2, P_3) = (x_2 - x_1)(y_3 - y_1) - (y_2 - y_1)(x_3 - x_1)

Positive means counterclockwise (left turn), zero means collinear, negative means clockwise (right turn). For P1=(0,0)P_1 = (0,0), P2=(10,5)P_2 = (10,5), P3=(5,20)P_3 = (5,20):

cross=(10)(20)−(5)(5)=200−25=0\text{cross} = (10)(20) - (5)(5) = 200 - 25 = {\cmv{computed}{crossProductExample}}

Since 175 >0> 0, the sequence makes a left turn -- confirming P3P_3 is to the left of the line P1P2P_1 P_2.

Graham Scan

The Graham scan (1972) builds the hull in O(nlog⁡n)O(n \log n). Set the algorithm view to for more detail.

  1. Anchor: Find the lowest point P0P_0 (guaranteed on the hull)
  2. Sort: Order all other points by polar angle θi=atan2(yi−y0,xi−x0)\theta_i = \text{atan2}(y_i - y_0, x_i - x_0), nearer points first when angles tie -- this O(nlog⁡n)O(n \log n) sort is the bottleneck
  3. 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 8 points the stack holds 4 points, and processing the latest point popped 0 from it.

Graham scan, step by step

P₀point 8050100020406080100xynot scanned yetscannedscan order (rays from P₀)stack after 8 points

The dotted rays from P0P_0 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 P0P_0, is the hull above.

Algorithm Race

How do the algorithms compare on your 20 points with distribution?

Operations against n (log scale)

gift wrapping here: n·h, h = 81020304050024number of points nlog₁₀(operations)brute force, n³gift wrapping, worst case (h = n)gift wrapping, uniform points (expected h)Graham scan, n log₂ n
  • Brute force O(n3)O(n^3): ~8,000 operations (every ordered pair of points is a candidate edge, checked against every point)
  • Graham scan O(nlog⁡n)O(n \log n): ~86 operations
  • Gift wrapping O(nh)O(nh): ~160 operations (hh wrapping steps, each checking all nn 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 h=0h = {\cmv{computed}{hullVertices}} vertices among n=0n = {\cmv{computed}{numPoints}} points. Since h≥log⁡2nh \geq \log_2 n, Graham scan does less work. Gift wrapping degrades toward O(n2)O(n^2) as hh approaches nn.

Gift Wrapping (Jarvis March) walks the hull boundary by always choosing the point with the smallest counterclockwise angle -- intuitive but O(nh)O(nh). Quickhull partitions points recursively like Quicksort, achieving O(nlog⁡n)O(n \log n) expected time. Chan's algorithm combines the best of both for the optimal O(nlog⁡h)O(n \log h).

Switch to circle distribution and drag 20 up -- with h=nh = n, gift wrapping sits on the n2n^2 curve while Graham scan holds steady. On random points hh grows like ln⁡n\ln n, so gift wrapping stays within a small factor of Graham scan; on the grid, with only four or five corners, it wins.

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 QQ is interior, check that it lies on the counterclockwise side of every directed hull edge -- the "half-plane intersection" method, running in O(h)O(h) or O(log⁡h)O(\log h) with binary search.

Try adjusting 20 points in distribution: right now there are 8 vertices, 0 boundary points and 12 interior points. Only the grid has boundary points that are not vertices; circle distributions leave zero points interior.

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

You might also like