Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computing Convex Hulls CLRS 33.3. Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics."— Presentation transcript:

1 Computing Convex Hulls CLRS 33.3

2 Computational Geometry Studies algorithms for solving geometric problems Applications in many fields –Computer graphics –Robotics –VLSI design –Computer-aided design, etc.

3 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

4 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 pq not convex

5 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.

6 Convex Hull

7

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

9 Problem Given P, compute a list of points that contains the vertices of CH(P), listed in clockwise (CW) order Expected output:

10 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

11 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) p q

12 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 }

13 How to test? if r lies to the left of directed line from p to q

14 Orientation test Given an ordered triple of points 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

15 Orientation test Given an ordered triple of points 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

16 Orientation test Given an ordered triple of points 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

17 Orientation

18 How about the last step? Given E, construct the list of vertices of CH(P) in CW order?

19 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?

20 Overall, BruteForce takes –How many ordered pairs? n (n-1) –For each we compare n-2 other points (r)

21 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

22 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

23 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

24 Construction of the Upper Hull already processed

25 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?

26 Construction of the Upper Hull Check whether the last two points on the current hull and make a right turn Left turn

27 Construction of the Upper Hull If a left turn, remove the last point from the current hull and try again Left turn

28 Construction of the Upper Hull If a left turn, remove the last point from the current hull and try again Left turn

29 Construction of the Upper Hull If a left turn, remove the last point from the current hull and try again Left turn

30 Construction of the Upper Hull If a right turn, add to the current hull right turn

31 Construction of the Upper Hull If a right turn, add to the current hull right turn

32 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()

33 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

34 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)


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

Similar presentations


Ads by Google