FarseerPhysics.Common.Decomposition.SeidelDecomposer.ConvexPartition C# (CSharp) Method

ConvexPartition() public static method

Decompose the polygon into several smaller non-concave polygons.
public static ConvexPartition ( Vertices vertices, float sheer = 0.001f ) : List
vertices Vertices The polygon to decompose.
sheer float The sheer to use if you get bad results, try using a higher value.
return List
        public static List<Vertices> ConvexPartition(Vertices vertices, float sheer = 0.001f)
        {
            Debug.Assert(vertices.Count > 3);

            List<Point> compatList = new List<Point>(vertices.Count);

            foreach (Vector2 vertex in vertices)
            {
                compatList.Add(new Point(vertex.X, vertex.Y));
            }

            Triangulator t = new Triangulator(compatList, sheer);

            List<Vertices> list = new List<Vertices>();

            foreach (List<Point> triangle in t.Triangles)
            {
                Vertices outTriangles = new Vertices(triangle.Count);

                foreach (Point outTriangle in triangle)
                {
                    outTriangles.Add(new Vector2(outTriangle.X, outTriangle.Y));
                }

                list.Add(outTriangles);
            }

            return list;
        }

Usage Example

Example #1
0
        public static List <Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid = true, float tolerance = 0.001f)
        {
            if (vertices.Count <= 3)
            {
                return new List <Vertices> {
                           vertices
                }
            }
            ;

            List <Vertices> results;

            switch (algorithm)
            {
            case TriangulationAlgorithm.Earclip:
                if (vertices.IsCounterClockWise())
                {
                    Vertices temp = new Vertices(vertices);
                    temp.Reverse();
                    results = EarclipDecomposer.ConvexPartition(temp, tolerance);
                }
                else
                {
                    results = EarclipDecomposer.ConvexPartition(vertices, tolerance);
                }
                break;

            case TriangulationAlgorithm.Bayazit:
                if (!vertices.IsCounterClockWise())
                {
                    Vertices temp = new Vertices(vertices);
                    temp.Reverse();
                    results = BayazitDecomposer.ConvexPartition(temp);
                }
                else
                {
                    results = BayazitDecomposer.ConvexPartition(vertices);
                }
                break;

            case TriangulationAlgorithm.Flipcode:
                if (!vertices.IsCounterClockWise())
                {
                    Vertices temp = new Vertices(vertices);
                    temp.Reverse();
                    results = FlipcodeDecomposer.ConvexPartition(temp);
                }
                else
                {
                    results = FlipcodeDecomposer.ConvexPartition(vertices);
                }
                break;

            case TriangulationAlgorithm.Seidel:
                results = SeidelDecomposer.ConvexPartition(vertices, tolerance);
                break;

            case TriangulationAlgorithm.SeidelTrapezoids:
                results = SeidelDecomposer.ConvexPartitionTrapezoid(vertices, tolerance);
                break;

            case TriangulationAlgorithm.Delauny:
                results = CDTDecomposer.ConvexPartition(vertices);
                break;

            default:
                throw new ArgumentOutOfRangeException("algorithm");
            }

            if (discardAndFixInvalid)
            {
                for (int i = results.Count - 1; i >= 0; i--)
                {
                    Vertices polygon = results[i];

                    if (!ValidatePolygon(polygon))
                    {
                        results.RemoveAt(i);
                    }
                }
            }

            return(results);
        }
All Usage Examples Of FarseerPhysics.Common.Decomposition.SeidelDecomposer::ConvexPartition