Advertisement
LinKin

Polygon Triangulation

Jun 26th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. /*
  2.  * Algorithm : Polygon Triangulation
  3.  * Complexity : O( N^2 );
  4.  * Note : Prints out N-3 diagonals (as pairs of integer indices)
  5.  *        which form a triangulation of Poly of counterclockwise order.
  6.  */
  7. #include<stdio.h>
  8. #include<string.h>
  9. #include<math.h>
  10. #include<vector>
  11. using namespace std;
  12. struct POINT{
  13.     long x,y;
  14.     bool Ear;
  15.     long Next,Prev;
  16. };
  17. vector<POINT> Poly;
  18. /* Returns TRUE iff (a,b) is a proper internal *or* external
  19. diagonal of P, *ignoring edges incident to a and b*. */
  20. bool Diagonalie( POINT a,POINT b ){
  21.     long i;
  22.     /* For each edge (c,c1) of P */
  23.     for( i=0;i<Poly.size();i++ ){
  24.         /* Skip edges incident to a or b */
  25.         POINT c = Poly[i],c1 = Poly[(i+1)%Poly.size()];
  26.         /*Intersect( a, b,c,d ) return TRUE iff segments ab and cd intersect, properly or improperly.*/
  27.         if( (c!=a) and (c1!=a) and (c!=b) and (c1!=b) and Intersect( a,b,c,c1 )) return false;
  28.     }
  29.     return true;
  30. }
  31. /* Returns TRUE iff the diagonal (a,b) is strictly internal to the
  32. polygon in the neighborhood of the a endpoint. */
  33. bool InCone( POINT a,POINT b ){
  34.    POINT a0,a1; /* a0,a,a1 are consecutive vertices. */
  35.    a1 = Poly[a.Next];
  36.    a0 = Poly[a.Prev];
  37.    /* If a is a convex vertex ... */
  38.    if( Area2( a,a1,a0 )>=0 ) return Area2( a,b,a0 )>0 and Area2( b,a,a1 )>0;
  39.    /* Else a is reflex: */
  40.    else return !( Area2( a,b,a1 )>=0 and Area2( b,a,a0 )>=0 );
  41. }
  42. /* Returns TRUE iff (a,b) is a proper internal diagonal.*/
  43. bool Diagonal( POINT a,POINT b ){
  44.    return InCone( a,b ) and InCone( b,a ) and Diagonalie( a,b );
  45. }
  46. void Triangulate( void ){
  47.     long v0,v1,v2,v3,v4;/* Index of five consecutive vertices */
  48.     long i,N = Poly.size();
  49.     for( i=0;i<Poly.size();i++ ){
  50.         Poly[i].Next = (i+1)%N;
  51.         Poly[i].Prev = (i-1+N)%N;
  52.         Poly[i].Ear = Diagonal( Poly[Poly[i].Next],Poly[Poly[i].Prev] );
  53.     }
  54.     /* Each step of outer loop removes one Ear. */
  55.     long StartPoint = 0;
  56.     while( N > 3 ){    
  57.         /* Inner loop searches for an Ear. */
  58.         v2 = StartPoint;
  59.         do{
  60.             if( Poly[v2].Ear ){
  61.                 /* Ear found. Fill variables. */
  62.                 v3 = Poly[v2].Next;v4 = Poly[v3].Next;
  63.                 v1 = Poly[v2].Prev;v0 = Poly[v1].Prev;
  64.                 /* (v1,v3) is a diagonal */
  65.                 Poly[v1].Ear = Diagonal( Poly[v0], Poly[v3] );
  66.                 Poly[v3].Ear = Diagonal( Poly[v1], Poly[v4] );
  67.                 /* Cut off the Ear v2 */
  68.                 Poly[v1].Next = v3;
  69.                 Poly[v3].Prev = v1;
  70.                 StartPoint = v3;
  71.                 N--;
  72.                 break;
  73.             }/* end if Ear found */
  74.             v2 = Poly[v2].Next;
  75.         } while( v2 != StartPoint );
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement