250 likes | 372 Views
Algorithms and Data Structures. Lecture 13. Agenda:. Plane Geometry: algorithms on polygons - Verification if point belongs to a polygon - Convex hull. Verification if point belongs to a polygon.
E N D
Algorithms and Data Structures Lecture 13
Agenda: • Plane Geometry: algorithms on polygons • - Verification if point belongs to a polygon • - Convex hull
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
Verification if point belongs to a polygon • 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
Verification if point belongs to a polygon • 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
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
Verification if point belongs to a polygon • If validation line contains end-point of some verge (pi-1, pi) it contains starting-point of verge (pi, pi+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 (pi-1, pi) and (pi, pi+1) in point pi
Verification if point belongs to a polygon • 2.Validation line is a tangent and touches verges (pi-1, pi) and (pi, pi+1) in point pi
Verification if point belongs to a polygon • If rotation directions of verges (pi-1, pi) and (pi, pi+1) are the same – validation line is a tangent and such intersection must not be taken into account • If rotation directions of verges (pi-1, pi) and (pi, pi+1) are distinct – validation line is a transversal and such intersection must be taken into account
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)
Plane geometry: Convex hull • 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 Θ(n3), where n is a number of points in Q; method’s ideas are the following: (a) for any two points pi pk build a line containing section (pi,pk); (b) if all the remained points of Q belong either to the left or to the right plane – section (pi,pk) is a verge of convex hull CH(Q), otherwise section (pi,pk) 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
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)
Plane geometry: Graham scan • Algorithm: • 1. Find point p0 that has minimal y coordinate; if a number of such points available – choose one with minimal x coordinate • 2. For each point p1…pm-1 calculate its angle relatively to the point p0 • 3. Sort points p1…pm-1 in ascending order of their angles; if there several points with the same angle – most far point is preserved in Q (relatively to p0), others are removed • 4. Points p0, p1 & p2 are added to the S • 5. For each point p3…pm-1 do … • 6. if curve (next_to_top->top->pi) turns in point “top” leftwards - add pi 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)
Plane geometry: Jarvis pass • Algorithm: • 1. Find point p0 that has minimal y coordinate; if a number of such points available – choose one with minimal x coordinate • 2. Start from point p0 (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 p0
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)