34
Computing Convex Hulls CLRS 33.3

Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Embed Size (px)

Citation preview

Page 1: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Computing Convex HullsCLRS 33.3

Page 2: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Computational Geometry

• Studies algorithms for solving geometric problems• Applications in many fields

– Computer graphics

– Robotics

– VLSI design

– Computer-aided design, etc.

Page 3: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Geometric algorithms

• The input (typically): a description of a set of geometric objects– A set of points

– A set of line segments

– Vertices of a polygon

– Etc.

• We will look at sample problems in two dimensions, that is, in the plane

Page 4: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Convex hulls in two dimensions

• What is a convex hull?

• What is convexity?– A subset S of the plane is called convex if and only if for

any pair of points p, q in S, the line segment pq is completely contained in S

p

q

convex

p q

not convex

Page 5: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Convex Hull

• The convex hull CH(S) of a set s is the smallest convex set that contains S.

• Consider the problem of computing the convex hull of a finite set P of n points in the plane.

},...,,{ 21 npppP

Page 6: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Convex Hull

Page 7: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Convex Hull

Page 8: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Convex Hull

• Alternative Definition: It is the unique convex polygon whose vertices are points from P and contains all the points of P

Page 9: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Problem

• Given P, compute a list of points that contains the vertices of CH(P), listed in clockwise (CW) order

• Expected output:

10p

2p

1p6p

4p

3p

5p

7p

9p

8p

},,,,{ 781042 ppppp

Page 10: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Brute-Force Solution

• Consider an edge of the convex hull between vertices p and q

• Consider the directed line passing through p and q

• Observe that CH(P) lies to the right of this line

p

q

Page 11: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Brute-Force Solution

• Reverse holds too:• If all points in lie to the right of the directed

line through p and q, the pq is an edge of CH(P)

},{ qpP

p

q

Page 12: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

BruteForce_ConvexHull(P) {

E=empty set

for all ordered pairs (p,q) in PxP, and p!= q do {

isEdge = true

for all points r in P, r!=p and r!=q do

if r lies to the left of directed line from p to q

isEdge= false

if isEdge the add pq to E

}

from the set of edges in E, construct a list L of vertices of CH(P), in CW order

return L

}

Page 13: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

How to test?

if r lies to the left of directed line from p to q

Page 14: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Orientation test

• Given an ordered triple of points <p,q,r> in the plane, we say that they have – positive orientation if they define a CCW oriented triangle

– negative orientation if they define a CW oriented triangle

– Zero orientation if they are collinear

p

q

r

Positive orientation

p

q

r

r is to the left of pq

Page 15: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Orientation test

• Given an ordered triple of points <p,q,r> in the plane, we say that they have – positive orientation if they define a CCW oriented triangle

– negative orientation if they define a CW oriented triangle

– Zero orientation if they are collinear

p

q

r

negative orientation

p

q

r

r is to the right of pq

Page 16: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Orientation test

• Given an ordered triple of points <p,q,r> in the plane, we say that they have – positive orientation if they define a CCW oriented triangle

– negative orientation if they define a CW oriented triangle

– Zero orientation if they are collinear

zero orientation

p

q

r

r is on pq

p

q

r

Page 17: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Orientation

)

1

1

1

det ( ),,(

yx

yx

yx

rr

qq

pp

signrqpOrient

Page 18: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

How about the last step?

• Given E, construct the list of vertices of CH(P) in CW order?

Page 19: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

How about the last step?

• Given E, construct the list L of vertices of CH(P) in CW order?

• Remove an arbitrary edge pq from E, put p and q in L• Now find in E the edge with origin q (say qr), remove that edge from

E and add r to L• Keep doing this until E has only one edge left

• time to contsruct L – why?

)( 2nO

Page 20: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Overall,

• BruteForce takes

– How many ordered pairs? • n (n-1)

– For each we compare n-2 other points (r)

)( 3nO

Page 21: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Can we do better?

• A number of algorithms that can compute CH(P) in O(n log n)

• We will study the Incremental Algorithm– Points are added one at a time and the hull is updated at each

insertion

Page 22: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Incremental Algorithm

• At any intermediate step of the algorithm– There is a current hull of points – Add point– Update the hull

• Add points in order of increasing x-coordinate– To guarantee that the new point is outside the

current hull– If same x-coordinate, then in increasing order of y

11,..., ipp

ip

Page 23: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Upper Hull and Lower Hull

We will construct the hull in two pieces:

construct upper hull from left to right

construct lower hull from right to left

1p np

Page 24: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• already processed11,..., ipp

1p

4p7p

9p

12p

13p

ip

Page 25: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• Observation: for a convex polygon, if we walk through the boundary in CW order, we always make a right turn– i.e. if we consider any three consecutive points: p, q, r

Orient(p,q,r) should be negative

• How can we use this?

1p

4p7p

9p

12p

13p

ip

Page 26: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• Check whether the last two points on the current hull and

make a right turn

1p

4p 7p9p

12p

13p

ip

ip

Left turn

Page 27: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• If a left turn, remove the last point from the current hull and try again

1p

4p 7p9p

12p

13p

ipLeft turn

Page 28: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• If a left turn, remove the last point from the current hull and try again

1p

4p 7p9p

12p

ipLeft turn

Page 29: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• If a left turn, remove the last point from the current hull and try again

1p

4p 7p9p

ipLeft turn

Page 30: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• If a right turn, add to the current hull

right turn

1p

4p7p

ip

ip

Page 31: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Construction of the Upper Hull

• If a right turn, add to the current hull

right turn

1p

4p7p

ip

ip

Page 32: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

• Store current hull on a stack U– U.top() returns the last point on the current hull– U.second() returns the second to last point on the

current hull

– while ( Orient(U.second(), U.top() ) >= 0)

U.push( )

U.pop()

ip

Page 33: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

IncrementalConvexHull(P) {

sort points in increasing order of their x-coordinate

U.push(p[1])

U.push(p[2])

for i=3 to n do

while U.size() >= 2 and orient(U.second(), U.top(), p[i]) >= 0) do

U.pop()

U.push(p[i])

L.push(p[n])

L.push(p[n-1])

for i=n-2 to 1 do

while L.size() >= 2 and Orient(L.second(), L.top(), p[i]) >= 0) do

L.pop()

L.push(p[i])

Combine U and L into a single list of points in CW direction

Page 34: Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics

Incremental Convex Hull

• Running time?– Sort: O(n log n)

– Each point is removed from the stack U at most once: O(n)

– Each point is entered into the stack U once: O(n)

– Thus, O(n) for upper hull

– Similarly O(n) for lower hull

– Total: O(n log n)