public static List<Vertices> ConvexPartition(Vertices vertices) { Polygon poly = new Polygon(); foreach (Vector2 vertex in vertices) { poly.Points.Add(new TriangulationPoint(vertex.X, vertex.Y)); } 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 Vector2((float)p.X, (float)p.Y)); } results.Add(v); } return results; }
/// <summary> /// Decompose the polygon into several smaller non-concave polygon. /// </summary> public static List <List <Vector2> > ConvexPartition(List <Vector2> vertices, List <List <Vector2> > holes = null) { FarseerPhysics.Common.Decomposition.CDT.Polygon.Polygon poly = new FarseerPhysics.Common.Decomposition.CDT.Polygon.Polygon(); foreach (Vector2 vertex in vertices) { poly.Points.Add(new TriangulationPoint(vertex.x, vertex.y)); } if (holes != null) { foreach (List <Vector2> holeVertices in holes) { FarseerPhysics.Common.Decomposition.CDT.Polygon.Polygon hole = new FarseerPhysics.Common.Decomposition.CDT.Polygon.Polygon(); foreach (Vector2 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 <List <Vector2> > results = new List <List <Vector2> >(); foreach (DelaunayTriangle triangle in poly.Triangles) { List <Vector2> v = new List <Vector2>(); foreach (TriangulationPoint p in triangle.Points) { v.Add(new Vector2((float)p.X, (float)p.Y)); } results.Add(v); } return(results); }
/// <summary> /// Decompose the polygon into several smaller non-concave polygon. /// </summary> public static List<Vertices> ConvexPartition(Vertices vertices) { if (vertices.Count <= 3) return new List<Vertices> { vertices }; Polygon poly = new Polygon(); foreach (Vector2 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 (Vector2 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 Vector2((float)p.X, (float)p.Y)); } results.Add(v); } return results; }
public void Add(Polygon p) { _polygons.Add(p); }
public PolygonSet(Polygon poly) { _polygons.Add(poly); }
/// <summary> /// Add a hole to the polygon. /// </summary> /// <param name="poly">A subtraction polygon fully contained inside this polygon.</param> public void AddHole( Polygon poly ) { if( _holes == null ) _holes = new List<Polygon>(); _holes.Add( poly ); // XXX: tests could be made here to be sure it is fully inside // addSubtraction( poly.getPoints() ); }