示例#1
0
    private static bool Snip(Vector2[] verts, int u, int v, int w, int n, int[] V)
    {
        int        p;
        Triangle2D tri = new Triangle2D(verts[V[u]], verts[V[v]], verts[V[w]]);

        if (Mathf.Epsilon > (((tri.b.x - tri.a.x) * (tri.c.y - tri.a.y)) - ((tri.b.y - tri.a.y) * (tri.c.x - tri.a.x))))
        {
            return(false);
        }
        for (p = 0; p < n; p++)
        {
            if ((p == u) || (p == v) || (p == w))
            {
                continue;
            }
            Vector2 P = verts[V[p]];
            if (tri.Contains(P))
            {
                return(false);
            }
        }
        return(true);
    }
    public void Update()
    {
        bool rlt = _triangle.Contains(Helper.GetVector2(_p));

        Debug.Log(rlt);
    }
示例#3
0
 public bool Intersects()
 {
     return(_triangle.Contains(_point.Vector));
 }
示例#4
0
    // !!! Does not work currently !!!
    public IEnumerable <Triangle2D> Without(Triangle2D triangle)
    {
        var points1 = this.ToEnumerable().ToArray();
        var points2 = triangle.ToEnumerable().ToArray();

        var points = this.ToEnumerable().Concat(triangle.ToEnumerable()).ToList();

        var indices1 = new List <int>(new int[] { 0, 1, 2 });
        var indices2 = new List <int>(new int[] { 3, 4, 5 });

        // Find all intersections between edges
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (Math2d.CheckLinesIntersect(points1[i], points2[(i + 1) % 3], points2[j], points2[(j + 1) % 3]))
                {
                    var intersect = Math2d.LineLineIntersection(points1[i], points2[(i + 1) % 3], points2[j], points2[(j + 1) % 3]);
                    points.Add(intersect);
                    indices1.Add(points.Count - 1);
                    indices2.Add(points.Count - 1);
                }
            }
        }

        if (points.Count == 6)           // No intersections found
        {
            if (triangle.Contains(points1[0]))
            {
                // This triangle is contained in the other one, result is empty
                yield break;
            }
            else if (this.Contains(points2[0]))
            {
                // Other triangle is contained in this one
                // TODO
                yield break;
            }
            else
            {
                // Triangles don't touch each other
                yield return(this);

                yield break;
            }
        }

        // Make indices clockwise
        var center1 = (points1[0] + points1[1] + points1[2]) / 3.0f;
        var center2 = (points2[0] + points2[1] + points2[2]) / 3.0f;

        indices1.Sort((i, j) => getAngle(center1, points[i]).CompareTo(getAngle(center1, points[j])));
        indices2.Sort((i, j) => getAngle(center2, points[i]).CompareTo(getAngle(center2, points[j])));

        // Find polygons and triangulate
        var visited = new bool[3];

        for (int i = 0; i < 3; i++)
        {
            if (triangle.Contains(points[i]) || visited[i])
            {
                continue;
            }
            int indices1position = indices1.IndexOf(i);
            var polygon          = new List <int>();
            while (true)
            {
                indices1position = (indices1position + 1) % indices1.Count;
                int polygonIndex = indices1[indices1position];

                polygon.Add(polygonIndex);

                if (polygonIndex == i)
                {
                    break;
                }
                if (polygonIndex < 3)
                {
                    visited[polygonIndex] = true;
                }

                if (indices2.Contains(polygonIndex))
                {
                    int indices2position = indices2.IndexOf(polygonIndex);
                    while (true)
                    {
                        indices2position = (indices2position - 1 + indices2.Count) % indices2.Count;
                        polygonIndex     = indices2[indices2position];
                        polygon.Add(polygonIndex);

                        if (indices1.Contains(polygonIndex))
                        {
                            indices1position = indices1.IndexOf(polygonIndex);
                            break;
                        }
                    }
                }
            }

            foreach (var t in Triangle2D.Triangulate(polygon.Select(p => points[p]).ToArray()))
            {
                yield return(t);
            }
        }
    }