/// <summary> /// Decompose the polygon into several smaller non-concave polygon. /// </summary> public static List <Vertices> ConvexPartition(Vertices vertices) { Debug.Assert(vertices.Count > 3); Polygon poly = new Polygon(); foreach (TSVector2 vertex in vertices) { poly.Points.Add(new TriangulationPoint(vertex.x, vertex.y)); } if (vertices.Holes != null) { foreach (Vertices holeVertices in vertices.Holes) { Polygon hole = new Polygon(); foreach (TSVector2 vertex in holeVertices) { hole.Points.Add(new TriangulationPoint(vertex.x, vertex.y)); } poly.AddHole(hole); } } DTSweepContext tcx = new DTSweepContext(); tcx.PrepareTriangulation(poly); DTSweep.Triangulate(tcx); List <Vertices> results = new List <Vertices>(); foreach (DelaunayTriangle triangle in poly.Triangles) { Vertices v = new Vertices(); foreach (TriangulationPoint p in triangle.Points) { v.Add(new TSVector2((FP)p.X, (FP)p.Y)); } results.Add(v); } return(results); }
public static List <Vertices> ConvexPartition(Vertices vertices) { Debug.Assert(vertices.Count > 3); Polygon polygon = new Polygon(); foreach (TSVector2 current in vertices) { polygon.Points.Add(new TriangulationPoint(current.x, current.y)); } bool flag = vertices.Holes != null; if (flag) { foreach (Vertices current2 in vertices.Holes) { Polygon polygon2 = new Polygon(); foreach (TSVector2 current3 in current2) { polygon2.Points.Add(new TriangulationPoint(current3.x, current3.y)); } polygon.AddHole(polygon2); } } DTSweepContext dTSweepContext = new DTSweepContext(); dTSweepContext.PrepareTriangulation(polygon); DTSweep.Triangulate(dTSweepContext); List <Vertices> list = new List <Vertices>(); foreach (DelaunayTriangle current4 in polygon.Triangles) { Vertices vertices2 = new Vertices(); foreach (TriangulationPoint current5 in current4.Points) { vertices2.Add(new TSVector2(current5.X, current5.Y)); } list.Add(vertices2); } return(list); }