On this page
Stretch a rubber band around ten nails in a board and let go. It snaps onto the outermost nails and skips the rest. That outline is the convex hull, and below you find one by hand.
What is the convex hull of a set of points?
The convex hull of a shape is the smallest convex set that contains it; for points in a plane it is the shape a stretched rubber band encloses (Convex hull, Wikipedia). If the points are not all on one line, the hull is a convex polygon whose corners are some of the input points (Convex hull algorithms, Wikipedia). Convex sets are also the subject of Helly's theorem on convex sets; here they become an algorithm.
How does the Graham scan algorithm work?
Ronald Graham published the method in 1972 (Graham 1972, DOI). The textbook version has three steps (Graham scan, Wikipedia):
Take the point with the lowest y (leftmost if tied). Call it P0; it is always on the hull.
Sort the other points by the angle they make with P0 and the x-axis.
Walk through them with a stack. Look at the top two stack points and the new one: a left turn means push; a right turn or straight line means pop the middle point and test again.
The turn test needs no angles. For P1, P2, P3, the value (x2-x1)(y3-y1) - (y2-y1)(x3-x1) is the z-component of the cross product of P1P2 and P1P3: positive is a left turn, negative a right turn, zero collinear (Graham scan, Wikipedia). It is the cross product from vector algebra formulas for Class 12.
Graham scan worked on 10 points
Take P0(2, 0), A(9, 1), B(6, 3), C(11, 6), D(8, 6), E(9, 10), F(5, 7), G(3, 11), H(1, 6), I(-1, 3). Sorted by angle from P0: A, C, B, D, E, F, G, H, I. C beats B because its slope from P0 is 6/9 against 3/4.
Stack: P0, A.
C: P0-A-C = 33, left, push. Stack: P0, A, C.
B: A-C-B = 19, left, push. Stack: P0, A, C, B.
D: C-B-D = -9, pop B; A-C-D = 15, push. Stack: P0, A, C, D.
E: C-D-E = -12, pop D; A-C-E = 18, push. Stack: P0, A, C, E.
F: C-E-F = 22, push. Stack: P0, A, C, E, F.
G: E-F-G = -22, pop F; C-E-G = 22, push. Stack: P0, A, C, E, G.
H: E-G-H = 32, push. Stack: P0, A, C, E, G, H.
I: G-H-I = -4, pop H; E-G-I = 52, push. Stack: P0, A, C, E, G, I.
The hull is P0, A, C, E, G, I, counter-clockwise; B, D, F and H are inside. Check step 4: (6-11)(6-6) - (3-6)(8-11) = 0 - 9 = -9. B survived step 3 and fell at step 4: the stack is a draft until the end.
Practice: add J(12, 12). It sorts between D and E, and E is popped when G arrives. Answer: P0, A, C, J, G, I.
Why does Graham scan run in O(n log n) time?
Finding P0 is O(n) and sorting is O(n log n); the loop looks quadratic, but each point is pushed once and popped at most once, so it is O(n) (Graham scan, Wikipedia). Our example pushed each of its 10 points once, popped 4 and made 12 turn tests. The sort dominates.
In general you cannot beat sorting. Put numbers x on the parabola y = x squared: walking the hull lists them in order, so a hull is as hard as sorting, which needs Omega(n log n) in the algebraic decision tree model (Convex hull algorithms, Wikipedia). Andrew Yao published a lower bound for finding convex hulls in 1981 (Yao 1981, DOI).
What is gift wrapping, and when is it better?
Gift wrapping, or the Jarvis march, was published by R. A. Jarvis in 1973 (Jarvis 1973, DOI). It starts at the leftmost point and keeps picking the next point so that all others lie on one side of the new edge, like winding string around nails; it takes O(nh) time, where h is the number of hull points (Gift wrapping algorithm, Wikipedia).
Many points, hull size unknown: Graham scan.
Very few corners (h smaller than log n): gift wrapping can be faster.
Every point on the hull (h = n): gift wrapping becomes quadratic, so use Graham scan.
Where is the convex hull used in collision detection?
The Gilbert-Johnson-Keerthi (GJK) algorithm, published in 1988 (GJK 1988, DOI), finds the minimum distance between two convex sets and is popular for real-time collision detection in video-game physics engines (GJK algorithm, Wikipedia). It needs convex shapes, and a hull supplies one: if two objects' hulls do not touch, the objects cannot touch either.
If the cross product feels like a trick, the Fermi Concept Library gives every concept in mathematics and computing its own lesson. For practice, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



