Exemplo n.º 1
0
 // find an ear (always a triangle) of the polygon and return the index of the middle (second) vertex in the ear
 public static int FindEar(Polygon poly)
 {
     for (int i = 0; i < poly.PtList.Count - 2; i++)
     {
         if (poly.VertexType(i + 1) == PolygonType.Convex)
         {
             // get the three points of the triangle we are about to test
             PointF a = poly.PtList[i];
             PointF b = poly.PtList[i + 1];
             PointF c = poly.PtList[i + 2];
             bool foundAPointInTheTriangle = false;  // see if any of the other points in the polygon are in this triangle
             for (int j = 0; j < poly.PtListOpen.Count; j++)  // don't check the last point, which is a duplicate of the first
             {
                 if (j != i && j != i+1 && j != i+2 && PointInTriangle(poly.PtList[j], a, b, c)) foundAPointInTheTriangle = true;
             }
             if (!foundAPointInTheTriangle)  // the middle point of this triangle is convex and none of the other points in the polygon are in this triangle, so it is an ear
                 return i + 1;  // EXITING HERE!
         }
     }
     throw new ApplicationException("Improperly formed polygon");
 }