ContainsVertex() приватный Метод

private ContainsVertex ( Vector3D, p ) : bool
p Vector3D,
Результат bool
    // Bowyer-Watson algorithm
    public IList <Triangle> Triangulate(Vector3[] pointList)
    {
        List <Triangle> triangulation = new List <Triangle>();
        Triangle        superTriangle = CreateBoundingTriangle(pointList);

        triangulation.Add(superTriangle);
        for (int i = 0; i < pointList.Length; i++)
        {
            List <Triangle> badTriangles = new List <Triangle>();
            for (int j = 0; j < triangulation.Count; j++)
            {
                Triangle triangle = triangulation[j];

                if (triangle.IsInsideCircumcircle(pointList[i]))
                {
                    badTriangles.Add(triangle);
                }
            }
            ICollection <Edge> polygon = new HashSet <Edge>();

            if (badTriangles.Count == 1)
            {
                Triangle     triangle = badTriangles[0];
                IList <Edge> edges    = triangle.GetEdges();
                foreach (Edge edge in edges)
                {
                    polygon.Add(edge);
                }
            }
            for (int j = 0; j < badTriangles.Count; j++)
            {
                Triangle triangle = badTriangles[j];
                foreach (Edge edge in triangle.GetEdges())
                {
                    ICollection <Triangle> triangleList = new HashSet <Triangle>(badTriangles);
                    triangleList.Remove(triangle);
                    if (edge.IsEdgeUnshared(triangleList))
                    {
                        polygon.Add(edge);
                    }
                }
            }
            //remove each triangle in badTriangles from triangulation
            foreach (Triangle triangle in badTriangles)
            {
                //remove triangle from triangulation
                triangulation.Remove(triangle);
            }
            foreach (Edge edge in polygon)
            {
                Triangle newTri = new Triangle(pointList[i], edge.A, edge.B);
                if (newTri.IsValid() && !triangulation.Contains(newTri))
                {
                    triangulation.Add(newTri);
                }
            }
        }
        triangulation = triangulation.FindAll(triangle => triangle.ContainsVertex(pointList));
        return(triangulation);
    }
Пример #2
0
    public List <Triangle> Triangulate(List <Vector2> vertices)
    {
        List <Triangle> triangles = new List <Triangle>();

        if (0 == vertices.Count)
        {
            return(triangles);
        }
        float minX = vertices[0].x;
        float minY = vertices[0].y;
        float maxX = minX;
        float maxY = minY;

        for (int i = 0, imax = vertices.Count; i < imax; i++)
        {
            float x = vertices[i].x;
            float y = vertices[i].y;
            if (x < minX)
            {
                minX = x;
            }
            else if (x > maxX)
            {
                maxX = x;
            }
            if (y < minY)
            {
                minY = y;
            }
            else if (y > maxY)
            {
                maxY = y;
            }
        }
        float   dx       = maxX - minX;
        float   dy       = maxY - minY;
        float   deltaMax = Mathf.Max(dx, dy);
        float   midX     = (minX + maxX) / 2;
        float   midY     = (minY + maxY) / 2;
        Vector2 p1       = new Vector2(midX - 20 * deltaMax, midY - deltaMax);
        Vector2 p2       = new Vector2(midX, midY + 20 * deltaMax);
        Vector2 p3       = new Vector2(midX + 20 * deltaMax, midY - deltaMax);

        triangles.Add(new Triangle(p1, p2, p3));
        for (int i = 0, imax = vertices.Count; i < imax; i++)
        {
            Vector2     p       = vertices[i];
            List <Edge> polygon = new List <Edge>();
            for (int j = 0, jmax = triangles.Count; j < jmax; j++)
            {
                Triangle t = triangles[j];
                if (t.circumCircleContains(p))
                {
                    t.IsBad = true;
                    polygon.Add(t.E0);
                    polygon.Add(t.E1);
                    polygon.Add(t.E2);
                    triangles[j] = t;
                }
            }
            triangles.RemoveAll((t) => t.IsBad);
            for (int j = 0, jmax = polygon.Count; j < jmax; j++)
            {
                Edge e = polygon[j];
                for (int k = j + 1; k < jmax; k++)
                {
                    Edge e2 = polygon[k];
                    if (e.AlmostEqual(e2))
                    {
                        e.IsBad    = true;
                        e2.IsBad   = true;
                        polygon[j] = e;
                        polygon[k] = e2;
                    }
                }
            }

            polygon.RemoveAll((e) => e.IsBad);
            foreach (Edge e in polygon)
            {
                triangles.Add(new Triangle(e.V0, e.V1, p));
            }
        }
        triangles.RemoveAll((t) => t.ContainsVertex(p1) || t.ContainsVertex(p2) || t.ContainsVertex(p3));
        return(triangles);
    }