示例#1
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);
    }
示例#2
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);
    }