Пример #1
0
    static private Slice2D SliceWithTwoHoles(Polygon polygon, Pair2f slice, Polygon holeA, Polygon holeB)
    {
        Slice2D result = Slice2D.Create();

        if (holeA == holeB)
        {
            Debug.LogError("Slicer2D: Incorrect Split 2: Cannot Split Into Same Hole");
            return(result);
        }

        Polygon polyA = new Polygon();
        Polygon polyB = new Polygon(polygon.pointsList);

        polyA.AddPoints(VectorList2f.GetListStartingIntersectLine(holeA.pointsList, slice));
        polyA.AddPoints(VectorList2f.GetListStartingIntersectLine(holeB.pointsList, slice));

        foreach (Polygon poly in polygon.holesList)
        {
            if (poly != holeA && poly != holeB)
            {
                polyB.AddHole(poly);
            }
        }

        polyB.AddHole(polyA);

        result.AddPolygon(polyB);
        return(result);
    }
Пример #2
0
    static private Slice2D SliceWithTwoHoles(Polygon polygon, List <Vector2f> slice, List <Vector2f> collisionSlice)
    {
        Slice2D result = Slice2D.Create();

        Polygon polyA = new Polygon();
        Polygon polyB = new Polygon(polygon.pointsList);

        Polygon holeA = polygon.PointInHole(slice.First());
        Polygon holeB = polygon.PointInHole(slice.Last());

        if (holeA == null || holeB == null)
        {
            Debug.LogError("Slicer2D: ERROR Split");              // Shouldn't really happen
            return(result);
        }

        List <Vector2f> pointsA = VectorList2f.GetListStartingIntersectSlice(holeA.pointsList, slice);
        List <Vector2f> pointsB = VectorList2f.GetListStartingIntersectSlice(holeB.pointsList, slice);

        polyA.AddPoints(pointsA);

        if (collisionSlice.Count > 0)
        {
            if (Vector2f.Distance(pointsA.Last(), collisionSlice.Last()) < Vector2f.Distance(pointsA.Last(), collisionSlice.First()))
            {
                collisionSlice.Reverse();
            }

            polyA.AddPoints(collisionSlice);
        }

        polyA.AddPoints(pointsB);

        if (collisionSlice.Count > 0)
        {
            collisionSlice.Reverse();

            polyA.AddPoints(collisionSlice);
        }

        foreach (Polygon poly in polygon.holesList)
        {
            if (poly != holeA && poly != holeB)
            {
                polyB.AddHole(poly);
            }
        }

        polyB.AddHole(polyA);
        result.AddPolygon(polyB);

        return(result);
    }
Пример #3
0
    static private Slice2D SliceWithOneHole(Polygon polygon, Pair2f slice, Polygon holeA, Polygon holeB)
    {
        Slice2D result = Slice2D.Create();

        if (holeA == holeB)
        {
            Polygon polyA = new Polygon(polygon.pointsList);
            Polygon polyB = new Polygon();
            Polygon polyC = new Polygon();

            Polygon currentPoly = polyB;

            foreach (Pair2f pair in Pair2f.GetList(holeA.pointsList))
            {
                Vector2f point = MathHelper.GetPointLineIntersectLine(slice, pair);
                if (point != null)
                {
                    polyB.AddPoint(point);
                    polyC.AddPoint(point);
                    currentPoly = (currentPoly == polyB) ? polyC : polyB;
                }
                currentPoly.AddPoint(pair.B);
            }

            if (polyB.pointsList.Count > 2 && polyC.pointsList.Count > 2)
            {
                if (polyB.GetArea() > polyC.GetArea())
                {
                    polyA.AddHole(polyB);
                    result.AddPolygon(polyC);
                }
                else
                {
                    result.AddPolygon(polyB);
                    polyA.AddHole(polyC);
                }

                result.AddPolygon(polyA);
            }

            return(result);
            // Cross From Side To Polygon
        }
        else if (polygon.PointInPoly(slice.A) == false || polygon.PointInPoly(slice.B) == false)
        {
            Polygon holePoly = (holeA != null) ? holeA : holeB;

            if (holePoly != null)
            {
                Polygon polyA = new Polygon();
                Polygon polyB = new Polygon(holePoly.pointsList);

                polyB.pointsList.Reverse();

                polyA.AddPoints(VectorList2f.GetListStartingIntersectLine(polygon.pointsList, slice));
                polyA.AddPoints(VectorList2f.GetListStartingIntersectLine(polyB.pointsList, slice));

                foreach (Polygon poly in polygon.holesList)
                {
                    if (poly != holePoly)
                    {
                        polyA.AddHole(poly);
                    }
                }

                result.AddPolygon(polyA);
                return(result);
            }
        }
        return(result);
    }
