Polygon Triangulation Shmuel Wimer Bar Ilan Univ., School of Engineering Feb 2011 1 Definitions v0 , v1, ,vn1, vn1 vertices of an n-vertex polygon. Edges e0 v0 v1 , , ei vi vi 1 , , en 1 vn 1v0 are bounding a polygon by a simple closed curve iff: ei ei 1 vi 1 , i 0, , n 1, and ei e j , j i 1. Jordan Curve theorem : Every simple closed plane curve divides the plane into two components. Feb 2011 2 Simple polygon Nonsimple polygons There are no holes in a simple polygon. Feb 2011 3 The Art Gallery Problem How many stationary guards are needed to guard a room? Guards are located at fixed points and have 2π range of visibility. A guard cannot see through walls. Given a polygon P, a point x can see a point y (y is visible from x) iff [ x, y] P. A set of guards covers a polygon if every point of the polygon is visible to some guard. Problem: Find the max over all n-vertex polygons of the min number of guards needed to cover the polygons. Feb 2011 4 This 12-vertex polygon needs 3 guards to cover it. This 12-vertex polygon needs 4 guards to cover it Feb 2011 5 g P - smallest number of guards needed to cover P. g P min | S : S covers P | . S G n - maximum of g Pn over all n-vertex polygons. G n max g Pn . Pn What is G n ? A trivial lower bound: G n 1 G n n. A trivial upper bound: Put a guard at each vertex. Feb 2011 6 What is G 3 ? G 3 1. What is G 4 ? G 4 1. Put a guard on internal diagonal. G 5 1. Put a guard at overlap of triangles. Feb 2011 7 Hexagons may require two guards, G 6 2. A shape with k prongs, n 3k , is covered by k guards. n 12, k 4 g P 4 Is the conjecture G n n 3 true? Feb 2011 8 Proving G n n 3 Necessity follows from the prong’s example. First Proof of sufficiency is due to Chvátal in 1975 (very complex). We’ll present a simple, four-step proof by Fisk from 1978. 1. Triangulation of polygon. 2. 3-coloring of triangulation graph. 3. Placing guards at a single color guarantees cover. 4. “Pigeon-hole principle”: placing n objects in k holes, one at least contains no more than └n/k┘ objects. Feb 2011 9 Diagonals and Triangulation A diagonal of a polygon is a line segment connecting two visible vertices. [a, b] is a diagonal of P iff: [a, b] P a, b and (a, b) P \ P. Diagonals [a, b] and [c, d ] are called noncrossing if (a, b) (c, d ) . Triangulation: the partition of a polygon by maximal non crossing diagonals into triangles (not unique!). Feb 2011 10 n=14 Interesting, both triangulations have same number of diagonals! Notice that number of diagonals equals n-3. Feb 2011 11 3-Coloring G(V,E) is a graph defined on a triangulated polygon P. E consists of P’s edges and the triangulation diagonals, while V consists of P’s vertices. k-coloring is an assignment of k colors to the vertices of V such that no adjacent vertices have same color. We’ll show later that a triangulation graph is 3-colorable. Feb 2011 12 5 4 5 6 3 5 Feb 2011 The coloring of a triangulation graph is unique! Assigning color to first vertex enforces the rest color assignments. 13 Guards Placement It is sufficient to place guards at one color only (say red). This follows since every triangle must have 3 distinct colors at its corners, hence one must be red. Therefore every triangle is covered. But the triangles cover the entire polygon P. Pigeon-hole principle: There must be one color whose number of assigned vertices doesn’t exceed └n/3┘. Conclusion : n 3 guards are sufficient. Feb 2011 14 Triangulation Theory Need to prove that every polygon can be triangulated. Though intuitive, proof is not trivial. Lemma: Every polygon must have at least one strictly convex vertex. Proof: Let the vertices be counterclockwise ordered. Traversing the boundary, a convex vertex corresponds to a left turn. Feb 2011 v L 15 Pick the lowest vertex (pick the rightmost If there are few). L is a line passing through v. The edge following v must lie above L. ■ Lemma: Every polygon of n≥4 vertices has a diagonal. Proof: There exists a strictly convex vertex v. Let a and b be vertices adjacent to v. If [a,b] is a diagonal we are done. So assume it is either exterior or intersecting with the boundary of P. Feb 2011 16 avb must contain at least one vertex of P. a b x c d v L Let x be the closest vertex to v, measured orthogonal to the line passing through ab. The interior of cvd cannot contain any point of P. Therefore [ x, v] P x, v, hence a diagonal. ■ Feb 2011 17 Theorem: Every n-vertex polygon P can be triangulated. Proof: By induction on n. If n=3 P is a triangle. Let n≥4. By lemma, P has a diagonal d which divides P into d two polygons P1 and P2, having n1<n and n2<n vertices, resp. P1 and P2 can be triangulated by induction hypothesis. ■ Feb 2011 18 Question: Are all triangulations have the same number of diagonals and triangles? Lemma: Every triangulation of an n-vertex polygon P has n-3 diagonals and n-2 triangles. Proof: By induction on n. For n=3 the claims are trivially true. For n≥4, divide P by a diagonal d into P1 and P2, having n1<n and n2<n vertices, resp. n1+n2 = n+2 since the end vertices of d are duplicated. Feb 2011 19 It follows from the induction hypothesis that the claims hold for n1-vertex and n2-vertex polygons. The total number of diagonals (n1-3)+(n2-3)+1=n-3, where +1 stands for counting d. The number of resulting triangles is (n1-2)+(n2-2)=n-2.■ Corollary: The sum of internal angles in an n-vertex polygon is (n-2)π. Proof: Polygon is covered by n-2 triangles, whose corners are placed at polygon’s vertices. Feb 2011 20 Triangulation Dual A vertex is placed in every triangle. An arc exists between two vertices iff the corresponding triangles are adjacent. Feb 2011 21 Lemma: The dual T of a triangulation of a simple polygon P is a tree whose vertex degree is 3 at most. Proof: Degree claim follows immediately from the fact that a triangle has three sides at most to share. Assume in contrary that T is not a tree. It then must have a closed cycle C, completely contained in P. C must enclose a vertex of P, which is the end point of a diagonal crossing C. Feb 2011 22 C Border vertices Completely in P P must therefore have a hole, thus contradicting its simplicity. ■ Feb 2011 23 Vertices of degree 1 are leaves of T. Vertices of degree 2 lie on T’s paths. Vertices of degree 3 are branches. When rooted at a vertex of degree 1 or 2, T is a binary. Definition: Three successive vertices a, b, c of a polygon form an ear if ac is a diagonal, while ab and bc are not. Theorem: A polygon with n≥4 has two nonoverlapping ears at least. Proof: A leaf of the dual tree T implies an ear. T has n-2 ≥2 vertices, at least 2 of which are leaves. ■ Feb 2011 24 3-Coloring Theorem Theorem: The triangulation graph of a simple n-vertex polygon P is 3-colorable. Proof: By induction on n. P has an ear. Ear removal obtains an (n-1)-vertex polygon, which by induction hypothesis is 3-colorable. The end vertices of the ear’s base (diagonal) are adjacent vertices of P and thus have two distinct colors. The tip of the ear is then colored with the third color. ■ Feb 2011 25 We are done! Feb 2011 26 Area of Polygons Let a, b, c R 3 , and Α b a, c B c a, be two vectors. The B area of the parrallelogram a A b ux Determinant Ax notation: Bx Feb 2011 defined by Α and B is Α B . Ay Bz Az By u x uy uz Ay Az Az Bx Ax Bz u y By Bz Ax By Ay Bx u z . 27 In R 2 Az Bz 0, so the area of a triangle T is: 1 A T Ax By Ay Bx 2 1 bx ax c y a y by a y cx ax 2 1 ax by a y bx a y cx ax c y bx c y by cx . 2 Area of polygons can be found by triangulation and calculation of triangles areas. We’d like to obtain a closed-form formula, independent of triangulation. Feb 2011 28 v3 v4 v2 v5 P v1 v0 v7 v6 A P A v0 , v1, v2 A v0 , v2 , v3 A v0 , vn2 , vn1 A(P)’s expression depends on triangulation. We’ll consider first quadrilaterals and show its area is independent of triangulation. Feb 2011 29 c d c d Q Q b a b a A Q A a, b, c A a, c, d A d , a, b A d , b, c 2A Q ax by a y bx a y cx ax c y bx c y by cx a x c y a y cx a y d x a x d y cx d y c y d x Diagonal related terms are canceled out! For any convex polygon: 2A P Feb 2011 n1 i 0 xi yi1 xi 1 yi 30 v2 What happens for non-convex quadrilateral ? v3 Q v1 A Q A v0 , v1, v2 A v0 , v3 , v2 v0 3 i 0 xi yi1 xi1 yi . The terms involving v0v2 cancel out. Lemma : Let T abc be a triangle whose vertices are counterclockwise oriented, and p a point in the plane, then A T A p, a, b A p, b, c A p, c, a . Feb 2011 31 c T b p a We’d like to obtain the area of a general simple polygon in terms of its vertices only. Theorem : Let the vertices v0 , v1, , vn1, vi xi , yi be labled counterclockwise, defining a polygon P, and let p be an arbitrary point. Then n 1 n 1 A P i 0 A p, vi , vi 1 i 0 xi yi 1 xi 1 yi Feb 2011 n 1 i 0 xi xi 1 yi 1 yi . 32 P is a polygon and p an arbitrary point (internal or external). p’s vertices are cyclically ordered counterclockwise. p We can traverse the edges along the border and consider the triangles viewed from p. The areas of rectangles counterclockwise oriented are added while the clockwise ones are subtracted. This is integration along the border (Green’s Theorem). Feb 2011 33 Proof : By induction on n. The case n 3 was shown before. P has an ear. Assume w.l.o.g it is E vn2 , vn1, v0 , and obtain Pn1 by P \ E. By induction hypothesis A Pn1 n2 A i 0 p, vi , vi1 , and by lemma A E A p, vn2 , vn1 A p, vn1, v0 A p, v0 , vn2 . Since A P A Pn1 A E , and A p, v0 , vn2 A p, vn2 , v0 , the theorem follows. Feb 2011 34 The proof of n 1 i 0 xi yi 1 xi 1 yi is obtained by expanding the determinant expression of E ' s area and canceling out the terms of E and Pn1 common edge. ■ Feb 2011 35 Triangulation Complexity Implementing triangulation as in the existence theorem requires O n4 time. There are n n 3 2 diagonal candidates. Cheking validity of a diagonal requires intersection test against all edges and previously defined diagonals, which takes O n time. This is repeated n 3 times, yielding total O n4 time. Feb 2011 36 Run time is reduced to O(n2) (Lennes, 1911) as follows. Pick the leftmost vertex v of P and connect its two neighbors u and w. Checking whether uw is a diagonal takes O(n). If it is, the rest is (n-1)-vertex polygon. If uw is not a diagonal, get x, the farthest vertex from uw inside Δuvw. This takes O(n) time. vx is a diagonal dividing P into P1 and P2, having n total number of vertices. Recursive application of the above procedure consumes total O(n2) time. Can we do better? Feb 2011 37 There is a type of polygons whose triangulation takes linear time, called monotone. Definition: P is monotone w.r.t to a line l if P intersects with any line l’ perpendicular to l in a single segment, a point or it doesn’t intersect. l’ l’ l l’ l’ not monotone monotone Feb 2011 l’ l’ l’ l’ 38 We’ll partition P into y-monotone pieces. start vertex end vertex merge vertex P split vertex regular vertex On horizontal edge the left vertex is considered above the right one. Feb 2011 39 Lemma: A polygon is y-monotone if it has neither split vertices nor merge vertices. Proof: If P is not monotone, there must exist a line l intersecting P in more than a single segment. Let [p,q] be its leftmost sub segment. split vertex p Feb 2011 Follow the boundary of P starting at q, where P is on the left. At some point r we must cross l. P q r l If r ≠ p then the highest vertex must be a split one. 40 If r = p we follow the boundary from q in opposite direction. P pr merge vertex Feb 2011 q r l At some point r’ we must cross l. r’ ≠ p as otherwise it contradicts that P is not monotone. This implies that the lowest encountered vertex must be a merge one. ■ 41 Getting Rid of Split and Merge Vertices Sort P’s vertices from top to bottom. It takes O(nlogn) time. P Apply scan line from top to bottom to encounter vertices. Diagonals are introduced at split and merge vertices. Feb 2011 42 Scan line intersection with P’s boundary is stored in a left to right ordered balanced binary tree. P Insertion into and deletion from a tree takes O(logn) time. Neighbor query takes O(1) time. Total time consumed by scanning is O(nlogn) time. Feb 2011 43 At a split point we get the nearest left and right edges in O(1) time. The last upper vertex is also stored (can be the top end of right or left edge). P This data suffices to decide on diagonal in O(1) time. Merge vertices are handled similarly. It takes O(nlogn) time to split P into monotone polygons. Feb 2011 44 Lemma : The added diagonals are non intersecting and partition P into monotone polygons. Proof : The resulting partitions contain neither split nor merge vertices, hence are monotone polygons. To see that the added diagonals are valid, we'll show that an added segment niether intersects P's edges nor previously added diagonals. We'll consider a split vertex. Merge vertex is similar. Feb 2011 45 vm When vm vi was added it couldn't intersect P's edge. Q ej vi ek If it intersects, there are two cases. If an end vertex of P's edge is inside Q it contradics vm is nearest to vi . Otherwise, the edge must cross Q, contrudiction to e j and ek being the nearest left and right vi 's edges. Feb 2011 46 Finally, a previously added diagonal must have its end vertices above vi . We've showed that Q cannot have any vertex of P, hence intersection with vm vi is impossible. ■ Theorem: An n-vertex simple polygon can be partitioned into y-monotone polygons in O(nlogn) time and O(n) storage. Feb 2011 47 Triangulating y-Monotone Polygon (Sketch) 1 2 3 P's vertices are sorted in descending y. 4 5 6 7 9 11 10 P Feb 2011 8 P's boundary is traversed with one leg on left and one leg on right, by poping and pushing vertices from a stack. Diagonals insertions are decided according to stack's top status. 48 Theorem: Strictly y-monotone n-vertex polygon can be triangulated in linear time. Theorem: (Gary et. al. 1978) A simple n-vertex polygon can be triangulated in O(nlogn) time and O(n) storage. The problem has been studied extensively between 1978 and 1991, when in 1991 Chazelle presented an O(n) time complexity algorithm. Feb 2011 49