Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and Data Structures Lecture 13. Agenda: Plane Geometry: algorithms on polygons - Verification if point belongs to a polygon - Convex hull.

Similar presentations


Presentation on theme: "Algorithms and Data Structures Lecture 13. Agenda: Plane Geometry: algorithms on polygons - Verification if point belongs to a polygon - Convex hull."— Presentation transcript:

1 Algorithms and Data Structures Lecture 13

2 Agenda: Plane Geometry: algorithms on polygons - Verification if point belongs to a polygon - Convex hull

3 Verification if point belongs to a polygon Problem: given a polygon P defined by a set of vertices [1…m], given an arbitrary point p; does p belong to a polygon P? Problem can be easily solved for simple polygons like squares and rectangles We need some more general solution that will work for any kinds of polygons

4 Verification if point belongs to a polygon

5 The ideas behind the algorithm are the following: - Take arbitrary point x which is out of the considered polygon - Point x should be located far from the polygon - Points p and x are connected by a validation line section - Considering intersections of a validation section with verges of a polygon we can determine whether point p belongs to a polygon or does not

6 Verification if point belongs to a polygon

7 If number of intersections is even number or zero – point p lays out of the polygon If number of intersections is odd number – point p belongs to the polygon

8 Verification if point belongs to a polygon What happens if a validation line intersects verge in start or end point? In this case we do not know whether validation line is transversal or tangent Therefore we do not know if such points should be counted as intersections or should not

9 Verification if point belongs to a polygon

10 If validation line contains end-point of some verge (p i-1, p i ) it contains starting-point of verge (p i, p i+1 ) as well Let’s consider both the cases (where validation line is either a transversal or tangent) separately 1.Validation line is a transversal and intersects verges (p i-1, p i ) and (p i, p i+1 ) in point p i

11 Verification if point belongs to a polygon

12 2.Validation line is a tangent and touches verges (p i-1, p i ) and (p i, p i+1 ) in point p i

13 Verification if point belongs to a polygon If rotation directions of verges (p i-1, p i ) and (p i, p i+1 ) are the same – validation line is a tangent and such intersection must not be taken into account If rotation directions of verges (p i-1, p i ) and (p i, p i+1 ) are distinct – validation line is a transversal and such intersection must be taken into account

14 Plane geometry: Convex hull Let Q is a finite set of points on a plane Convex hull of Q is a minimal convex polygon that contains all the points of Q Convex hull of a set Q is denoted as CH(Q) Some points of a Q are inside the CH(Q), some belong to verges of CH(Q) and some are vertices of CH(Q) None of Q points can be located outside the CH(Q)

15 Plane geometry: Convex hull

16 Building a convex hull from a given set of points is a very popular task of plane geometry There are a number of algorithms that solve the problem; they have distinct estimates of running times E.g. so called power method is Θ(n 3 ), where n is a number of points in Q; method’s ideas are the following: (a) for any two points p i p k build a line containing section (p i,p k ); (b) if all the remained points of Q belong either to the left or to the right plane – section (p i,p k ) is a verge of convex hull CH(Q), otherwise section (p i,p k ) is not a verge of a CH(Q); (c) perform verification for all possible pairs of points We will consider two more efficient methods: Graham scan and Jarvis pass

17 Plane geometry: Graham scan Auxiliary structures: Q – set of points, S – stack of points INPUT: arbitrary set Q, |Q|=n, n>=3 OUTPUT: S – contains vertices of a convex hull There are two additional operations under the stack are defined: - top(S) – returns point from the top of the stack, but stack is not modified (point is not removed) - next_to_top(S) – returns point that is next to the topmost point of the stack; stack is not modified (point is not removed)

18 Plane geometry: Graham scan Algorithm: 1. Find point p 0 that has minimal y coordinate; if a number of such points available – choose one with minimal x coordinate 2. For each point p 1 …p m-1 calculate its angle relatively to the point p 0 3. Sort points p 1 …p m-1 in ascending order of their angles; if there several points with the same angle – most far point is preserved in Q (relatively to p 0 ), others are removed 4. Points p 0, p 1 & p 2 are added to the S 5. For each point p 3 …p m-1 do … 6. if curve (next_to_top->top->p i ) turns in point “top” leftwards - add p i to the S, otherwise continue removing topmost points from the S until curve changes its direction 7. if there are any points in Q continue for next point ( return to the step 5)

19 Plane geometry: Graham scan

20

21 Plane geometry: Jarvis pass Algorithm: 1. Find point p 0 that has minimal y coordinate; if a number of such points available – choose one with minimal x coordinate 2. Start from point p 0 (point is “current”) 3. For current point do … 4. Calculate angles of all points relatively to the “current” point 5. Choose point with smallest angle; if a number of points have the same angle – we choose most far from “current” point 6. Add new verge 7. Mark just found new point as “current” 8. Continue from step (3) until “current” point returns to point p 0

22 Plane geometry: Jarvis pass

23 Plane geometry: Graham scan

24 Plane geometry: Convex hull Graham scan is O(n*log(n)) Jarvis pass is O(n*h), n= |Q|, h – is a number of vertices in CH(Q)

25 Q & A


Download ppt "Algorithms and Data Structures Lecture 13. Agenda: Plane Geometry: algorithms on polygons - Verification if point belongs to a polygon - Convex hull."

Similar presentations


Ads by Google