Algorithms · Visualized
Geometry: Convex Hull
Computational Geometry
Points, polygons, and hulls — geometry made computational.
The convex hull is the smallest convex polygon that encloses all given points — imagine stretching a rubber band around all the points and letting it snap tight. Time: O(n log n) average, O(n log n) worst; space: O(n) — the hull stack.
Big-O: Cross products and sweeping tell left/right and inside/outside.
Convex Hull (Graham Scan)
Computational GeometryThe convex hull is the smallest convex polygon that encloses all given points — imagine stretching a rubber band around all the points and letting it snap tight. Graham Scan finds it by sorting points around a fixed starting point by angle, then sweeping through them, always keeping the hull 'turning' in a consistent (counter-clockwise) direction and discarding points that would make it turn the wrong way.
How Convex Hull Works
- Find the point with the lowest y-coordinate (breaking ties with the lowest x-coordinate) — this is guaranteed to be on the hull, and becomes the starting/reference point.
- Sort all remaining points by the angle they make with the reference point, in counter-clockwise order.
- Process the sorted points one at a time, maintaining a stack representing the hull built so far.
- For each new point, check if the last two points on the stack plus this new point form a 'clockwise turn' (a right turn) — if so, the middle point isn't part of the hull, so pop it off the stack and check again.
- Once no more clockwise turns are found, push the new point onto the stack. After processing all points, the stack contains the vertices of the convex hull in order.
Convex Hull Worked Example
Input: [[0, 3], [1, 1], [2, 2], [4, 4], [0, 0], [1, 2], [3, 1], [3, 3]]
Starting point: (0,0) (lowest y). Sort remaining points by angle from (0,0). Sweep through, pushing points and popping any that create a clockwise turn — for instance, if (1,1) is followed by (2,2) then (3,1), the point (2,2) would be popped since it's not on the outer boundary. After the full sweep, the resulting hull vertices form the outer boundary enclosing all 8 points.
def convex_hull(points):
pts = sorted(points) # sort by x, then y
def cross(o, a, b):
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
lower = []
for p in pts:
while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0:
lower.pop()
lower.append(p)
upper = []
for p in reversed(pts):
while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0:
upper.pop()
upper.append(p)
return lower[:-1] + upper[:-1]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Convex Hull Time & Space Complexity
When to Use Convex Hull
- Collision boundary computation in graphics and robotics, GIS territory/boundary mapping, and pattern recognition (shape simplification).
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.