Computational Geometry
2D Convex Hulls
Joseph S. B. Mitchell
Stony Brook University
Comparing O(n), O(n log n), O(n2)
n
n log n
n²
210 10³
10 • 210 104
220 106
20 • 220 2 • 107 240 1012
Interactive
Processing
220 106
n log n algorithms n² algorithms
n = 1000
yes
?
n = 1000000
?
no
2
Convexity
q
q
Set X is convex if p,qX pq X
convex non-convex
Point p X is an extreme point if there exists
a line (hyperplane) through p such that all
other points of X lie strictly to one side
Extreme points in red
r
q
p
p
p
Fact: If X=S is a finite set of points in 2D,
then CH(X) is a convex polygon whose vertices
(extreme points) are points of S.
3
Fundamental Problem: 2D
More generally:
Convex Hulls
CH(polygons)
Input: n points S = (p1, p2, …, pn)
p2
p8
p7
p4
p3
p5
Output: (9,6,4,2,7,8,5)
p1
p6
p9
Output: A boundary representation, e.g.,
ordered list of vertices (extreme points), of
the convex hull, CH(S), of S (convex polygon)
4
Equivalent Definitions of
Convex Hull, CH(X)
{all convex combinations of d+1 points of X }
[Caratheodory’s Thm] (in any dimension d)
T
T X , T convex
H
H X , H halfspace
Set-theoretic “smallest” convex set
containing X.
In 2D: min-area (or min-perimeter) enclosing
convex body containing X
In 2D: abc
a ,b , c X
5
2D Convex Hull Algorithms
O(n4) simple, brute force (but finite!)
O(n3) still simple, brute force
O(nh) simple, “output-sensitive”
•
h = output size (# vertices)
O(n log n) worst-case optimal (as fcn of n)
O(n log h) “ultimate” time bound (as fcn of n,h)
Simple, elegant
Randomized, expected O(n log n)
y= x2
Lower bound: (n log n)
(xi ,xi2 )
From SORTING:
Note: Even if the output of CH is not required to be an
ordered list of vertices (e.g., just the # of vertices),
(n log n) holds
6
x
Primitive Computation
• “Left” tests: sign of a cross product
(determinant), which determines the
orientation of 3 points
• Time O(1)
(“constant”)
c
Left( a, b, c ) = TRUE ab ac > 0
a
b
c is left of ab
7
SuperStupidCH: O(n4)
Fact: If s ∆pqr, then s is not a vertex
q
of CH(S)
p
s
p
•qp
O(n3)
r
r p,q
• s p,q,r: If s ∆pqr then mark
s as NON-vertex
O(n)
Output: Vertices of CH(S)
Can sort (O(n log n) ) to get ordered
8
StupidCH: O(n3)
Fact: If all points of S lie strictly to
one side of the line pq or lie in between
p and q, then pq is an edge of CH(S).
p
q
p
O(n2)
•qp
r
• r p,q: If r red then mark pq
as NON-edge (ccw)
O(n)
Output: Edges of CH(S)
Can sort (O(n log n) ) to get ordered
Caution!! Numerical errors require care to avoid crash/infinite loop!
Applet by
Snoeyink
9
O(nh) : Gift-Wrapping
Jarvis March
Idea: Use one edge to help find the
next edge.
r
q
O(n) per step
h steps
Total: O(nh)
p
Output: Vertices of CH(S)
Demo applet of Jarvis march
Key observation:
Output-sensitive!
10
O(n log n) : Graham Scan
Idea: Sorting helps!
Start with vlowest (min-y), a known vertex
O(n log n)
Sort S by angle about vlowest
Graham scan:
O(n)
O(n)
• Maintain a stack representing (left-turning) CH so far
Demo applet
If pi is left of last edge of stack, then PUSH
Else, POP stack until it is left, charging work to popped points
vlowest
CH so far
12
O(n log n) : Divide and Conquer
Time O(n)
Split S into Sleft and Sright , sizes n/2
Recursively compute CH(Sleft ), CH(Sright) Time 2T(n/2)
Merge the two hulls by finding upper/lower
bridges in O(n), by “wobbly stick”
Time O(n)
Sleft
Sright
Time:
T(n) 2T(n/2) + O(n)
T(n) = O(n log n)
13
Demo applet
QuickHull
Applet (programmed by Jeff So)
Applet (by Lambert)
14
QuickHull
QuickHull(a,b,S)
to compute upperhull(S)
• If S=, return ()
• Else return (QuickHull(a,c,A), c, QuickHull(c,b,B))
c
Discard points in abc
Works well in
higher
dimensions too!
Qhull website
B
A
Worst-case: O(n2 )
Avg: O(n)
c = point furthest from ab
b
a
S
15
Qhull, Brad Barber (used within MATLAB)
O(n log n) : Randomized Incremental
Add points in random order
Keep current Qi = CH(v1 ,v2 ,…,vi )
Add vi+1 :
• If vi+1 Qi , do nothing
• Else insert vi+1 by
finding tangencies,
updating the hull
Expected cost of insertion: O(log n)
16
Each uninserted vj Qi (j>i+1) points to the bucket
(cone) containing it; each cone points to the list of
uninserted vj Qi within it (with its conflict edge)
Add vi+1 Qi :
• Start from “conflict” edge e, and
walk cw/ccw to establish new
tangencies from vi+1 , charging walk
to deleted vertices
• Rebucket points in affected cones
(update lists for each bucket)
• Total work: O(n) + rebucketing work
• E(rebucket cost for vj at step i) =
O(1) P(rebucket) O(1) (2/i ) = O(1/i )
vj
E(total rebucket cost
for vj ) =
= O(log n)
Total expected work = O(n log n)
e
vi+1
Backwards Analysis:
vj was just rebucketed iff the
O(1/i ) last point inserted was one of
the 2 endpoints of the (current)
17
conflict edge, e, for vj
More Demos
Various 2D and 3D algorithms in an
applet
18
More CG Applets
Applets of algorithms in O’Rourke’s book
Applets from Jack Snoeyink
19
© Copyright 2025 Paperzz