Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.504 Advanced Algorithms Computational Geometry Prof. Karen Daniels Spring, 2007 Lecture 1 Start of Part I Material O’Rourke.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.504 Advanced Algorithms Computational Geometry Prof. Karen Daniels Spring, 2007 Lecture 1 Start of Part I Material O’Rourke."— Presentation transcript:

1 UMass Lowell Computer Science 91.504 Advanced Algorithms Computational Geometry Prof. Karen Daniels Spring, 2007 Lecture 1 Start of Part I Material O’Rourke Chapter 1

2 Part I O’Rourke Chapter 1 Polygon Triangulation: Art Gallery Theorems Triangulation: Theory Area of Polygon Implementation Issues Segment Intersection Triangulation: Implementation

3 Art Gallery Theorems ä Polygons ä The Art Gallery Theorem ä Fiske’s Proof of Sufficiency

4 Art Gallery Theorems: Polygons ä What is a polygon? ä “region of a plane bounded by a finite collection of line segments forming a simple closed curve” v0v0v0v0 v1v1v1v1 v2v2v2v2 v3v3v3v3 v4v4v4v4 e0e0e0e0 e1e1e1e1 e2e2e2e2 e3e3e3e3 e4e4e4e4 n=5 convex nonconvex simplenonsimple P

5 Art Gallery Theorems: The Theorem ä How many stationary guards are needed to guard the room?  Visibility: 2  range ä Empirically: ä n is sufficient (2D) ä n necessary for small n? 3? 4? 5? ä Guess: in general n/3 necessary Find G(n) = the maximum (over all n-vertex polygons) of the minimum number of guards needed to cover the polygon.

6 Art Gallery Theorems: Fiske’s Proof ä Given arbitrary n-vertex P: ä Triangulate P using diagonals ä Form triangulation graph G ä G can be 3-colored (proof later...) ä Place a guard at each same-colored vertex of G ä This covers all of P ä By pigeon-hole principle: n/3 guards Diagonal: line segment between 2 vertices that are clearly visible to each other (no grazing contact)

7 Triangulation Theory ä Existence of a Diagonal ä Properties of Triangulations ä Triangulation Dual ä 3-Coloring Proof

8 Triangulation Theory: Existence of a Diagonal ä Step 1: Every polygon must have >= 1 strictly convex vertex (no collinearity) ä Step 2: Every polygon of n >= 4 vertices has a diagonal ä Conclusion: Every n-vertex polygon P may be partitioned into triangles by adding (>=0) diagonals [proof by induction using diagonals]

9 Triangulation Theory: Properties ä Every triangulation of an n-vertex polygon P uses n-3 diagonals and consists of n-2 triangles.  The sum of the internal angles of an n- vertex polygon is (n-2) . 9 vertices 7 triangles 6 diagonals

10 Triangulation Theory: Duality ä The dual of a triangulation is a tree with each node of degree at most 3. ä “2-Ears” Theorem: every polygon of n >= 4 vertices has at least 2 nonoverlapping ears. ear ear

11 Triangulation Theory: 3-Coloring ä “2-Ears” Theorem can be used to easily prove 3-colorability of triangulation graphs ä Induction on n ä Base case: n = 3 ä For n >= 4: ä 2-ears theorem guarantees that an ear abc exists ä apply inductive hypothesis to polygon P’ without ear ä “reattaching” ear adds back in one vertex (w.l.o.g. b) ä color b whatever color a and c don’t use ä result is a 3-coloring of P a b c

12 A Triangulation Approach Algorithm: TRIANGULATION Initialize the ear tip status of each vertex. while n > 3 do Locate an ear tip v 2. Locate an ear tip v 2. Output diagonal v 1 v 3. Delete v 2. Update the ear tip status of v 1 and v 3. v1v2v3 TriangulateEarInit Diagonal InCone Diagonalie Intersect LeftOn Area2 Left Xor Collinear Between IntersectProp

13 Area of Polygon ä Cross Product ä Determinant Form ä Area of a Convex Polygon ä Area of a Convex Quadrilateral ä Area of a Nonconvex Quadrilateral ä Area from an Arbitrary Center ä Volume in >= 3 Dimensions

14 Area of Polygon: Cross Product or Determinant 0 for 2D a bcA B T or...

15 Area of Polygon: Convex Quadrilateral ä Could find area of polygon via triangulation...but we want to avoid this! ä To see how, first consider triangulation for 2 easy polygon cases: Convex Convex Quadrilateral v0v0v0v0 v1v1v1v1 v2v2v2v2 v3v3v3v3 v4v4v4v4 c a b d a c d b

