Esempio n. 1
0
        public bool IsAdjacent(TriangleF other)
        {
            PointF v1 = Vertices[0], v2 = Vertices[1], v3 = Vertices[2];
            PointF o1 = other.Vertices[0], o2 = other.Vertices[1], o3 = other.Vertices[2];

            bool alreadyMatchedOne = v1 == o1 || v1 == o2 || v1 == o3;

            if (v2 == o1 || v2 == o2 || v2 == o3)
            {
                if (alreadyMatchedOne)
                {
                    return(true);
                }

                alreadyMatchedOne = true;
            }


            if (alreadyMatchedOne && (v3 == o1 || v3 == o2 || v3 == o3))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        private bool InsideCircumcircle(PointF point, TriangleF triangle)
        {
            var distSq = point.DistanceSqTo(triangle.CircumCenter);

            return(distSq <= triangle.CircumRadiusSq);
        }
Esempio n. 3
0
        private List <TriangleF> GetDelauneyTriangulation(List <PointF> points)
        {
            var enclosingTriangle = new TriangleF(
                new PointF(0, 0),
                new PointF(Width * 2, 0),
                new PointF(0, Height * 2)
                );

            var triangulation = new List <TriangleF>();

            triangulation.Add(enclosingTriangle);

            foreach (var point in points)
            {
                // find all triangles that are no longer valid due to this node's insertion
                var badTriangles = new List <TriangleF>();
                foreach (var triangle in triangulation)
                {
                    if (InsideCircumcircle(point, triangle))
                    {
                        badTriangles.Add(triangle);
                    }
                }

                // Find the boundary of polygonal hole formed by these "bad" triangles...
                // Get the edges of the "bad" triangles which don't touch other bad triangles...
                // Each pair of nodes here represents a line.
                var polygon = new List <PointF>();
                foreach (var triangle in badTriangles)
                {
                    for (var i = 0; i < 3; i++)
                    {
                        var edgeFrom = triangle.Vertices[i];
                        var edgeTo   = triangle.Vertices[i == 2 ? 0 : i + 1];

                        var sharedWithOther = false;
                        foreach (var other in badTriangles)
                        {
                            if (other == triangle)
                            {
                                continue;
                            }

                            if (!other.Vertices.Contains(edgeFrom))
                            {
                                continue;
                            }

                            if (!other.Vertices.Contains(edgeTo))
                            {
                                continue;
                            }

                            sharedWithOther = true;
                            break;
                        }

                        if (!sharedWithOther)
                        {
                            polygon.Add(edgeFrom);
                            polygon.Add(edgeTo);
                        }
                    }
                }

                // discard all bad triangles
                foreach (var triangle in badTriangles)
                {
                    triangulation.Remove(triangle);
                }

                // re-triangulate the polygonal hole ... create a new triangle for each edge
                for (var i = 0; i < polygon.Count - 1; i += 2)
                {
                    var triangle = new TriangleF(polygon[i], polygon[i + 1], point);
                    triangulation.Add(triangle);
                }
            }

            // remove all triangles that contain a vertex from the original super-triangle
            for (var i = 0; i < triangulation.Count; i++)
            {
                var triangle = triangulation[i];
                foreach (var vertex in triangle.Vertices)
                {
                    if (enclosingTriangle.Vertices.Contains(vertex))
                    {
                        triangulation.RemoveAt(i);
                        i--;
                        break;
                    }
                }
            }

            return(triangulation);
        }