Exemplo n.º 1
0
        private int[] Triangulate()
        {
            foreach (var vertex in EnumerateVertex())
            {
                SetConvex(vertex);
                SetEar(vertex);
            }

            while (true)
            {
                if (Ears.LastOrDefault() is not LinkedListNode <Vertex> vertex)
                {
                    return(null);
                }

                var prev = vertex.GetPrevious();
                var next = vertex.GetNext();

                var triangle = Direction == PolygonDirection.ClockWise ? new Triangle(next.Value.Index, vertex.Value.Index, prev.Value.Index) : new Triangle(prev.Value.Index, vertex.Value.Index, next.Value.Index);
                Triangles.Add(triangle);
                Ears.Remove(vertex);
                Vertices.Remove(vertex);

                if (Vertices.Count < 3)
                {
                    return(Triangles.SelectMany(t => t.GetVertices(Direction)).ToArray());
                }

                SetConvex(prev);
                SetConvex(next);

                SetEar(prev);
                SetEar(next);
            }
        }
Exemplo n.º 2
0
        private void SetEar(LinkedListNode <Vertex> vertex)
        {
            if (vertex.Value.IsConvex)
            {
                var prev = vertex.GetPrevious();
                var next = vertex.GetNext();
                if (!EnumerateVertex(next, prev).Any(p => PointInTriangle(prev.Value.Position, vertex.Value.Position, next.Value.Position, p.Value.Position)))
                {
                    Ears.Add(vertex);
                    return;
                }
            }

            Ears.Remove(vertex);
        }