16 Area of Polygon: (continued) ä Nonconvex Quadrilateral ä Arbitrary Center ca b d negative area T c a b p1 p2 more efficient

17 Area of Polygon: Volume ä Determinant area formulation extends to higher dimensions. ä In 3D: y a=(1,0,0) b=(0,1,0) c=(0,0,1) d=(0,0,0) xz

18 Implementation Issues ä Representation of a Point ä Representation of a Polygon ä Code for Area Text describes C code. C or Java code available from text’s Web site

19 Implementation Issues: Point C code #defineX 0 #defineY 1 typedefenum { FALSE, TRUE } bool; #defineDIM 2 /* Dimension of points */ typedefint tPointi[DIM]; /* Type integer point */ X is 0 th element of point; Y is 1st integers to minimize roundoff errors (but overflow is possible…) uppercase constants lowercase type identifier i for integer

20 Implementation Issues: Polygon doubly linked circular list for easy vertex deletion/insertion typedefstruct tVertexStructure tsVertex; /* Used only in NEW(). */ typedeftsVertex *tVertex; structtVertexStructure { intvnum;/* Index */ tPointiv;/* Coordinates */ bool ear;/* TRUE iff an ear */ tVertex next,prev; }; /* Global variable definitions */ tVertexvertices = NULL;/* "Head" of circular list. */ intnvertices = 0;/* Total number of polygon vertices. */

21 Implementation Issues: Area Code /*--------------------------------------------------------------------- Returns twice the signed area of the triangle determined by a,b,c. The area is positive if a,b,c are oriented ccw, negative if cw,and zero if the points are collinear. ---------------------------------------------------------------------*/ int Area2( tPointi a, tPointi b, tPointi c ) { return (b[X] - a[X]) * (c[Y] - a[Y]) - return (b[X] - a[X]) * (c[Y] - a[Y]) - (c[X] - a[X]) * (b[Y] - a[Y]); } minimizes number of multiplications to help avoid overflow

22 Segment Intersection ä Diagonals: key triangulation step ä Left ä Boolean Intersection ä Segment Intersection Code Infinite slopes are problematic, so avoid slopes!

23 Segment Intersection: Diagonals Diagonal: line segment between 2 vertices that are clearly visible to each other that are clearly visible to each other (no grazing contact) (no grazing contact) which one is a legal diagonal?

24 Segment Intersection: Left /*--------------------------------------------------------------------- Returns true iff c is strictly to the left of the directed line through a to b. ---------------------------------------------------------------------*/ boolLeft( tPointi a, tPointi b, tPointi c ) { return AreaSign( a, b, c ) > 0; return AreaSign( a, b, c ) > 0;} a bc returns sign (+,0,-) of area instead of area value itself

25 Segment Intersection: Boolean Intersection /*--------------------------------------------------------------------- Returns true iff ab properly intersects cd: they share a point interior to both segments. The properness of the intersection is ensured by using strict leftness. ---------------------------------------------------------------------*/ boolIntersectProp( tPointi a, tPointi b, tPointi c, tPointi d ) { /* Eliminate improper cases. */ /* Eliminate improper cases. */ if ( Collinear(a,b,c) || Collinear(a,b,d) || Collinear(c,d,a) || Collinear(c,d,b)) if ( Collinear(a,b,c) || Collinear(a,b,d) || Collinear(c,d,a) || Collinear(c,d,b)) return FALSE; return FALSE; return Xor( Left(a,b,c), Left(a,b,d) ) && Xor( Left(c,d,a), Left(c,d,b) ); return Xor( Left(a,b,c), Left(a,b,d) ) && Xor( Left(c,d,a), Left(c,d,b) );} a bcd a b c d acd b which are “proper” intersections?

26 Segment Intersection: Intersection Code /*--------------------------------------------------------------------- Returns TRUE iff segments ab and cd intersect, properly or improperly. ---------------------------------------------------------------------*/ boolIntersect( tPointi a, tPointi b, tPointi c, tPointi d ) { if ( IntersectProp( a, b, c, d ) ) if ( IntersectProp( a, b, c, d ) ) return TRUE; else if (Between( a, b, c ) || Between( a, b, d ) else if (Between( a, b, c ) || Between( a, b, d ) || Between( c, d, a ) || Between( c, d, b )) || Between( c, d, a ) || Between( c, d, b )) return TRUE; else else return FALSE; }

