Skip to the workspace
Pebellslearn by building
Computational Geometry  ›  Lesson

Convex Hull

Convex Hull 32 minAndrew's monotone chain builds the smallest convex boundary around a set of points by sorting once and then walking a stack: a new point pops any point behind it that would make a non-left turn

dsa / Convex Hull

Explore each scene at your own pace.

01 · Wrapping a boundary around five stops

Wrapping a boundary around five stops

Convex Hull

Definition
The convex hull of a set of points is the smallest convex polygon that contains all of them; Andrew's monotone chain builds it by sorting the points and keeping a stack that drops any point that would make the boundary turn inwards.
In plain words
Hammer a nail at each stop, stretch a rubber band around all of them and let go. The band snaps tight around the outermost nails, and that outline is the hull.

In short: the convex hull of a set of points is the smallest convex polygon that contains every one of them — stretch a rubber band around the points and let go, and it snaps to the hull. Andrew's monotone chain builds it in O(n log n): sort the points once by (x, y), then build two chains — a lower hull scanning left to right and an upper hull scanning right to left. Each chain is a stack: a new point is pushed on, but first, while the top of the stack and the new point do NOT make a left turn, the top point is popped — it would create a dent, so it can't be on the hull. Every point gets pushed once and popped at most once, so the whole walk after sorting is O(n).

A zigzag path of five points sorted left to right, with a dashed X marking the one point that gets popped because the point after it would need a right turn to reach, and the remaining four points connected by a solid line labelled "lower hull"

Wrapping a boundary around five stops

Five of Meridian's stops sit at (0,0), (1,-1), (2,0), (3,-1) and (4,0) — already sorted by x. Building the lower hull:

watch it move · stack

A new point is pushed, but first the stack pops any point that would make a non-left turn with it — a point that would dent the boundary inward never belongs on the hull.

Nothing to compare yet, so (0,0) just goes on. Stack, 1 item. Top is (0,0).

1 / 9
step through it
1def orientation(a, b, c):
2 cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
3 if cross > 0:
4 return 1
5 if cross < 0:
6 return -1
7 return 0
8 
9def half_hull(points):
10 hull = []
11 for p in points:
12 while len(hull) >= 2 and orientation(hull[-2], hull[-1], p) <= 0:
13 hull.pop()
14 hull.append(p)
15 return hull
16 
17points = [(0, 0), (1, -1), (2, 0), (3, -1), (4, 0)]
18print(half_hull(points))

(2, 0) goes on the stack and comes right back off two points later — pushed once, popped once, never visited again. That's why the whole scan, after the one O(n log n) sort, costs O(n): the total number of pops across the entire run can never exceed the total number of pushes.

Scene 1 of 6
Project context and further resources
You're building a piece ofMeridian — service areas & coverage
This piece — convex_hull(): Wraps a depot's delivery stops in the smallest convex boundary that contains all of them.
Scenario A depot has its delivery stops plotted on the map, and Meridian needs the smallest boundary that contains every one of them — the depot's service area, drawn as a polygon instead of a vague radius.
convex_hull.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
convex_hull(points) → listThe convex hull's vertices, counter-clockwise, starting from the lexicographically smallest point.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.