Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric...

Preview:

Citation preview

Computing Convex HullsCLRS 33.3

Computational Geometry

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

– Computer graphics

– Robotics

– VLSI design

– Computer-aided design, etc.

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

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

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

Convex Hull

Convex Hull

Convex Hull

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

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

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

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

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

}

How to test?

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

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

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

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

Orientation

)

1

1

1

det ( ),,(

yx

yx

yx

rr

qq

pp

signrqpOrient

How about the last step?

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

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

Overall,

• BruteForce takes

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

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

)( 3nO

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

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

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

Construction of the Upper Hull

• already processed11,..., ipp

1p

4p7p

9p

12p

13p

ip

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

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

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

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

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

Construction of the Upper Hull

• If a right turn, add to the current hull

right turn

1p

4p7p

ip

ip

Construction of the Upper Hull

• If a right turn, add to the current hull

right turn

1p

4p7p

ip

ip

• 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

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

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)

Recommended