Computational geometry Convex Hull

Computational Geometry
• Geometric problems (this course Euclidean plane).
• Does a set of line segments intersect, dividing plane into regions, find closest point,
motion planning (robotics), overlaying of maps, lightening of scenes/computing
shadows (computer graphics).
• This course:
Computational geometry
• Convex hull
Inge Li Gørtz
CLRS Chapter 33.0, 33.1, 33.3.
Convex Hull
• Convex hull. Given set of points Q, the convex hull CH(Q), is the smallest polygon
containing Q.
• Polygon. Region of plane bounded by a cycle of line segments (edges). Points
where edges meet are called the vertices of the polygon.
• Convex. For any two points p, q inside the polygon, the line segment pq
 ̄ is
completely inside the polygon. p
Convex Hull
q
Convex
p
q
Not convex
• Smallest. Any convex proper subset of the convex hull excludes at least one
point in Q.
• Example.
Convex Hull
Application of Convex Hull
• Convex hull. Given set of points Q, the convex hull CH(Q), is the smallest convex
polygon containing Q.
• Output from oil wells: mixture of several different components and proportions may vary
between different sources. Can be mixed to obtain specific mixture. Say only interested
in 2 of the components A and B. Want 12% A and 30% B. If we have 3 mixtures: • Polygon. Region of plane bounded by a cycle of line segments (edges). Points
where edges meet are called the vertices of the polygon.
• Convex. For any two points p, q inside the polygon, the line segment pq
 ̄ is
completely inside the polygon. p
p
q
• Mix M1 and M2 in ratio: 2:1.
• Cannot get 13% A and 22% B from M1 and M2.
• Mix M1, M2 and M3 in ratio 1:3:1.
• Represent mixtures by point in plane: p1=(0.1,0.35), p2=(0.16,0.2), p3 = (0.07, 0.15):
q
Convex
• M1 (10% A, 35%B) and M2 (16% A, 20% B) and M3 (7% A, 15% B). (0.1,0.35)
Not convex
Can make any combination inside triangle
• Smallest. Any convex proper subset of the convex hull excludes at least one
point in Q.
• Example.
p6
p5
(0.16,0.2)
p4
p1
(0.07, 0.15)
p2
p3
• Output. Vertices of convex hull in counterclockwise order: ⟨p1,p2,p3,p4,p5,p6⟩.
• n base mixtures: can make any combination in convex hull.
Convex Hull
Convex hull: Easy cases
• 3 equivalent definitions of convex hull: Given set of points Q, the convex hull CH(Q)
is
• |Q| = 1. Return Q.
• Def 1. The smallest convex polygon containing Q.
• Def 2. The largest convex polygon, whose vertices all are points in Q. • Def 3. The convex polygon containing Q and whose vertices all are points in Q.
• |Q| = 2. Return Q. • |Q| = 3. All 3 points are in CH(Q). Check if in counterclockwise order.
• Assume p0 is furthest to the left.
p2 = (x2,y2)
p0 = (x0,y0)
• Assumption (we will get rid of this later). No three points lie on a common line.
p1 = (x1,y1)
• Consider line segments  ̄ ̄
p0p1 and  ̄ ̄
p0p2.
p5
p6
Counterclockwise slope of  ̄ ̄
p0p1 is less than slope of  ̄ ̄
p0p2.
(y1-y0)/(x1-x0) < (y2-y0)/(x2-x0)
(y1-y0)(x2-x0) < (y2-y0)(x1-x0).
p4
p1
p2
p3
Counterclockwise (y1-y0)(x2-x0) < (y2-y0)(x1-x0)
Jarvis’s march
Graham’s scan
• Start with adding lowest point p0 to CH(Q).
• Graham’s scan. • Next point after p: • Pick lowest point p0 as starting point
• point appearing to be furthest to the right to someone standing at p and looking
at the other points (smallest if sorted in counterclockwise order). • If q is the point following p then for any other point r in Q p,q,r are in
counterclockwise order.
• Can find next vertex by performing n-1 counterclockwise tests.
• Time: • Sort remaining points in counterclockwise order around p0.
• Use linear time scan to build hull:
• Push p0, p1 and p2 onto the stack.
• Next point p:
• If adding p gives a left turn push p onto stack
• If adding p gives a right turn pop top element from stack and check again.
Continue checking until we get a left turn or only 2 vertices left on stack.
• Ɵ(1) for each counterclockwise test. • n tests for each vertex in the convex hull
p8
• Ɵ(nh)
r
• Output sensitive
p6
p4
p9
q
p7
p
p3
p5
p2
p1
p0
Graham’s scan
Left turns and right turns
• Graham’s scan
• Next point p: Let p’ and p’’ be the two top elements of the stack
• Pick lowest point p0 as starting point
• Check if adding p gives a left turn or a right turn.
• Sort remaining points in counterclockwise order around p0.
• Use linear time scan to build hull.
• If adding p gives a left turn then p’, p’’, p are in counterclockwise order:
• Push p0, p1 and p2 onto the stack.
• Next point p: Let p’ and p’’ be the two top elements of the stack
p
• If adding p gives a left turn push p onto stack
p’’
• If adding p gives a right turn pop top element from stack and check again.
Continue checking until we get a left turn or only 2 vertices left on stack.
p8
• If adding p gives a right turn then p’, p’’, p are in clockwise order:
p6
p4
p9
p7
p3
p5
p1
p0
p’
p’’
p2
p’
p
Graham’s scan
Graham’s scan
• Graham’s scan
• Graham’s scan
• Pick lowest point p0 as starting point
• Pick lowest point p0 as starting point
• Sort remaining points in counterclockwise order around p0.
• Sort remaining points in counterclockwise order around p0.
• Use linear time scan to build hull.
• Use linear time scan to build hull.
• Push p0, p1 and p2 onto the stack.
• Push p0, p1 and p2 onto the stack.
• Next point p: Let p’ and p’’ be the two top elements of the stack
• If p’, p’’, p are in counterclockwise order: push p onto stack
• If p’, p’’, p are in clockwise order: pop top element from stack and check
again. Continue until we get a left turn or only 2 vertices left on stack.
• Next point p: Let p’ and p’’ be the two top elements of the stack
• If p’, p’’, p are in counterclockwise order: push p onto stack
• If p’, p’’, p are in clockwise order: pop top element from stack and check again.
Continue until we get a left turn or only 3 vertices left on stack.
• Analysis. p8
• Sorting Ɵ(n log n)
• Counterclockwise check: Ɵ(1)
p6
p4
p9
• Each check is due to a push or a pop.
• Each point pushed once and popped at most once.
p7
p3
p5
p2
• n pops, O(n) pushes, O(n) counterclockwise checks. All constant time each.
• Time Ɵ(n) for scan.
p1
• Total time Ɵ(n log n)
p0
Chan’s algorithm (shattering)
Chan’s algorithm (shattering)
• Guess h.
• Guess h.
• Shatter the input into arbitrary n/h subsets.
• Shatter the input into arbitrary n/h subsets.
Chan’s algorithm (shattering)
Chan’s algorithm (shattering)
• Guess h.
• Guess h.
• Shatter the input into arbitrary n/h subsets.
• Shatter the input into arbitrary n/h subsets.
• Compute the convex hull of each subset using Graham’s scan.
• Time: O((n/h) h log h) = O(n log h).
• Compute the convex hull of each subset using Graham’s scan.
• Time: O((n/h) h log h) = O(n log h).
• Use idea from Jarvis’ march (wrapping) around the n/h subhulls.
• Successor can be found in O(h log h) time.
• Time for second part: O((n/h) h log h) = O(n log h).