Пример #4
0
    static private Slice2D SliceWithoutHoles(Polygon polygon, List <Vector2f> slice, List <Vector2f> collisionSlice)
    {
        Slice2D result = Slice2D.Create();

        // Simple non-hole slice
        Polygon polyA = new Polygon();
        Polygon polyB = new Polygon();

        Polygon currentPoly = polyA;

        foreach (Pair2f p in Pair2f.GetList(polygon.pointsList))
        {
            List <Vector2f> intersections = MathHelper.GetListLineIntersectSlice(p, slice);
            Vector2f        intersection  = null;
            if (intersections.Count() > 0)
            {
                intersection = intersections.First();
            }

            if (intersections.Count > 0)               // Only if 1 or 2
            {
                if (intersections.Count == 2)
                {
                    Vector2f first = intersections.First();
                    Vector2f last  = intersections.Last();

                    if (Vector2f.Distance(last, p.A) < Vector2f.Distance(first, p.A))
                    {
                        first = intersections.Last();
                        last  = intersections.First();
                    }

                    currentPoly.AddPoint(first);

                    if (collisionSlice.Count > 0)
                    {
                        if (Vector2f.Distance(first, collisionSlice.Last()) < Vector2f.Distance(first, collisionSlice.First()))
                        {
                            collisionSlice.Reverse();
                        }

                        currentPoly.AddPoints(collisionSlice);
                    }

                    currentPoly.AddPoint(last);
                    currentPoly.AddPoint(p.B);

                    currentPoly = polyB;

                    if (collisionSlice.Count > 0)
                    {
                        currentPoly.AddPoints(collisionSlice);
                    }

                    currentPoly.AddPoint(last);
                    currentPoly.AddPoint(first);

                    currentPoly = polyA;
                }

                if (intersections.Count == 1)
                {
                    currentPoly.AddPoint(intersection);

                    if (collisionSlice.Count > 0)
                    {
                        if (Vector2f.Distance(intersection, collisionSlice.Last()) < Vector2f.Distance(intersection, collisionSlice.First()))
                        {
                            collisionSlice.Reverse();
                        }

                        currentPoly.AddPoints(collisionSlice);
                    }

                    currentPoly = (currentPoly == polyA) ? polyB : polyA;

                    currentPoly.AddPoint(intersection);
                }
            }
            currentPoly.AddPoint(p.B);
        }

        result.AddPolygon(polyA);
        result.AddPolygon(polyB);

        foreach (Polygon poly in result.polygons)
        {
            foreach (Polygon hole in polygon.holesList)
            {
                if (poly.PolyInPoly(hole) == true)
                {
                    poly.AddHole(hole);
                }
            }
        }

        return(result);
    }
