Exemple #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);
    }
Exemple #2
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);
    }