Exemplo n.º 1
0
        /// <summary>
        /// Determines the winding order of a polygon given a set of vertices.
        /// </summary>
        /// <param name="vertices">The vertices of the polygon.</param>
        /// <returns>The calculated winding order of the polygon.</returns>
        public static WindingOrder DetermineWindingOrder(TriangulatorVector2[] vertices)
        {
            int clockWiseCount        = 0;
            int counterClockWiseCount = 0;
            TriangulatorVector2 p1    = vertices[0];

            for (int i = 1; i < vertices.Length; i++)
            {
                TriangulatorVector2 p2 = vertices[i];
                TriangulatorVector2 p3 = vertices[(i + 1) % vertices.Length];

                TriangulatorVector2 e1 = p1 - p2;
                TriangulatorVector2 e2 = p3 - p2;

                if (e1.X * e2.Y - e1.Y * e2.X >= 0)
                {
                    clockWiseCount++;
                }
                else
                {
                    counterClockWiseCount++;
                }

                p1 = p2;
            }

            return(clockWiseCount > counterClockWiseCount
                                ? WindingOrder.Clockwise
                                : WindingOrder.CounterClockwise);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reverses the winding order for a set of vertices.
        /// </summary>
        /// <param name="vertices">The vertices of the polygon.</param>
        /// <returns>The new vertices for the polygon with the opposite winding order.</returns>
        public static TriangulatorVector2[] ReverseWindingOrder(TriangulatorVector2[] vertices)
        {
            Log("\nReversing winding order...");
            TriangulatorVector2[] newVerts = new TriangulatorVector2[vertices.Length];

#if DEBUG
            StringBuilder vString = new StringBuilder();
            foreach (TriangulatorVector2 v in vertices)
            {
                vString.Append(string.Format("{0}, ", v));
            }
            Log("Original Vertices: {0}", vString);
#endif

            newVerts[0] = vertices[0];
            for (int i = 1; i < newVerts.Length; i++)
            {
                newVerts[i] = vertices[vertices.Length - i];
            }

#if DEBUG
            vString = new StringBuilder();
            foreach (TriangulatorVector2 v in newVerts)
            {
                vString.Append(string.Format("{0}, ", v));
            }
            Log("New Vertices After Reversal: {0}\n", vString);
#endif

            return(newVerts);
        }
Exemplo n.º 3
0
        private static bool IsConvex(Vertex c)
        {
            Vertex p = polygonVertices[polygonVertices.IndexOf(c) - 1].Value;
            Vertex n = polygonVertices[polygonVertices.IndexOf(c) + 1].Value;

            TriangulatorVector2 d1 = TriangulatorVector2.Normalize(c.Position - p.Position);
            TriangulatorVector2 d2 = TriangulatorVector2.Normalize(n.Position - c.Position);
            TriangulatorVector2 n2 = new TriangulatorVector2(-d2.Y, d2.X);

            return(TriangulatorVector2.Dot(d1, n2) <= 0f);
        }