27 Triangulation: Implementation ä Diagonals (Internal or External) ä InCone (distinguish Internal from External) ä Triangulation by Ear Removal ä Analysis Algorithm: TRIANGULATION Initialize the ear tip status of each vertex. while n > 3 do Locate an ear tip v 2. Locate an ear tip v 2. Output diagonal v 1 v 3. Delete v 2. Update the ear tip status of v 1 and v 3. v1v2v3

28 Triangulation: Implementation Diagonals (Internal or External) /*--------------------------------------------------------------------- Returns TRUE iff (a,b) is a proper internal *or* external diagonal of P, *ignoring edges incident to a and b*. ---------------------------------------------------------------------*/ bool Diagonalie( tVertex a, tVertex b ) { tVertex c, c1; tVertex c, c1; /* For each edge (c,c1) of P */ /* For each edge (c,c1) of P */ c = vertices; c = vertices; do { c1 = c->next; /* Skip edges incident to a or b */ do { c1 = c->next; /* Skip edges incident to a or b */ if ( ( c != a ) && ( c1 != a ) && ( c != b ) && ( c1 != b ) && Intersect( a->v, b->v, c->v, c1->v ) ) && Intersect( a->v, b->v, c->v, c1->v ) ) return FALSE; return FALSE; c = c->next; } while ( c != vertices ); } while ( c != vertices ); return TRUE; return TRUE;}

29 Triangulation: Implementation InCone /*--------------------------------------------------------------------- Returns TRUE iff the diagonal (a,b) is strictly internal to the polygon in the neighborhood of the a endpoint. ---------------------------------------------------------------------*/ bool InCone( tVertex a, tVertex b ) { tVertex a0,a1;/* a0,a,a1 are consecutive vertices. */ tVertex a0,a1;/* a0,a,a1 are consecutive vertices. */ a1 = a->next; a1 = a->next; a0 = a->prev; a0 = a->prev; /* If a is a convex vertex... */ /* If a is a convex vertex... */ if( LeftOn( a->v, a1->v, a0->v ) ) if( LeftOn( a->v, a1->v, a0->v ) ) return Left( a->v, b->v, a0->v ) && Left( b->v, a->v, a1->v ); /* Else a is reflex: */ /* Else a is reflex: */ return !( LeftOn( a->v, b->v, a1->v ) && LeftOn( b->v, a->v, a0->v ) ); return !( LeftOn( a->v, b->v, a1->v ) && LeftOn( b->v, a->v, a0->v ) );}

30 Triangulation: Implementation /* Prints out n-3 diagonals (as pairs of integer indices) which form a triangulation of P*/ void Triangulate( void ){ tVertex v0, v1, v2, v3, v4;/* five consecutive vertices */ tVertex v0, v1, v2, v3, v4;/* five consecutive vertices */ int n = nvertices;/* number of vertices; shrinks to 3. */ int n = nvertices;/* number of vertices; shrinks to 3. */ EarInit(); EarInit(); while ( n > 3 ) { /* Inner loop searches for an ear and removes it. */ while ( n > 3 ) { /* Inner loop searches for an ear and removes it. */ v2 = vertices; do { if (v2->ear) { /* Ear found. */ v3 = v2->next; v4 = v3->next; v1 = v2->prev; v0 = v1->prev; PrintDiagonal( v1, v3 ); v1->ear = Diagonal( v0, v3 ); v3->ear = Diagonal( v1, v4 ); /* Update earity*/ v1->next = v3; v3->prev = v1; /* Cut off ear. */ vertices = v3;/* In case the head was v2. */ n--; n--; break; /* out of inner loop; resume outer loop */ break; /* out of inner loop; resume outer loop */ } /* end if ear found */ } /* end if ear found */ v2 = v2->next; v2 = v2->next; } while ( v2 != vertices ); } while ( v2 != vertices ); } /* end outer while loop */ } /* end outer while loop */ } /* end Triangulate */

31 Triangulation: Implementation Analysis O(n 2 ) + + Naïve Analysis Tighter Analysis O(n)iterations Algorithm: TRIANGULATION Initialize the ear tip status of each vertex. while n > 3 do Locate an ear tip v 2. Locate an ear tip v 2. Output diagonal v 1 v 3. Delete v 2. Update the ear tip status of v 1 and v 3. O(n) O(n)iterations O(n)iterations O(n) Total: O(n 3 ) Total: O(n 2 ) Triangulation can be done in O(nlgn) time with different approach.


Download ppt "UMass Lowell Computer Science 91.504 Advanced Algorithms Computational Geometry Prof. Karen Daniels Spring, 2007 Lecture 1 Start of Part I Material O’Rourke."

Similar presentations


Ads by Google