You are on page 1of 71

COMPUTATIONAL GEOMETRY

GUN SRIJUNTONGSIRI

POINT-LINE DISTANCE

Given a point P and a line L in 2D, the distance between them is the distance between P and the point P on L that is closest to P. How to compute this distance?
Note: a line with slope = m is perpendicular to a line with slope = -1/m.

One method: use calculus. Can do the same for 3D.

ART GALLERY GUARDING

ART GALLERY GUARDING


Whats the smallest number of cameras needed to guard a given gallery? And where to place them?
Assume cameras can rotate 360 degrees.

FORMAL PROBLEM
Assume the floor plan of a gallery is a closed simple polygon.
Edges of the polygon do not intersect among themselves. No holes inside!

Let n be the number of vertices of the polygon (the floor plan).


6

Lets find the upper bound for the number of cameras needed for a gallery of size n.
Will find the worst-case bound. Finding the minimum number of cameras for the specific polygon is NP-hard.

THE IDEA
Decompose the polygon into pieces that are easier to guard---triangles. 1. By drawing diagonals between pairs of vertices.
The diagonals must stay inside the polygon. They must not intersect each other.

2. Keep drawing diagonals until you cannot add any more. 3. Now you have a triangulation of the polygon. Then place one camera in each triangle. (Why does this work?)

A polygon can have more than one triangulation. Questions:


Can you always find a triangulation? How many triangles are in a triangulation? Do every triangulations of the same polygon contain the same number of triangles?

Theorem: Every simple polygon admits a triangulation, and any triangulation of a simple polygon with n vertices consists of exactly n-2 triangles. Proof: Exercise.

10

Proof by induction on n. Base: n = 3. One triangle = 3-2. Inductive:


Draw a valid diagonal (always exists, can be proven). The diagonal separates the polygon into two subpolygons of size, say, m1 and m2 vertices. m1 + m2 = n + 2 (the two endpoints of the diagonal belong to both subpolygons). So by induction hypothesis, there are m1-2 + m2-2 = m1 + m2 4 = n 2 triangles in the simple polygon with n vertices.

11

WHAT WE GOT FROM THE PROOF


A recursive algorithm for finding a triangulation!

12

Placing a camera per triangle is overkill.


A camera on a diagonal can guard two triangles.
About n/2 cameras if choosing cameras correctly.

A camera on some vertex may be even better. Can guard many triangles sharing the vertex.

13

BETTER APPROACH
Find a subset of vertices of the polygon so that any triangle in a triangulation has at least one selected vertex. Place cameras at the chosen vertices. Question: How to choose this subset?

14

HOWS ABOUT 3-COLORING?


Use polygon vertices as vertices of the graph. Use triangle edges as edges of the graph. Find 3-coloring of this graph. (Does it exist?) If found, find the color with smallest number of vertices. Place cameras at the vertices with that color. So use floor(n/3) cameras.

15

3-COLORING OF TRIANGULATION ALWAYS EXISTS


Consider the dual graph G(T) of a triangulation T. The dual graph:
One vertex for each triangle. Has edge between vertex v and w if the two triangles share a diagonal.

16

Claim: G(T) is a tree!


Proof: Exercise.

17

Proof that G(T) is a tree:


Any diagonal cuts the polygon into two. Same as: removing an edge from G(T) splits the graph into two. => G(T) doesnt have a cycle.

18

HOW TO FIND 3-COLORING OF A TRIANGULATION?


Exercise. Hint: Use the fact that G(T) is a tree.

19

TO FIND 3-COLORING
Use Depth-First-Search on G(T) starting from any vertex v. Color the three vertex of the triangle corresponding to v with the three colors. When reach the next node w of G(T), two of the vertices of the triangle corresponding to w are already colored. Color the remaining vertex with the third color.
Can do so because G(T) is a tree so the other nodes adjacent to w are not colored yet.

20

So, a simple polygon can be guarded with floor(n/3) cameras. Can we do better?

21

For any n, there are simple polygons that require floor(n/3) cameras. Ex: n vertex polygon that needs floor(n/3) cameras:

So floor(n/3) is the best bound for worst case.

22

ARE WE DONE?
Not yet. Still need an algorithm for finding triangulation.

23

TRIANGULATION: EAR CLIPPING


