740 likes | 961 Views
Computational Geometry. What is Computational Geometry?. Deals with problems involving geometric primitives: points lines polygons. Geometric Primitives. Points a geometric element determined by an ordered set of coordinates 2D: (x,y) 3D: (x,y,z) 4D: (x,y,z,w) …. B. A.
E N D
What is Computational Geometry? • Deals with problems involving geometric primitives: • points • lines • polygons
Geometric Primitives • Points • a geometric element determined by an ordered set of coordinates • 2D: (x,y) • 3D: (x,y,z) • 4D: (x,y,z,w) • …
B A Geometric Primitives • Line • a set of points satisfying equation • P = t A + (1-t) B Parametric equation • in 2D • y = m x + c Analytic equation • Line segment • closed subset of a line contained between two points a and b which are called end points • P = t A + (1-t) B where 0 t 1 Parametric equation
Geometric Primitives • Polygons • circular sequence (cycle) of points (vertices)and segments (edges)
Segment intersection:Given two segments, do they intersect Simple closed path:given a set of points, find a non-intersecting polygon with vertices on the points. Inclusion in polygon:Is apointinside or outside apolygon ? Some Geometric Problems
Segment Intersection • Test whether segments (a,b) and (c,d) intersect. How do we do it? a d c b • We could start by writing down the equations of the lines through the segments, then test whether the lines intersect, then ... • An alternative (and simpler) approach is based in the notion of orientation of an ordered triplet of points in the plane
2 1 • Counterclockwise 2 • Colinear 1 2 1 Orientation • Clockwise if slope2 > slope1 then counterclockwise else if slope2 < slope1 clockwise else colinear
q q 1 1 q q 2 2 p p 2 2 p p 1 1 Intersection and Orientation (p1,q1,p2), (p1,q1,q2), (p2,q2,p1), (p2,q2,q1) (p1,q1,p2), (p1,q1,q2), (p2,q2,p1), (p2,q2,q1)
q 1 q 2 p 2 (p1,q1,p2), (p1,q1,q2), (p2,q2,p1), (p2,q2,q1) p 1 Intersection and Orientation
q 1 q 2 p 2 p 1 Intersection and Orientation • Two segments (p1,q1) and (p2,q2) intersect iff • general case: (p1,q1,p2) and (p1,q1,q2) have different orientations and (p2,q2,p1) and (p2,q2,q1) have different orientations • special case: (p1,q1,p2), (p1,q1,q2), (p2,q2,p1), and (p2,q2,q1) are all collinear and the x-projections of (p1,q1) and (p2,q2) intersect, the y-projections of (p1,q1) and (p2,q2) intersect
q 1 p 1 Intersection and Orientation(special cases) q 2 p 2 q 2 q 1 p 2 (p1,q1,p2), (p1,q1,q2), (p2,q2,p1), (p2,q2,q1) p (p1,q1,p2), (p1,q1,q2), (p2,q2,p1), (p2,q2,q1) 1
y3-y2 x3-x2 y2-y1 x2-x1 How to Compute the Orientation? p3 • slope of segment (p1,p2): = (y2-y1) / (x2-x1) • slope of segment (p2,p3): = (y3-y2) / (x3-x2) p2 • Orientation test • counterclockwise (left turn): < • clockwise (right turn):> • collinear : = p1 • The orientation depends on whether the expression (y3-y2)(x2-x1) -(y2-y1)(x3-x2) is positive, negative, or zero.
Point Inclusion • given a polygon and a point • is the point inside ? or • outside the polygon?
a b c e f Point Inclusion • Draw a horizontal line to the right of each point and extend it to infinity • Count the number of times a line intersects the polygon. -even number point is outside -odd number point is inside
Point Inclusion • Draw a horizontal line to the right of each point and extend it to infinity • Count the number of times a line intersects the polygon. -even number point is outside -odd number point is inside a b c d e f g What about points d and g ?? Special Case !
Segment-Segment Intersection Problem Definition: Let n: #of input segments in a plane Find all intersection points among segments. How many intersections k, possible?
Brute Force Intersect Algorithm Algorithm: Brute Force Intersect(S) Test all pairs (si, sj), i j. If si intersects sj, report si intersects sj. Complexity -Independent of the number of intersections.
Goal Spend time depending on the # of intersection ? • Idea: • Avoid testing all pairs. • Do not Intersect if they are “far apart”.
a2 b1 b b2 a a1 Sweep-line Sweep line ab Status Event Points a b a a b a a1 b1 b2 a2 b1 b2 a2 ab b2 a2 b2 a2 a2
Plane Sweeping Algorithm • Sweep the L over the plane stopping at events. • Three types of events • The left end point is reached • The right end point is reached • An intersection point is reached
Plane Sweeping Algorithm • Sweep the L over the plane stopping at events. • Three types of events • The left end point is reached Insert segment • The right end point is reached Remove segment • An intersection point is reached Switch adjacent segments
b d c e a Plane Sweeping Algorithm
Plane Sweep Algorithm • Analysis • Total # of events = 2 n + k • Event is inserted once and deleted once from event queue Q • Cost of maintaining Q = O((n+k) log(n+k)) • k is at most n2. • So cost of maintaining Q = O((n+k) log n) • Cost of maintaining segment list is O(n log n)
Plane Sweep Algorithm • Analysis (contd.) • Total cost of maintaining Q = O((n+k) log n) • Cost of maintaining segment list: • n segments inserted and deleted at O(log n) cost each. • At every event there are at most 2 new segment adjacencies. • Total intersection calls: O(n+k). • Thus total cost of maintaining segment list: O(n log n) • Total overall cost: O((n+k) log n)
Simple Closed Path Problem:Given a set of points ... “Connect the dots” without crossings
a Simple Closed Path 1. Pick the bottom most point as the anchor point 2. For each point p, compute the angle(p) of the segment (a,p) with respect to the x-axis:
Simple Closed Path • Traverse the points by increasing angle a
Simple Closed Path How do we compute angles? • Option 1: use trigonometry (e.g., tan-1). • the computation is inefficient since trigonometric functions are not in the normal instruction set of a computer and need a call to a math-library routine. a
Simple Closed Path How do we compute angles? • Observation: • we don’t care about the actual values of the angles. We just want to sort by angle. • Option 2 : use orientation to compare angles without actually computing them!! a
p q a Simple Closed Path • Orientation to sort by angles ? • angle(p) < angle(q) orientation of (a,p,q) is counterclockwise • We obtain an O(n log n)-time algorithm for the simple closed path problem on n points
Convex Polygon • A convex polygon is a nonintersecting polygon whose internal angles are all convex (i.e., less than 180º) • In a convex polygon, a segment joining two vertices of the polygon lies entirely inside the polygon convex nonconvex
Convex Hull • Definition: The convex hull of a set of points is the smallest convex polygon containing the points • Think of a rubber band snapping around the points
Special Cases • The convex hull is a segment • All the points are collinear • The convex hull is a point • All the points are coincident
Applications • Motion planning • Find an optimal route that avoids obstacles for a robot • Geometric algorithms • Convex hull is like a two-dimensional sorting obstacle start end
The Package Wrapping Algorithm Idea: Think of wrapping a package. Put the paper in contact with the package and continue to wrap around from one surface to the next until you get all the way around.
q p Package Wrap • Question: Given the current point, how do we compute the next point to be wrapped? • Set up an orientation tournament using the current point as the anchor-point. • The next point is selected as the point that beats all other points at CCWorientation, i.e., for any other point, we have orientation(c, p, q) = CCW c
Time Complexity of Package Wrap • For every point on the hull we examine all the other points to determine the next point • Notation: N:number of points M: number of hull points (MN) • Time complexity: (MN) • Worst case: (N2) (All the points are on the hull, and thusM = N) • Average case: (N log N ) — (N4/3) for points randomly distributed inside a square, M = (log N) on average for points randomly distributed inside a circle, M = (N1/3) on average
Computing the Convex Hull • The following method computes the convex hull of a set of points Phase 1: Find the lowest point (anchor point) Phase 2: Form a nonintersecting polygon by sorting the points counterclockwise around the anchor point Phase 3: While the polygon has a nonconvex vertex, remove it
Removing Nonconvex Vertices • Testing whether a vertex is convex can be done using the orientation function • Let p, q and r be three consecutive vertices of a polygon, in counterclockwise order • qconvexorientation(p, q, r) = CCW • qnonconvex orientation(p, q, r) = CW or COLL r r q q p p
The Graham scan is a systematic procedure for removing nonconvex vertices from a polygon The polygon is traversed counterclockwise and a sequence H of vertices is maintained for each vertex r of the polygon Let q and p be the last and second last vertex of H whileorientation(p, q, r) = CW or COLLremove q from Hq pp vertex preceding p in H Add r to the end of H r r p r q p q p q H H H Graham Scan
Analysis • Computing the convex hull of a set of points takes O(n log n) time • Finding the anchor point takes O(n) time • Sorting the points counterclockwise around the anchor point takes O(n log n) time • Use the orientation comparator and any sorting algorithm that runs in O(n log n) time (e.g., heap-sort or merge-sort) • The Graham scan takes O(n) time • Each point is inserted once in sequence H • Each vertex is removed at most once from sequence H
H B M F G D I E C Quick Hull Example J O K P A L N [A B C D E F G H I J K L M N O P]
H B M F G D I E C Quick Hull Example J O K P A L N [A B C D E F G H I J K L M N O P]
H B M F G D I E C Quick Hull Example J O K P A L N [ A B D F G H J K M O P]
H B M F G D I E C Quick Hull Example - Maximum J O K P A L N [A B D F G H J K M O P]
H B M F G D I E C Quick Hull Example - Filter J O K P A L N [[A B F J ] [J O P]]