What is Computational Geometry? • Study of Data Structures and Algorithms for Geometric Problems • 1970’s: Need for Computational Geometry Recognized; Progress made on 1 and 2 • Today: “Mastered” 1 and 2, not so successful with 3 Problem s in Application Dom ains Specific Geom etrical Problems Algorithm s to Solve G eom etric Problems form ulate design m odify m ethods fororiginal problem s (implementation) 1 3 2
64
Embed
What is Computational Geometry? Study of Data Structures and Algorithms for Geometric Problems 1970 ’ s: Need for Computational Geometry Recognized; Progress.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
What is Computational Geometry?
• Study of Data Structures and Algorithms for Geometric Problems
• 1970’s: Need for Computational Geometry Recognized; Progress made on 1 and 2
• Today: “Mastered” 1 and 2, not so successful with 3
Problems inApplicationDomains
SpecificGeometrical
Problems
Algorithms toSolve Geometric
Problems
formulate design
modify methods for original problems(implementation)
1
3
2
Application Domains
• Computer Graphics and Virtual Reality– 2-D & 3-D: intersections, hidden surface elimination, ray tracing– Virtual Reality: collision detection (intersection)
– (e) p1p2, p3p4 do not intersect • (p3 – p1) x (p2 – p1) = 0
• (p4 – p1) x (p2 – p1) = 0
Two Segments Intersect - Algorithm
• Two_line_segments_intersect (p1p2, p3p4)
1. (x1’,y1’)=(min(x1,x2), min(y1,y2))
2. (x2’,y2’)=(max(x1,x2), max(y1,y2))
3. (x3’,y3’)=(min(x3,x4), min(y3,y4))
4. (x4’,y4’)=(max(x3,x4), max(y3,y4))
5. if not ((x3’ x2’) and (x1’ x4’) and (y3’ y2’) and (y1’ y4’)) then
6. return false // case (e) and more
7. else
8. if (p3–p1) x (p2–p1) or (p4–p1) x (p2–p1) are zero then
9. return true // case (c) and (d)
10. else if (p3–p1) x (p2–p1) and (p4–p1) x (p2–p1) have different sign then
11. return true // case (a)
12. else
13. return false // case (b)
Line Segment Intersection Problem• The Problem: Given n line segments, is any pair of them i
nsect?• Clearly, doing pairwise intersection testing takes O(n2) ti
me. By a sweeping technique, we can solve it in
O(n log n) time (without printing all the intersections).• In the worst case, there are (n2) intersections.• Simplifying assumptions
– No input segment is vertical.– No three input segments intersect at a single point.
Problem Definition:
Input : S={s1, s2, …, sn} of n segments in plane.
Output: set I of intersection points among segments in S. (with segments containing each intersection pt)
How many intersections possible?
In worst case, )(2
2nn
Problem Definition
Algorithm: Brute Force Intersect(S)
Test all pairs (si, sj)SXS, i not equal j.
If si intersects sj, report si intersects sj.
Complexity
-There may be much fewer intersections.
-Can we spend less time (depending on |I|) ?
Output sensitive:
-Running time depends on:
n: #of input segments
k: size of the output
)( 2n
Brute Force Intersect Algorithm
What would be the best time?
O(n+k)?
Actually a lower bound of (nlogn+k) is known.
Goal:
An output-sensitive algorithm that avoids testing pairs of segments for intersection if they are “far apart”.
Goal
Idea:
Project segments onto y-axis.
1. If si & sj intersect, then so do their y-intervals (e.g. s1 & s2 ).
2. If the y-intervals of si & sj don’t intersect, then neither do si & sj (e.g. s1 & s3 )
3. If the y-intervals of si & sj do intersect, then maybe si & sj intersect (e.g. s3 & s4 )
we may still test extra, but better than before.
y
x
S1
S2
S4
S3
Idea
Plane Sweep Algorithm:
L is horizontal sweep line initially above all segments.
Sweep L down over segments, and keep track of all segments intersecting it.
Status T of sweep line is the set of segments currently intersecting L .
Events are points where status changes.At each event point
Update status of sweep line: add/remove segments from TPerform intersection tests
Plane Sweep Algorithm
Ordering Segments• Two nonintersecting segments s1 and s2 are comparable at
x if the vertical sweep line with x‑coordinate x intersects both of them.
• s1 is above s2 at x, written s1 >x s2, if s1 and s2 are comparable at x and the intersection of s1 with the sweep line at x is higher than the intersection of s2 with the same sweep line.
Example
Moving the Sweep Line• Sweeping algorithms typically manage two sets of data:
1. The sweep‑line status gives the relationships among the objects intersected by the sweep line.
2. The event‑point schedule is a sequence of x‑coordinates, ordered from left to right, that defines the halting positions (event points) of the sweep line. Changes to the sweep‑line status occur only at event points.
• The event‑point schedule can be determined dynamically as the algorithm progresses or determined statically, based solely on simple properties of the input data.
Moving the Sweep Line• Sort the segment endpoints by increasing
x‑coordinate and proceed from left to right. • Insert a segment into the sweep‑line status when its left
endpoint is encountered, and delete it from the sweep‑line status when its right endpoint is encountered.
• Whenever two segments first become consecutive in the total order, we check whether they intersect.
Moving the Sweep Line• The sweep‑line status is a total order T. for which we
require the following operations:– INSERT(T, s): insert segment s into T.– DELETE(T, s): delete segment s from T.– ABOVE(T, s): return the segment immediately above segment
s in T.– BELOW(T, s): return the segment immediately below segment
s in T.
• If there are n segments in the input, we can perform each of the above operations in O(log n) time using red‑black trees.
• Replace the key comparisons by cross‑product comparisons that determine the relative ordering of two segments.
Segment‑intersection Pseudocode
Simple Closed Path• Problem: Finding a tour through aset of n given points. • Observation: Restricted TSP.• Algorithm:
1. Pick an "anchor" point. 2. Compute the angle made by drawing a line from eac
h of the points in the set to the anchor and then out in the positive horizontal direction.
3. Sort the points according to the angles computed in step 2.
Computing the Angle• angle = tan-1(dy/dx).• It make sense to use a function that is much easier to co
mpute but has the same ordering property as the arctangent.function theta(p1, p2: point): real;var dx, dy, ax, ay: integer; t: real;begin dx := p2.x - p1.x; ax := abs(dx); dy := p2.y - p1.y; ay := abs(dy); if ax+ay = 0 then t := 0 else t := dy/(ax+ay); if dx < 0 then t := 2 - t else if dy < 0 then t := 4 + t; theta := t*90.0; /* returns a no. between 0 and 360 */end;
Whether a Point is Inside
• Problem definition
– Given a simple polygon P and a point q, determine whether the point is inside or outside the polygon.
• The polygon can be a non-convex polygon
• Idea
– Draw a line from the point to a point outside of the polygon, count the number of intersections with the edges of the polygon.
– The point is inside the polygon iff the number of intersections is odd
Whether a Point is Inside (2)
• Point_in_polygon(P, q)
// P: polygon with vertices p1, p2, .., pn, edges e1, e2, .., en
// q: point
1. pick an arbitrary point s outside the polygon
2. count = 0
3. for all edges ei of the polygon do
4. if ei intersects the line segment qs then
5. count++
6. if count is odd then return true
7. else return false
• Complexity
– O(n) when n = size of the polygon
Whether a Point is Inside (3)
• Assume special cases don’t occur
– Case (a) and (b)
• Observation
– Is it indeed important to find an optimal line (i.e., the line segment qs) that minimizes the number of intersections?
– No. Finding the number of intersections is more important
• Idea: Can use the vertical line that pass the point q
q
s(a) should not count (b) should count
q
s
Whether a Point is Inside (4)
• Point_in_polygon_2(P, q)
// P: polygon with vertices p1, p2, .., pn, edges e1, e2, .., en
// q: point (x0, y0)
1. count = 0
2. for all edges ei of the polygon do
3. if ei intersects the line x=x0 then
4. yi = y-coordinate of the intersection
5. if yi < y0 then count++ // this can be yi > y0
6. if count is odd then return true
7. else return false
• Complexity
– O(n) when n = size of the polygon
Finding the Convex Hull• A convex hull of n given points is defined as the smallest
convex polygon containing them all
• Computing Convex hull is closed related to sorting. (Why?)
Idea
Method 1: Package Wrapping1. Find an anchor point (say, the one with the smallest y va
lue).2. Take a horizontal ray in the positive direction, and swee
p it upward until hitting another point, this point must be on the hull.
3. Anchor at the newly found point and continue sweeping until hitting another point.
4. Repeat step 3 until the "package" is fully "wrapped"
• Time complexity: If there are m vertices on the hull, then package wrapping technique requires about mn steps: (n-1) + (n-2) + ... + (n-m) = mn - m(m+1)/2.
Method 2: The Graham Scan1. Construct a simple closed path from the given n points.2. Select an extreme point, start a scan on all the points. If
an angle ABC is found to be reflex, Eliminate point B from the convex hull.
• Time complexity: O( n lg n). After the sort, the Graham scan is a linear-time process.
Method 3: Interior Elimination
• The interior elimination method is linear, on the average.