Take the leftmost vertex v of the polygon. Try to connect its two neighbors u and w. If uw is inside the polygon, this is a valid diagonal. Otherwise, there must be other vertices inside the uvw triangle. Connect v with the vertex inside uvw that is farthest from uw instead. Use it as the diagonal. Then recursively find triangulations of the two subpolygons.

24

Take linear time to find a diagonal. The diagonal may split the polygon into a triangle and a polygon with n-1 vertices.
E.g. if uw is a chosen diagonal.

So worst case is n-3 iterations. So O(n^2) operations. Better algorithms:


Using monotone polygons, O(n log n), Chazelle, O(n), very complex.

25

ORTHOGONAL RANGE SEARCHING

26

ORTHOGONAL RANGE SEARCHING


Given (many) points in the 2D plane. Want to find the ones inside a given box.

27

1D RANGE SEARCHING
Given a set of real numbers, find all points in the set that are in [x, x]. There is a solution using an array.
But cannot generalize to higher dimensions.

Another generalizable solution: Use a balanced binary tree.

28

BALANCED BINARY TREE


Store data on leaves ONLY. Internal nodes store splitting values.

29

Assume: nodes on the left subtree of v have values < that of v. Nodes on the right subtree of v have values >= that of v.

30

1D RANGE SEARCH
Range is [x, x]. Search with x in the tree. Let m be the leaf where the search ends. Search with x in the tree. Let m be the leaf where the search ends. All leaves between m and m are in the range. Question: Are m and m in the range?
Maybe. Have to check.

31

HOW TO REPORT THE LEAVES IN BETWEEN M AND M?


They are the leaves of certain subtrees in between the search paths to m and m. First, find the node where the paths to m and m split.
Call it v_split.

32

Then, starting from v_split, follow the search path of x. At each node where the path goes left, report all leaves in the right subtree.
Dont report anything when the path goes right.

33

Similarly, follow the search path of x starting from v_split. At each node where the path goes right, report all leaves in the left subtree.
Dont report anything when the path goes left.

Also check if m and m are in the range.

34

EXERCISE
Implement 1D Range Searching

35

TIME COMPLEXITY
Let n be the number of points. Building a balanced binary search tree can be done in O(n log n) time. Worst-case for 1D range search (assuming you already have the tree)?
(n) if all points are in the range.

But just checking every point directly is also (n)! So whats the point?

36

Let k be the number of outputs (the number of points in [x, x]). Lets analyze the running time as a function of both n and k. Note: the time to report all leaves in a subtree is linear in the number of reported points. Therefore, the time spent to report all points in the range (excluding possibly m and m) is O(k). Time to search for x and x = O(log n). So total is O(k + log n).
37

2D RANGE SEARCHING
Given a set of points (px, py), find all points inside a given rectangle [x, x] x [y, y]. A point is inside this rectangle if x <= px <= x and y <= py <= y.
Assume for now: no two points have the same x value. No two points have the same y value.

38

TO GENERALIZE TO 2D
Can think of 1D binary search tree as follows: The set of 1D points is split into two subsets of roughly equal size:
One subset contains the points smaller than or equal to the splitting value. The other contains the points larger than the splitting value.

The splitting value is stored at the root. The two subsets are stored recursively in the two subtrees.

39

In 2D, each point has two values: its x value and its y yalue. First, split on x-coordinate, Next on y-coordinate, Then on x-coordinate, and so on.

40

At the root, we split the set of points with a vertical line l. All points to the left of l and on l will be in the left subtree. All points to the right of l will be in the right subtree. Store l at the root.

41

2D-TREES (OR 2 DIMENSIONAL KDTREES)

42

EXERCISE
Implement a program to construct a 2D tree from a given set of 2D points.
Split at the median. (In general, you can split at other places). The median of n numbers should be defined as the floor(n/2)-th smallest number. E.g. the median of 2 numbers is the smaller one. Otherwise, the algorithm may not terminate. Median can be found in O(n), where n is the number of points (dont have to do this unless you know how to do).

43

2D QUERY
An internal node v of a 2D tree defines a rectangular region. Call it region(v). Note: Any points stored in a subtree rooted at a node v lies in region(v).

44

THE ALGORITHM

45

EXAMPLE OF HOW THE ALGORITHM WORKS

46

EXERCISE
Implement 2D Range Search.

47

The query algorithm is O(sqrt(n) + k) time, k is the number of reported points.

48

