a tutorial on computational geometry
DESCRIPTION
My talk about computational geometry in NTU's APEX Club in NTU, Singapore in 2007. The club is for people who are keen on participating in ACM International Collegiate Programming Contests organized by IBM annually.TRANSCRIPT
A Tutorial on Computational Geometry
Pham Minh TriPh.D. Candidate and Project OfficerSchool of Computer Engineering
1 Mar 2008
presented by
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
Point Inside Polygon Test
• Given a point, determine
if it lies inside a polygon
or not
Ray Test
• Fire ray from point• Count intersections
– Odd = inside polygon– Even = outside polygon
Problems With Rays
• Fire ray from point• Count intersections
– Odd = inside polygon– Even = outside polygon
• Problems– Ray through vertex
Problems With Rays
• Fire ray from point• Count intersections
– Odd = inside polygon– Even = outside polygon
• Problems– Ray through vertex
Problems With Rays
• Fire ray from point• Count intersections
– Odd = inside polygon– Even = outside polygon
• Problems– Ray through vertex– Ray parallel to edge
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
• One winding = inside
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
A Better Way
• zero winding = outside
Requirements
• Oriented edges• Edges can be processed
in any order
Computing Winding Number
• Given unit normal n• =0
• For each edge (p1, p2)
• for some integer k• k = winding number
– If k == 1, then inside– If k == 0, then outside
1p
2px
2k
xpxp
xpxp
xpxp
xpxpn
21
211
21
21 )()(cos
)()(
))()((
Advantages
• Extends to 3D!• Numerically stable • Even works on models with holes:
– Odd k: inside– Even k: outside
• No ray casting
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
Convex Hulls
Subset of S of the plane is convex, if for all pairs p,q in S the line segment pq is completely contained in S.
The Convex Hull CH(S) is the smallest convex set, which contains S.
p
q
pq
p
q
pq
Convex hull of a set of points in the plane
Rubber band experiment
The convex hull of a set P of points is the unique convex polygon whose vertices are points of P and which containsall points from P.
•
• •
•
• •
•
•
•
Convex Hull
• Input:– Set S = {s1, …, sn} of n points
• Output:– Find its convex hull
• Many algorithms:– Naïve – O(n3)– Insertion – O(n logn)– Divide and Conquer – O(n logn)– Gift Wrapping – O(nh), h = no of points on the hull– Graham Scan – O(n logn)
Graham Scan
• Polar sort the points around a point inside the hull
• Scan points in counter-clockwise (CCW) order– Discard any point that causes a
clockwise (CW) turn• If CCW, advance• If !CCW, discard current point
and back up
Graham-Scan : (1/11)
p1
p0
p3p4
p2
p5p6p7
p8p9
p10
p11
p12
Graham-Scan :(1/11)
p1
p0
p3p4
p2
p5p6p7
p8p9
p10
p11
p12
1.Calculate polar angle
2.Sorted by polar angle
Graham-Scan : (2/11)
p2p1p0
Stack S:
p1
p0
p3p4
p2
p5p6p7
p8p9
p10
p11
p12
Graham-Scan : (3/11)
Stack S:
p1
p0
p3p4
p2
p5p6p7
p8p9
p10
p11
p12
p3p1p0
Graham-Scan : (4/11)
p1
p0
p3p4
p2
p5p6p7
p8p9
p10
p11
p12
p4p3p1p0
Stack S:
Graham-Scan (5/11)
p1
p0
p3p4p2
p5p6p7
p8p9
p10
p11
p12
p5p3p1p0
Stack S:
Graham-Scan (6/11)
p1
p0
p3p4p2
p5p6p7
p8p9
p10
p11
p12
p8p7p6p5p3p1p0
Stack S:
Graham-Scan (7/11)
p1
p0
p3p4p2
p5p6
p7p8
p9
p10
p11
p12
p9p6p5p3p1p0
Stack S:
Graham-Scan (8/11)
p1
p0
p3p4p2
p5p6p7
p8
p9
p10
p11
p12
p10p3p1p0
Stack S:
Graham-Scan (9/11)
p1
p0
p3p4p2
p5p6p7
p8
p9
p10
p11
p12
p11p10p3p1p0
Stack S:
Graham-Scan (10/11)
p1
p0
p3p4p2
p5p6p7
p8
p9
p10
p11p12
p12p10p3p1p0
Stack S:
Time complexity Analysis
• Graham-Scan– Sorting in step 2 needs O(n log n).– Time complexity of stack operation is O(2n)– The overall time complexity in Graham-Scan is O(n log n).
• Demo:– http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
Line segment intersection
• Input:– Set S = {s1, …, sn} of n line segments, si = (xi, yi)
• Output:– k = All intersection points among the segments in S
Line segment intersection
• Worst case:– k = n(n –1)/2 = O(n2) intersections
• Sweep line algorithm (near optimal algorithm):– O(n log n + k) time and O(n) space– O(n) space
Sweep Line Algorithm
Avoid testing pairs of segments that are far apart.
Idea: imagine a vertical sweep line passes through the given set of line segments, from left to right.
Sweepline
Assumption on Non-degeneracy
No segment is vertical. // the sweep line always hits a segment at // a point.
If a segment is vertical, imagine we rotate it clockwise by a tiny angle.This means:
For each vertical segment, we will consider its lower endpoint before upper point.
Sweep Line Status
The set of segments intersecting the sweep line.
It changes as the sweep line moves, but not continuously.
Updates of status happen only at event points. left endpointsright endpointsintersections
event points
AG
C
T
Ordering Segments
A total order over the segments that intersect the current position of the sweep line: •Based on which parts of the segments we are currently interested in
A
B
C
D
E
B > C > D(A and E not inthe ordering)
C > D(B drops out ofthe ordering)
At an event point, the sequence of segments changes:
Update the status.
Detect the intersections.
D > C(C and D swaptheir positions)
Status Update (1)
A new segment L intersecting the sweep line
L
M
K
Check if L intersects with the segment above (K) and the segment below (M).
new event point
Intersection(s) are new event points.
Event point is the left endpoint of a segment.
N
K, M, N K, L, M, N
O
Status Update (2)
The two intersecting segments (L and M) change order.
L
M
K
Check intersection with new neighbors (M with O and L with N).
Intersection(s) are new event points.
Event point is an intersection.
N
O
O, L, M, N O, M, L, N
Status Update (3)
The two neighbors (O and L) become adjacent.
L
M
K
Check if they (O and L) intersect.
Intersection is new event point.
Event point is a lower endpoint of a segment.
N
O, M, L, N O, L, N
O
Data Structure for Event Queue
Data structure: balanced binary search tree (e.g., red-black tree).
Ordering of event points:
by x-coordinates
by y-coordinates in case of a tie in x-coordinates.
Supports the following operations on a segment s.
inserting an event
fetching the next event
Every event point p is stored with all segments starting at p.
// O(log m)
// O(log m)
m = #event points in the queue
Data Structure for Sweep-line Status
Describes the relationships among the segments intersected by the sweep line.
Use a balanced binary search tree T to support the following operations on a segment s.
Insert(T, s) Delete(T, s)Above(T, s) // segment immediately above s Below(T, s) // segment immediately below s
e.g, Red-black trees, splay trees (key comparisons replaced by cross-product comparisons).
O(log n) for each operation.
An Example
L
K
M
N
O
K
L O
N M
K
L
N
O
The bottom-up order of the segments correspond to the left-to-right order of the leaves in the tree T.
Each internal node stores the segment from the rightmost leaf in its left subtree.
The Algorithm
FindIntersections(S)Input: a set S of line segmentsOuput: all intersection points and for each intersection the segment containing it.1. Q // initialize an empty event queue2. Insert the segment endpoints into Q // store with every left endpoint // the corresponding segments3. T // initialize an empty status structure4. while Q 5. do extract the next event point p6. Q Q – {p}7. HandleEventPoint(p)
Handling Event Points
Status updates (1) – (3) presented earlier.
Degeneracy: several segments are involved in one event point (tricky).
A
B
C
C
A
D
G
E
H
l
D
D
AG E
HC
A
C
G
ET:
G C
HA
B
A
G
C
B
(a) Delete D, E, A, C (b) Insert B, A, C
p
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
Motivation
Transformation of a topographic map
into a perspective view
TerrainsGiven: A number of sample points p1..., pn
Required: A triangulation T of the points resulting in a “realistic” terrain.
"Flipping" of an edge:900
930
2050
900
930
20
50
Goal: Maximise the minimum angle in the triangulation
Triangulation of Planar Point Sets
Given: Set P of n points in the plane (not all collinear).
A triangulation T(P) of P is a planar subdivision of the convex hull of P into triangles with vertices from P.
T(P) is a maximal planar subdivision.
For a given point set there are only finitely many differenttriangulations.
Size of TriangulationsTheorem : Let P be a set of n points in the plane, not all collinear and let k denote the number of points in P that lie on the boundary of the convex hull for P. Then any trianglation of P has 2n-2-k triangles and 3n-3-k edges.
Proof :
Let T be triangulation of P, and let m denote the # of triangles of T. Each triangle has 3 edges, and the unbounded face has k edges. nf = # of faces of triangulation = m + 1every edge is incident to exactly 2 faces. Hence, # of edges ne = (3m +k)/2. Euler‘s formula : n - ne + nf = 2.Substituting values of ne and nf , we obtain:
m = 2n – 2 – k and ne = 3n – 3 – k .
Angle Vector
Let T(P) be a triangulation of P ( set of n points). Suppose T(P) has m triangles. Consider the 3m angles of triangles of T(P), sorted by increasing value.A(T) = { a1..., a3m } is called angle-vector of T.
Triangulations can be sorted in lexicographical order according to A(T).
A triangulation T(P) is called angle-optimal if A(T(P)) A(T´(P)) for all triangulations T´ of P.
Illegal Edge
a1
a2
a6a5
a3
a4 Pj
Pi a1‘
a2‘ a4‘
a3‘ a5‘
a6‘
Pk
Pj Pi
Edge flip
The edge pi pj is illegal if 6i1
min 6i1
min
‘i
Note: Let T be a triangulation with an illegal edge e. Let T´ be the triangulation obtained from T by flipping e. Then, A(T´) A(T) .
i
Consider a quadrilateral:
Legal Triangulation
P i
Pk
P j
P l
Definition : A triangulation T(P) is called a legal triangulation, if T(P) does not contain any illegal edges.
Test for illegality
Lemma :
Let edge pi pj be incident to triangles pi pj pk
and pi pj pl , and let C be the circle thru pi, pj and pk. The edge pi pj is illegal iff the point pl
lies in the interior of C. Furthermore, if thepoints pi, pj, pk, pl form a convex quadri-lateral and do not lie on a common circle, then exactly one of pi pj orpk pl is an illegal edge.
Test of IllegalityObservation:
pl lies inside the circle through pi, pj and pk iff pk lies inside thecircle through pi, pj, pl . When all four points lie on circle, bothpi pj and pk pl are legal.
P i
Pk
P j
P l
P i
Pk
P j
P l
Thales Theorem
a
b
p
q
r
s
pipj
pk
pl
asb aqb = apb arb
illegal
Lemma: Let C be the circle through the triangle pi, pj, pk and let the point pl be the fourth point of a quadrilateral.The edge pi pj is illegal iff pl lies in the interior of C.
Thales Theorem
pi pj
pl
pk
pi pj
pl
pk
Consider the quadrilateral with pl in the interior of the circle that goes through pi, pj, pk.
Claim: The minimum angle does not occur at pk!
(likewise: Minimum angle does not occur at pl )
Goal: Show that pi pj is illegal
pi pj
pk
pl
a1a2
a3
a4
pi pj
pk
pl
a1a2
a3
a4
a1‘a2‘
a3‘ a4‘
pi pj
pk
pl
a1‘a2‘
a3‘ a4‘
W.l.o.g.
a4 minimal
Thales Theorem: Proof
a1‘ > a4
a2‘ > a2
pi pj
pk
pl
a1a2
a3
a4
a1‘a2‘
a3‘ a4‘
Circle criterion violated illegal edge
Thales Theorem: Proof
a4‘ > a1
a3‘ > a3
Hence, min{ai‘} > min{ai}
Circle Criterion
Definition:A triangulation fulfills the circle criterion if and only if the circumcircle of each triangle of the triangulation does not contain any other point in its interior.
Theorems
Theorem:A triangulation T(P) of a set P of points does not contain an illegal edge if and only if nowhere the circle criterion is violated.
Theorem:Every triangulation T(P) of a set P of points can be finally transformed into an angle-optimal triangulation in a finite number of steps.
Definition of the Delaunay Triangulation
A triangulation T(P) is a Delaunay Triangulation of P, denoted as DT(P), if and only if the circumcircle of any triangle of T does not contain any other point of P in its interior (i.e. T fulfills the circle criterion).
Equivalent Characterisations of the Delaunay Triangulation
1. DT(P) is the straight-line-dual of Voronoi Diagram VD(P).
2. DT(P) is a triangulation of P such that all edges are legal (local angle-optimal).
3. DT(P) is a triangulation of P such that for each triangle the circle criterion is fulfilled.
4. DT(P) is global angle-optimal triangulation.
5. DT(P) is a triangulation of P such that for each edge pi pj there is a circle, on which pi and pj lie and which does not contain any other point from P.
Algorithm DT(P) (randomized, incremental)
Given: Point set P = {p1..., pn }
Initially: Compute triangle (x, y, z), which includes the points p1..., pn.
m
z (0,3m)
y (3m,0)
x (-3m,-3m)
Algorithm DT(P)m = max { |xi|,|yi| }
T = ((3m, 0), (3m, 3m), (0, 3m))
1. initialize DT(P) as T.
2. permutate the points in P randomly.
3. for r = 1 to n do
find the triangle in DT(P), which contains pr ;
insert new edges in DT(P) to pr ;
legalize new edges.
4. remove all edges, which are connected with x, y or z.
Inserting a Point
pi
pj
pr
pi
pj
pkpr
pl
pi
pj
pk
pr
2 cases : pr is inside a triangle pr is on an edge
Legalize (pr ,pi pj ,T):if pi pj is illegal then let pi pj pk be the triangle adjacent
to pr pi pj along pi pj.flip pi pj ,i.e. replace pi pj by pr pk
Legalize (pr , pi pk, T)Legalize (pr , pk pj, T)
Algorithm Delaunay Triangulation Input: A set of points P = {p1..., pn } in general position
Output: The Delaunay triangulation DT(P) of P
1. DT(P) = T = (x, y, z)
2. for r = 1 to n do
3. find a triangle pi pj pk T, that contains pr.
4. if pr lies in the interior of the triangle pi pj pk
5. then split pi pj pk 6. Legalize(pr, pi pj), Legalize(pr, pi pk),
Legalize(pr, pj pk)
7. if pr lies on an edge of pi pj pk (say pi pj)
8. then split pi pj pk and pi pj pl Legalize (pr, pi pl), Legalize (pr, pi pk), Legalize (pr, pj pl), Legalize (pr, pj pk)
9. Delete (x, y, z) with all incident edges to P
CorrectnessLemma :Every new edge created in the algorithm for constructing DT during the intersection of pr is an edge of the Delaunay graph of { p1,...,pr } .
pq is a Delaunay edge iff there is a (empty) circle, which contains only p and q on the circumference.
Proof idea : Shrink a circle which was empty before addition of pr !
pr
pr
Observation: After insertion of pr , every new edge produced by edge-flips is incident to pr!
Correctness of the algorithm: Consider newly produced edges:Correctness
pr
pi
pj
pk
Edge-flips produce only legal edges.
Before inserting pr , circle that goes through pi, pj, pk was empty!
Edge-flips produce edges that are always incident to pr !
Edge Flips
Data Structure for Point Location
t1
t2
t3
t2
t3
t4t5
t4
t6
t7
t1 t2
t4 t5
t3
t6 t7
t1 t2 t3
t1 t2 t3
t1 t2
t4 t5
t3
Split t1
flip pi pj
flip pi pk
pi
pj
pi pk
Analysis of the Algorithm
Lemma :
The expected number of triangles created by the incremental algorithm for constructing DT(P) is atmost 9n + 1.
Analysis of the RuntimeTheorem :
The Delaunay triangulation of a set of P of n points in theplane can be computed in O(n log n) expected time, usingO(n) expected storage.
Proof :
Running time without Point Location :Proportional to the number of created triangles = O(n).
Point Location :The time to locate the point pr in the current triangulationis linear in the number of nodes of D that we visit.
Relation to Euclidean MST
• Problem:– Find a spanning tree on a set P of N points in a plane such that
the total length of all the edges of the tree is minimized (Euclidean MST)
• Relation of Delaunay Triangulation– A Euclidean MST is a sub-graph of a Delaunay Triangulation– Blindly applying MST finding methods like Kruskal’s or Prim’s on
this problem gives O(m) where m = n(n-1)/2.– If one builds the DT(P) in O(nlogn) and then does Kruskal’s or
Prim’s, one get an O(nlogn) solution.
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
Geometry and Air Traffic Control
» Avoid collisions at all costs.» Start by finding the two aeroplanes closest to each other, at specific elevations.
» During peak times over 5000 planes, per hour, around Frankfurt airspace.
Geometry and Astronomy» Find the two nearest objects in space.
Search Quickly… and Why?» Find the two nearest objects in space.
Problem Description
• Input: A set S of n points in the plane.• Output: The pair of points with the shortest Euclidean distance
between them.
• Other applications in fundamental geometric primitive:– Graphics, computer vision, geographic information systems, molecular
modeling
– Special case of nearest neighbour, Euclidean MST, Voronoi
Closest Pair of Points
• Naïve method: Brute force.– Check all pair of points p and q with Θ(n2) comparisons.
• 1D version– Simple if all points are on a single line.
– Run time O(n log n)
Key to reducing quadratic runtime of the naïve method:
Simply don´t visit each point n times.
Algorithm: DAC – Divide
• Sort S in ascending order of x-coordinates.
• Find the median line L, so that we have ½ n points in SL and ½ n points SR.
SL SR
Algorithm: DAC – Conquer
• Recursively, find the closest pair in SL and SR.
SL SR
Algorithm: DAC – Merge (Strip-Margins)
• Assuming that distance < S = min(SL, SR).
• Observation: Only need to consider the points within S of line L.
SL SRS
S = min(12,21)
Handling Merge
• Sort points in 2S-strip in ascending y-coordinate order.
• (First observation) Only check those points within 11 positions in sorted list.
SL SRS
S = min(12,21)
Comparing EfficientlyDefinition: Let si be the point in the 2S-strip with the
ith smallest coordinate.
Claim: If |i - j| 12, then the distance between si and sj is at least S.
Proof:
• The points si and sj cannot lie in same ½ S-by- ½ S box.
• si and sj are at least 2 rows apart, and have distance 2(½ S).
Fact:
• The above still holds if we replace 12 with 7.SS
Closest Pair: DAC AlgorithmAlgorithm: Closest-Pair( S = {p1, …, pn} )
Sort all n points in ascending x-coordinate order
Compute the median line L, and divide S into SL and SR
Set SL Closest-Pair(SL)
Set SR Closest-Pair(SR)
Set S min(SL,SR)
Delete all points further than S from separation line L
Sort remaining points in ascending y-coordinate order
Scan points in y-order and compare distance between each point and next 7 neighbours. Update S if new shorter distance is found.
Return S
Closest Pair: DAC Analysis
Run time:T(N) 2T(N/2) + O(N log N) T(N) = O(N log2 N)
Can we achieve a better run time?Yes. Don’t sort points in strip from scratch each time.• Each recursive step returns two lists: all points sorted by y-coordinate,
and all points sorted by x-coordinate.• Sort by merging two pre-sorted list.
T(N) 2T(N/2) + O(N) T(N) = O(N log N)
Space:O(n)
Algorithm: Scan-Line
• Sort S in ascending order of x-coordinates.
• Place sorted points p1, …, pn in the event-queue.
• Initialise distance(p1, p2).
• Start scanning from p3.» Event-points?
» Status structure?
The Status-Structure
• Maintain the status-structure (-slab):
– Insert the new event-point pi.
– Remove all points that are greater than distance to the left of the current event-point pi.
– All points in the status structure are maintained in ascending y-coordinate order.
The Status-Structure
• Maintain the status-structure (-slab):
– Insert the new event-point pi.
– Remove all points that are greater than distance to the left of the current event-point pi.
– All points in the status structure are maintained in ascending y-coordinate order.
– Perform the ‘Efficient Comparison’, as described earlier, BUT only between the new event point pi and at most 7 other nearest points to pi.
– Update if a shorter distance is found.
Closest Pair: Scan-Line Analysis
Invariant:
Whenever an event-point pi has been handled, then is the minimum distance between any pair of points encountered from p1 up to pi.
Run time:
O(n log n)
Space:
O(n)
Related Problems
• Nearest neighbour query:– Given a set S of n points, and a query point q,
determine which s S is closest to q.
– Brute force: O(n)
– Goal: O(n log n) pre-processing, O(log n) per query.
• Voronoi region, VR(S): – A set of points closest to a given point.
• Voronoi diagram, VD(S):– Planar subdivision deliniating Voronoi regions.
• Delaunay Triangulation (the dual of VD(S)).
What will be covered?
• Mainly popular static problems:– Point inside polygon– Convex hull– Line segment intersection– Delaunay triangulation– Closest pair of points
Acknowledgment
• Slides prepared mainly from the materials of Prof. Dr. Thomas Ottman