Exemplo n.º 1
0
        // Triangulate the polygon.
        //
        // For a nice, detailed explanation of this method,
        // see Ian Garton's Web page:
        // http://www-cgrl.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian/cutting_ears.html
        static List <int> Triangulate(this PolygonShape p_polygon)
        {
            List <int> v_indexes = new List <int>();

            if (p_polygon.Vertices.Count > 2)
            {
                // Copy the points into a scratch array.
                Vector2[] v_pts = p_polygon.Vertices.ToArray();

                // Make a scratch polygon.
                MorphableShape v_pgon = new MorphableShape(v_pts);

                // Orient the polygon Clockwise
                if (!v_pgon.IsOrientedClockwise())
                {
                    v_pgon.ReverseOrientation();
                }

                // Make room for the triangles.
                List <MorphableShape> v_triangles = new List <MorphableShape>();

                // While the copy of the polygon has more than
                // three points, remove an ear.
                while (v_pgon.Vertices.Count > 3)
                {
                    // Remove an ear from the polygon.
                    v_pgon.RemoveEar(v_triangles);
                }

                // Copy the last three points into their own triangle.
                v_triangles.Add(new MorphableShape(new List <Vector2>()
                {
                    v_pgon.Vertices[0], v_pgon.Vertices[1], v_pgon.Vertices[2]
                }));

                //Find Index of vertice of each triangle in self Vertice List
                foreach (var v_triangle in v_triangles)
                {
                    if (!v_triangle.IsOrientedClockwise())
                    {
                        v_triangle.ReverseOrientation();
                    }
                    foreach (var v_vertice in v_triangle.Vertices)
                    {
                        for (int i = 0; i < p_polygon.Vertices.Count; i++)
                        {
                            var v_thisVertice = p_polygon.Vertices[i];
                            if (v_thisVertice == v_vertice)
                            {
                                v_indexes.Add(i);
                            }
                        }
                    }
                }
            }

            return(v_indexes);
        }
Exemplo n.º 2
0
            // Return true if the three points form an ear.
            public bool FormsEar(int p_a, int p_b, int p_c)
            {
                if (Vertices != null)
                {
                    // See if the angle ABC is concave.
                    if (GetAngle(Vertices[p_a], Vertices[p_b], Vertices[p_c]) > 0)
                    {
                        // This is a concave corner so the triangle
                        // cannot be an ear.
                        return(false);
                    }

                    // Make the triangle A, B, C.
                    MorphableShape v_triangle = new MorphableShape(new List <Vector2>()
                    {
                        Vertices[p_a], Vertices[p_b], Vertices[p_c]
                    });

                    // Check the other points to see
                    // if they lie in triangle A, B, C.
                    for (int i = 0; i < Vertices.Count; i++)
                    {
                        if ((i != p_a) && (i != p_b) && (i != p_c))
                        {
                            if (v_triangle.PointInShape(Vertices[i]))
                            {
                                // This point is in the triangle
                                // do this is not an ear.
                                return(false);
                            }
                        }
                    }
                    // This is an ear.
                    return(true);
                }
                return(false);
            }