GENERAL SETS OF POINTS


We have been assuming that no two points have equal x- or y- coordinate.
Unrealistic.

Lets lift this assumption.

49

A composite number of two reals a and b is denoted by (a|b). (a|b) < (a|b) iff a < a or (a = a and b < b) Replace each point p := (px, py) by a new point p = ( (px|py), (py|px) ). Also replace the desired range R := [x, x] x [y, y] by R := [(x|-), (x|)] x [(y|-), (y|)] Will we get correct result? Lemma: p is in R iff p is in R. Proof: Exercise.
50

Proof: p in R iff x <= px <= x and y <= py <= y, which is iff (x|-) <= (px|py) <= (x|) and (y|-) <= (py|px) <= (y|).

51

NEAREST NEIGHBOR SEARCH

52

NEAREST NEIGHBOR SEARCH


Given a set of 2D points P and a query point q, find the point in P that is closest to q. Straightforward approach: Compute distance from q to every point in P.
O(n), where n = |P|.

53

NEAREST NEIGHBOR SEARCH WITH 2DTREES


Construct the 2D-tree of the points in P. Search the tree with q from the root. Once reach the leaf, set the leaf as the current best. Unwind the recursion. On each node, check if any points on the other side of the splitting plane can be closer to q than the current best (How?). If so, do the same search on that other subtree. If not, can eliminate that other subtree and go back up and continue unwinding.
54

This algorithm is O(sqrt(n)) in worst case, O(log n) if points are randomly distributed.

55

EXERCISE
Implement nearest neighbor search using 2D-tree.

56

POINT LOCATION

57

POINT LOCATION
Given a map and a location, in what country is this location? Problem: Given a planar subdivision (formed by multiple polygons) S and a point q, report the face of S that contains q. If q lies on an edge or a vertex, report that, too.

58

PLANAR SUBDIVISION

59

DOUBLY-CONNECTED EDGE LIST


A common representation for a planar subdivision. Use half-edges. If the subdivision has an edge connecting vertices u and v, then store a directed half-edge from u to v and another directed half-edge from v to u. Define the incident face so that if you walk around the face by following the half-edges, the face is on your left. The face record stores a pointer to one of the halfedges bounding it.
60

61

62

REMARKS
IncidentEdge of a vertex is an arbitrary half-edge that has v as its origin (if more than one, choose any). InnerComponents(f) is when holes are allowed. Can ignore them for now.

63

POINT LOCATION
Draw vertical lines through all vertices of the subdivision. This partitions the plane into vertical slabs. Store the x-coordinates of the vertices in sorted order in an array.
So can determine in O(log n) time which slab contains a point q.

64

PROPERTIES OF A SLAB
No vertices of S inside a slab (only at boundaries). So all edges of S in a slab do not cross each other! So can order the edges from top to bottom. Region in the slab between two consecutive edges belong to a unique face of S. The lowest and highest region of the slab are the unbounded face of S. So store edges in sorted order in an array. Label each edge with the face of S immediately above and below it.
65

THE QUERY ALGORITHM


Do a binary search with x-coordinate of the query point q to find the slab containing q. Do a binary search with q in the array for that slab. Time Complexity:
Suppose S has n edges. So the first binary search is on an array with <= 2n elements (n edges, at most 2n vertices). The second binary search is on an array with <= n elements (at most n edges in a slab). So O(log n).

66

EXERCISE
Implement this algorithm for Point Location. You can store the faces immediately above and below a given edge. Or you can use doublyconnected edge list.

67

VORONOI DIAGRAM

68

THE POST OFFICE PROBLEM


There are a number of post offices. Want to decide the jurisdiction of each post office in the following way: A house H is in post office Ps jurisdiction if P is the closest post office to H. The diagrams showing the jurisdiction of every post office is a Voronoi diagram. Post offices are often called sites.

69

EXAMPLE OF A VORONOI DIAGRAM

70

SOME PROPERTIES OF VORONOI DIAGRAMS


If all the sites (suppose there are n of them) are collinear, then the Voronoi diagram is n-1 parallel lines. For n >= 3, the number of vertices in the Voronoi diagram of n sites is at most 2n-5 and the number of edges is at most 3n-6. A vertex of the Voronoi diagram is equally-far-away from three or more sites. A point on an edge of the Voronoi diagram is equally-far-away from two sites.

71

You might also like