Title: Computational Geometry
1Computational Geometry
2What is Computational Geometry?
- Deals with problems involving geometric
primitives - points
- lines
- polygons
3Geometric Primitives
- Points
- a geometric element determined by an ordered set
of coordinates - 2D (x,y)
- 3D (x,y,z)
- 4D (x,y,z,w)
-
4Geometric 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
5Geometric Primitives
- Polygons
- circular sequence (cycle) of points (vertices)
and segments (edges)
6Some Geometric Problems
7Segment 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
8Orientation
if slope2 gt slope1 then counterclockwise else
if slope2 lt slope1 clockwise else
colinear
9Intersection 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)
10Intersection and Orientation
11Intersection 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
12Intersection and Orientation(special cases)
q
2
p
2
q
q
2
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
13How to Compute the Orientation?
- slope of segment (p1,p2) ? (y2-y1) / (x2-x1)
- slope of segment (p2,p3) ? (y3-y2) / (x3-x2)
p3
p2
- Orientation test
- counterclockwise (left turn) ? lt ?
- clockwise (right turn) ? gt ?
- collinear ? ?
p1
- The orientation depends on whether the expression
(y3-y2) (x2-x1) - (y2-y1) (x3-x2) is positive,
negative, or zero.
14Point Inclusion
- given a polygon and a point
- is the point inside ?
- or
- outside the polygon?
15Point 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
16Point 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 !
17Segment-Segment Intersection
18Segment-Segment Intersection
- Problem Definition
- Let n of input segments in a plane
- Find all intersection points among segments.
How many intersections k, possible?
19Brute 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.
20Goal
- Spend time depending on the of intersection ?
Idea Avoid testing all pairs. Do not Intersect
if they are far apart.
21Sweep-line
Sweep line
ab
Status
Event Points
a
b a
a b
a
a1 b1 b2 a2
ab b2 a2
b2 a2
a2
b1 b2 a2
22Plane 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
23Plane 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
24Plane Sweeping Algorithm
25Plane 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((nk) log(nk))
- k is at most n2.
- So cost of maintaining Q O((nk) log n)
- Cost of maintaining segment list is O(n log n)
26Plane Sweep Algorithm
- Analysis (contd.)
- Total cost of maintaining Q O((nk) 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(nk).
- Thus total cost of maintaining segment list O(n
log n) - Total overall cost O((nk) log n)
27Simple Closed Path
ProblemGiven a set of points ...
Connect the dots without crossings
28Simple 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
29Simple Closed Path
- Traverse the points by increasing angle
a
30Simple Closed Path
How do we compute angles?
a
- 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.
31Simple Closed Path
How do we compute angles?
a
- Observation
- we dont 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!!
32Simple Closed Path
- Orientation to sort by angles ?
- angle(p) lt 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
33Convex Hull
34Convex 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
35Convex 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
36Special Cases
- The convex hull is a segment
- All the points are collinear
- The convex hull is a point
- All the points are coincident
37Applications
- 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
38The 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.
39Package 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 CCW orientation, i.e.,
for any other point, we have orientation(c, p,
q) CCW
c
40Time 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 (M ? N)
- Time complexity ?(M N)
- Worst case ?(N2)
- (All the points are on the hull, and thus M
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
41Computing 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
42Removing 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 - q convex ? orientation(p, q, r) CCW
- q nonconvex ? orientation(p, q, r) CW or COLL
r
r
q
q
p
p
43Graham Scan
- 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 - while orientation(p, q, r) CW or COLLremove q
from Hq ? pp ? vertex preceding p in H - Add r to the end of H
44Analysis
- 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
45Quick Hull Algorithm
46Quick 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
47Quick 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
48Quick Hull Example
J
O
K
P
A
L
N
A B D F G H J K M O P
49Quick Hull Example - Maximum
J
O
K
P
A
L
N
A B D F G H J K M O P
50Quick Hull Example - Filter
J
O
K
P
A
L
N
A B F J J O P
51Quick Hull Example - Maximum
J
O
B
K
P
A
L
N
A B F J J O P
52Quick Hull Example - Base Case
J
O
B
K
P
A
L
N
A B B J J O P
53Quick Hull Example - Done
J
O
B
K
P
A
L
N
A B B J J O O P
54Divide and Conquer Algorithm
55Divide and Conquer Algorithm
B
A
56Divide and Conquer Algorithm
B
A
57Divide and Conquer Algorithm
Upper Tangent
B
A
Lower Tangent
58Divide and Conquer Algorithm
Upper Tangent
B
A
Lower Tangent
59Divide and Conquer Algorithm-Find Lower Tangent-
b
B
A
a
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
60Divide and Conquer Algorithm -Find Lower Tangent-
b
a1
B
A
a
a
a-1
a-1
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
61Divide and Conquer Algorithm -Find Lower Tangent-
b
b-1
b1
b
B
A
a
a
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
62Divide and Conquer Algorithm -Find Lower Tangent-
b
B
A
a
a-1
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
63Divide and Conquer Algorithm -Find Lower Tangent-
B
b
A
a
a-1
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
64Divide and Conquer Algorithm -Find Lower Tangent-
B
b
A
a
a-1
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
65Divide and Conquer Algorithm -Find Lower Tangent-
B
A
b
a
a-1
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
66Divide and Conquer Algorithm -Find Lower Tangent-
B
A
b
b1
a
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
67Divide and Conquer Algorithm -Find Lower Tangent-
B
A
b
a
ab is a lower tangent if (a-1) and (a1) are
above/isLeftOn ab and (b-1) and (b1) are
above/isLeftOn ab
68Dynamic Convex Hull
- The basic convex hull algorithms were fairly
interesting, but you may have noticed that you
cant draw the hull until after all of the points
have been specified. - Is there an interactive way to add points to the
hull and redraw it while maintaining an optimal
time complexity?
69Two Halves One Hull
- For this algorithm, we consider a convex hull as
being two parts - An upper hull...
and a lower hull...
70Adding points Case 1
- Case 1 the new point is within the horizontal
span of the hull - Upper Hull 1a
- If the new point is above the upper hull,
then it should be added to the upper hull and
some upper-hull points may be removed. - .
Upper Hull 1b If the new point is below
the upper hull, no changes need to be made
71Case 1 (cont.)
- The cases for the lower hull are similar.
- Lower Hull 1a
- If the new point is below the lower hull,
then it is added to the lower hull and some
lower-hull points may be removed. -
Lower Hull 1b If the added point is above
the existing point, it is inside the existing
lower hull, and no changes need be made.
72Adding Points Case 2
- Case 2 the new point is outside the horizontal
span of the hull - We must modify both the upper and lower hulls
accordingly.
73Hull Modification
- In Case 1, we determine the vertices l and r of
the upper or lower hulls immediately
preceding/following the new point p in the
x-order.
If p has been added to the upper hull, examine
the upper hull rightward starting at r. If p
makes a CCW turn with r and its right neighbor,
remove r. Continue until there are no more CCW
turns. Repeat for point l examining the upper
hull leftward. The computation for the bottom
hull is similar
Bad
74Analysis
- Let n be the current size of the convex hull
- Operation locate takes O(log n) time
- Operation insert takes O((1 k)log n) time,
where k is the number of vertices removed - Operation hull takes O(n) time
- The amortized running time of operation insert is
O(log n)