Geometry
Geometry
History.
Reference: Chapters 24-25, Algorithms in C, 2nd Edition, Robert Sedgewick. ! Ancient mathematical foundations.
! Most geometric algorithms less than 25 years old.
Point: two numbers (x, y). any line not through origin Warning: intuition may be misleading.
Line: two numbers a and b [ax + by = 1] !Humans have spatial intuition in 2D and 3D.
Line segment: two points. !Computers do not.
Polygon: sequence of points. !Neither has good intuition in higher dimensions!
1 2 18 4 18 4 19 4 19 4 20 3 20 3 20
Jordan curve theorem. [Veblen 1905] Any continuous simple closed Does line segment intersect ray?
curve cuts the plane in exactly two pieces: the inside and the outside.
yi+1 " yi (xi+1 , yi+1 )
y = (x " xi ) + yi
Is a point inside a simple polygon? xi+1 " xi
where xi # x # xi+1 !
(xi , yi )
!
! (x , y )
!
public boolean contains(double x0, double y0) {
int crossings = 0;
for (int i = 0; i < N; i++) {
double slope = (y[i+1] - y[i]) / (x[i+1] - x[i]);
boolean cond1 = (x[i] <= x0) && (x0 < x[i+1]);
boolean cond2 = (x[i+1] <= x0) && (x0 < x[i]);
boolean above = (y0 < slope * (x0 - x[i]) + y[i]);
http://www.ics.uci.edu/~eppstein/geom.html
if ((cond1 || cond2) && above) crossings++;
}
return (crossings % 2 != 0);
Application. Draw a filled polygon on the screen. }
5 6
CCW. Given three point a, b, and c, is a-b-c a counterclockwise turn? CCW. Given three point a, b, and c, is a-b-c a counterclockwise turn?
!Analog of comparisons in sorting. !Determinant gives twice area of triangle.
!Idea: compare slopes.
ax ay 1
c c b
2 " Area(a, b, c) = bx by 1 = (bx # a x )(c y # a y ) # (by # a y )(c x # a x )
c b c
cx cy 1
b c b b a c
!
!
If area > 0 then a-b-c is counterclockwise.
a a a a b a
If area < 0, then a-b-c is clockwise.
yes no Yes
If area = 0, then a-b-c are collinear.
??? ??? ???
(! slope) (collinear) (collinear) (collinear)
7 8
Immutable Point ADT
Convex Hull
public final class Point {
public final int x;
public final int y;
9 10
A set of points is convex if for any two points p and q in the set, Mechanical algorithm. Hammer nails perpendicular to plane;
the line segment pq is completely in the set. stretch elastic rubber band around points.
p p
q q
convex hull
Properties.
! "Simplest" shape that approximates set of points.
! Shortest (perimeter) fence surrounding the points.
! Smallest (area) convex polygon enclosing the points.
11 12
Brute Force Package Wrap (Jarvis March)
13 14
Implementation. Parameters.
!Compute angle between current point and all remaining points. ! N = number of points.
!Pick smallest angle larger than current angle. ! h = number of points on the hull.
!"(N) per iteration.
Package wrap running time. "(N h) per iteration.
15 16
Graham Scan: Example Graham Scan: Example
Running time. O(N log N) for sort and O(N) for rest.
why?
17 18
Quick elimination.
!Choose a quadrilateral Q or rectangle R with 4 points as corners.
!If point is inside, can eliminate. Q
Algorithm Running Time
– 4 ccw tests for quadrilateral Package wrap Nh
– 4 comparisons for rectangle
R Graham scan N log N
Quickhull N log N
Three-phase algorithm
! Pass through all points to compute R. Mergehull N log N
Practice. Can eliminate almost all points asymptotic cost to find h-point hull in N-point set
these
in linear time. points t assumes "reasonable" point distribution
eliminated
19 20
Convex Hull: Lower Bound
Models of computation.
!Comparison based: compare coordinates.
Closest Pair of Points
(impossible to compute convex hull in this model of computation)
21 22
Closest pair. Given N points in the plane, find a pair with smallest Algorithm.
Euclidean distance between them. ! Divide: draw vertical line L so that roughly !N points on each side.
! Conquer: find closest pair in each side recursively.
Fundamental geometric primitive. ! Combine: find closest pair with one point in each side.
! Graphics, computer vision, geographic information systems, ! Return best of 3 solutions. seems like "(N2)
23 26
Closest Pair of Points Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < $. Def. Let si be the point in the 2$-strip, with
Observation: only need to consider points within $ of line L.
! the ith smallest y-coordinate.
Sort points in 2$-strip by their y coordinate.
!
$ 30
$ $ 31
Running time.
Closest-Pair(p1, …, pn) {
Compute separation line L such that half the points O(N log N)
are on one side and half on the other side. T(N) " 2T ( N /2) + O(N log N) # T(N) = O(N log 2 N)
$1 = Closest-Pair(left half)
2T(N / 2)
$2 = Closest-Pair(right half)
!
$ = min($1, $2)
avoid sorting by y-coordinate from scratch
Delete all points further than $ from separation line L O(N)
Sort remaining points by y-coordinate. O(N log N) Upper bound. Can be improved to O(N log N).
Scan points in y-order and compare distance between Lower bound. In quadratic decision tree model, any algorithm for
each point and next 11 neighbors. If any of these O(N)
distances is less than $, update $. closest pair requires #(N log N) steps.
return $.
}
32 33
1854 Cholera Outbreak, Golden Square, London
Nearest Neighbor
http://content.answers.com/main/content/wp/en/c/c7/Snow-cholera-map.jpg
34 35
Input. N Euclidean points. Voronoi region. Set of all points closest to a given point.
Voronoi diagram. Planar subdivision delineating Voronoi regions.
Nearest neighbor problem. Given a query point p, which one of original Fact. Voronoi edges are perpendicular bisector segments.
N points is closest to p?
36 37
Voronoi Diagram Voronoi Diagram: Applications
Voronoi region. Set of all points closest to a given point. Toxic waste dump problem. N homes in a region. Where to locate
Voronoi diagram. Planar subdivision delineating Voronoi regions. nuclear power plant so that it is far away from any home as possible?
Fact. Voronoi edges are perpendicular bisector segments.
looking for largest empty circle
(center must lie on Voronoi diagram)
38 39
http://www.diku.dk/hjemmesider/studerende/duff/Fortune
42 43
Discretized Voronoi. Solve nearest neighbor problem on an N-by-N grid. Hoff's algorithm. Align apex of a right circular cone with sites.
!Minimum envelope of cone intersections projected onto plane is
the Voronoi diagram.
!View cones in different colors & render Voronoi.
Brute force. For each grid cell, maintain closest point. When adding a Implementation. Draw cones using standard graphics hardware!
new point to Voronoi, update N2 cells. http://www.cs.unc.edu/~geom/voronoi/siggraph_paper/voronoi.pdf
44 45
Delaunay Triangulation Summary
Delaunay triangulation. Triangulation of N points such that no point Summary. Many fundamental geometric problems require ingenuity
is inside circumcircle of any other triangle. to solve large instances.
polygon triangulation N2 N
46 47