Пример #5
0
    static private Slice2D SliceWithOneHole(Polygon polygon, List <Vector2f> slice, List <Vector2f> collisionSlice)
    {
        Slice2D result = Slice2D.Create();

        Polygon holeA    = polygon.PointInHole(slice.First());
        Polygon holeB    = polygon.PointInHole(slice.Last());
        Polygon holePoly = (holeA != null) ? holeA : holeB;


        if (polygon.PointInPoly(slice.First()) == false || polygon.PointInPoly(slice.Last()) == false)
        {
            if (holeA == holeB)
            {
                if (Slicer2D.complexSliceType == Slicer2D.SliceType.Regular)
                {
                    return(result);
                }

                if (holeA == null)
                {
                    Debug.LogError("Slicer2D: This happened when collider had a lot of paths but they were not holes");
                    return(result);
                }

                List <Vector2f> slice2 = new List <Vector2f> (slice);

                Polygon polyA = new Polygon(polygon.pointsList);
                Polygon polyB = new Polygon(slice);
                Polygon polyC = new Polygon(slice2);

                // Get First Point - NOT FINISHED WITH INTERSECTION
                int             Add;
                List <Vector2f> list;
                List <Pair2f>   iterateList = Pair2f.GetList(holeA.pointsList);

                Add  = 0;
                list = new List <Vector2f> ();
                foreach (Pair2f pair in iterateList)
                {
                    List <Vector2f> intersect = MathHelper.GetListLineIntersectSlice(pair, slice);
                    if (intersect.Count > 0)
                    {
                        Add += intersect.Count;
                    }

                    if (Add == 1)
                    {
                        list.Add(pair.B);
                    }
                }

                if (list.Count > 0)
                {
                    if (Vector2f.Distance(list.First(), slice.First()) < Vector2f.Distance(list.First(), slice.Last()))
                    {
                        slice.Reverse();
                    }

                    polyB.AddPoints(list);
                }

                Add  = 0;
                list = new List <Vector2f> ();
                foreach (Pair2f pair in iterateList)
                {
                    List <Vector2f> intersect = MathHelper.GetListLineIntersectSlice(pair, slice2);
                    if (intersect.Count > 0)
                    {
                        Add += intersect.Count;
                    }

                    if (Add == 2)
                    {
                        list.Add(pair.B);
                    }
                }

                foreach (Pair2f pair in iterateList)
                {
                    List <Vector2f> intersect = MathHelper.GetListLineIntersectSlice(pair, slice2);
                    if (intersect.Count > 0)
                    {
                        Add += intersect.Count;
                    }

                    if (Add == 2)
                    {
                        list.Add(pair.B);
                    }
                }

                if (list.Count > 0)
                {
                    if (Vector2f.Distance(list.First(), slice2.First()) < Vector2f.Distance(list.First(), slice2.Last()))
                    {
                        slice2.Reverse();
                    }

                    polyC.AddPoints(list);
                }

                if (polyB.GetArea() > polyC.GetArea())
                {
                    Polygon swap = polyB;
                    polyB = polyC;
                    polyC = swap;
                }

                // Add holes to new created polygon
                foreach (Polygon poly in polygon.holesList)
                {
                    if (poly != holeA && polyB.PolyInPoly(poly) == true)
                    {
                        polyB.AddHole(poly);
                    }
                }

                if (Slicer2D.complexSliceType == Slicer2D.SliceType.FillSlicedHole)
                {
                    result.AddPolygon(polyB);
                }

                polyA.AddHole(polyC);

                // Adds polygons if they are not in the hole
                foreach (Polygon poly in polygon.holesList)                 // Check for errors?
                {
                    if (poly != holeA && polyC.PolyInPoly(poly) == false)
                    {
                        polyA.AddHole(poly);
                    }
                }

                result.AddPolygon(polyA);
                return(result);
            }
            else if (holePoly != null)
            {
                Polygon polyA = new Polygon();
                Polygon polyB = new Polygon(holePoly.pointsList);
                polyB.pointsList.Reverse();

                List <Vector2f> pointsA = VectorList2f.GetListStartingIntersectSlice(polygon.pointsList, slice);
                List <Vector2f> pointsB = VectorList2f.GetListStartingIntersectSlice(polyB.pointsList, slice);

                if (pointsA.Count < 1)
                {
                    Debug.LogError("Slicer2D: " + pointsA.Count + " " + polygon.pointsList.Count);
                }

                polyA.AddPoints(pointsA);

                if (collisionSlice.Count > 0)
                {
                    // pointsA empty
                    if (Vector2f.Distance(pointsA.Last(), collisionSlice.Last()) < Vector2f.Distance(pointsA.Last(), collisionSlice.First()))
                    {
                        collisionSlice.Reverse();
                    }

                    polyA.AddPoints(collisionSlice);
                }

                polyA.AddPoints(pointsB);

                if (collisionSlice.Count > 0)
                {
                    collisionSlice.Reverse();
                    polyA.AddPoints(collisionSlice);
                }

                foreach (Polygon poly in polygon.holesList)                 // Check for errors?
                {
                    if (poly != holePoly)
                    {
                        polyA.AddHole(poly);
                    }
                }

                result.AddPolygon(polyA);

                return(result);
            }
        }

        return(result);
    }