Esempio n. 1
0
    // Might Break (Only for 2 collisions)
    /// <summary>
    /// Return a list which starts with first interesction with given slice
    /// </summary>
    public static List <Vector2D> GetListStartingIntersectSlice(List <Vector2D> pointsList, List <Vector2D> slice)
    {
        List <Vector2D> result = new List <Vector2D> ();
        bool            start  = false;

        List <Pair2D> pointsPairList = Pair2D.GetList(pointsList);

        foreach (Pair2D p in pointsPairList)
        {
            List <Vector2D> r = Math2D.GetListLineIntersectSlice(p, slice);
            if (start == true)
            {
                result.Add(p.A);
            }

            if (r.Count > 0)
            {
                result.Add(r.First());
                start = true;
            }
        }

        foreach (Pair2D p in pointsPairList)
        {
            List <Vector2D> r = Math2D.GetListLineIntersectSlice(p, slice);
            if (start == true)
            {
                result.Add(p.A);
            }

            if (r.Count > 0)
            {
                result.Add(r.First());
                start = false;
            }
        }
        return(result);
    }
Esempio n. 2
0
    static private Slice2D SliceWithoutHoles(Polygon2D polygon, List <Vector2D> slice, ComplexCollision collisionSlice)
    {
        Slice2D result = Slice2D.Create(null, slice);

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

        Polygon2D currentPoly = polyA;

        List <Vector2D> slices = new List <Vector2D>(collisionSlice.GetPointsInsidePlus());

        foreach (Pair2D p in Pair2D.GetList(polygon.pointsList))
        {
            List <Vector2D> intersections = Math2D.GetListLineIntersectSlice(p, slice);

            if (intersections.Count() > 0)
            {
                if (intersections.Count == 2)
                {
                    Vector2D first = intersections.First();
                    Vector2D last  = intersections.Last();

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

                    //currentPoly.AddPoint (first);

                    // Add Inside Points
                    if (collisionSlice.GetPointsInsidePlus().Count > 0)
                    {
                        if (Vector2D.Distance(first, collisionSlice.Last()) < Vector2D.Distance(first, collisionSlice.First()))
                        {
                            collisionSlice.Reverse();
                        }

                        currentPoly.AddPoints(collisionSlice.GetPointsInsidePlus());
                    }
                    /////

                    //currentPoly.AddPoint (last);

                    currentPoly = polyB;

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

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

                    currentPoly = polyA;
                }

                if (intersections.Count == 1)
                {
                    Vector2D intersection = intersections.First();

                    //currentPoly.AddPoint (intersection);

                    ///// Add Inside Points
                    if (collisionSlice.GetPointsInsidePlus().Count > 0)
                    {
                        if (Vector2D.Distance(intersection, collisionSlice.Last()) < Vector2D.Distance(intersection, collisionSlice.First()))
                        {
                            collisionSlice.Reverse();
                        }

                        currentPoly.AddPoints(collisionSlice.GetPointsInsidePlus());
                    }
                    /////

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

                    //currentPoly.AddPoint (intersection);
                }
            }

            currentPoly.AddPoint(p.B);
        }

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

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

        result.AddSlice(slices);
        return(result);
    }
Esempio n. 3
0
    static private Slice2D SliceWithOneHole(Polygon2D polygon, List <Vector2D> slice, ComplexCollision collisionSlice)
    {
        Slice2D result = Slice2D.Create(null, slice);

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

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

        List <Vector2D> slices = new List <Vector2D>(collisionSlice.GetPointsInsidePlus());

        if (polygon.PointInPoly(slice.First()) == false || polygon.PointInPoly(slice.Last()) == false)
        {
            // Slicing Into The Same Hole
            if (holeA == holeB)
            {
                if (Slicer2D.complexSliceType == Slicer2D.SliceType.Regular)
                {
                    return(result);
                }

                if (collisionSlice.polygonCollisionPairs.Count == 1)
                {
                    Polygon2D slicePoly = new Polygon2D(collisionSlice.GetPointsInsidePlus());

                    Polygon2D newHole = new Polygon2D();
                    if (slicePoly.PolyInPoly(holePoly))
                    {
                        newHole = slicePoly;
                    }
                    else
                    {
                        foreach (Pair2D pair in Pair2D.GetList(holePoly.pointsList))
                        {
                            newHole.AddPoint(pair.A);

                            if (Vector2D.Distance(pair.A, collisionSlice.Last()) < Vector2D.Distance(pair.A, collisionSlice.First()))
                            {
                                collisionSlice.Reverse();
                            }

                            if (Math2D.LineIntersectSlice(pair, slice))
                            {
                                newHole.AddPoints(collisionSlice.GetPoints());
                            }
                        }
                    }

                    Polygon2D polygonA = new Polygon2D(polygon.pointsList);
                    polygonA.AddHole(newHole);

                    // Adds polygons if they are not in the hole
                    foreach (Polygon2D poly in polygon.holesList)                       // Check for errors?
                    {
                        if (poly != holePoly && polygonA.PolyInPoly(poly) == true)
                        {
                            polygonA.AddHole(poly);
                        }
                    }

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

                    result.AddPolygon(polygonA);

                    result.AddSlice(slices);

                    return(result);
                }
                else
                {
                    Polygon2D polyA    = new Polygon2D(polygon.pointsList);
                    Polygon2D newHoleA = new Polygon2D();
                    Polygon2D newHoleB = new Polygon2D();

                    List <Pair2D> iterateList = Pair2D.GetList(holePoly.pointsList);

                    bool addPoints = false;

                    foreach (Pair2D pair in iterateList)
                    {
                        List <Vector2D> intersect = Math2D.GetListLineIntersectSlice(pair, slice);

                        switch (addPoints)
                        {
                        case false:
                            if (intersect.Count > 0)
                            {
                                addPoints = true;
                            }

                            break;

                        case true:
                            newHoleA.AddPoint(pair.A);

                            if (intersect.Count > 0)
                            {
                                addPoints = false;

                                if (Vector2D.Distance(intersect[0], collisionSlice.Last()) < Vector2D.Distance(intersect[0], collisionSlice.First()))
                                {
                                    collisionSlice.Reverse();
                                }
                                newHoleA.AddPoints(collisionSlice.GetPointsInsidePlus());
                            }
                            break;
                        }
                    }

                    addPoints = true;
                    foreach (Pair2D pair in iterateList)
                    {
                        List <Vector2D> intersect = Math2D.GetListLineIntersectSlice(pair, slice);

                        switch (addPoints)
                        {
                        case false:
                            if (intersect.Count > 0)
                            {
                                addPoints = true;
                            }

                            break;

                        case true:
                            newHoleB.AddPoint(pair.A);

                            if (intersect.Count > 0)
                            {
                                addPoints = false;

                                if (Vector2D.Distance(intersect[0], collisionSlice.Last()) < Vector2D.Distance(intersect[0], collisionSlice.First()))
                                {
                                    collisionSlice.Reverse();
                                }
                                newHoleB.AddPoints(collisionSlice.GetPointsInsidePlus());
                            }
                            break;
                        }
                    }

                    if (newHoleB.GetArea() > newHoleA.GetArea())
                    {
                        Polygon2D tempPolygon = newHoleA;
                        newHoleA = newHoleB;
                        newHoleB = tempPolygon;
                    }

                    polyA.AddHole(newHoleA);

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

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

                    result.AddPolygon(polyA);

                    result.AddSlice(slices);
                    return(result);
                }

                // Slicing From Outside To Hole
            }
            else if (holePoly != null)
            {
                Polygon2D polyA = new Polygon2D();
                Polygon2D polyB = new Polygon2D(holePoly.pointsList);
                polyB.pointsList.Reverse();

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

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

                polyA.AddPoints(pointsA);

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

                    polyA.AddPoints(collisionSlice.GetPointsInside());
                }

                polyA.AddPoints(pointsB);

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

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

                result.AddPolygon(polyA);

                result.AddSlice(slices);
                return(result);
            }
        }

        return(result);
    }
Esempio n. 4
0
            static public Slice2D SliceIntoSameHole(Polygon2D polygon, Polygon2D holePoly, List <Vector2D> slice, ComplexCollision collisionSlice)
            {
                Slice2D result = Slice2D.Create(null, slice);

                // Slice Into Same Pair
                if (collisionSlice.polygonCollisionPairs.Count == 1)
                {
                    Polygon2D slicePoly = new Polygon2D(collisionSlice.GetPointsInsidePlus());

                    Polygon2D newHole = new Polygon2D();
                    if (slicePoly.PolyInPoly(holePoly))
                    {
                        newHole = slicePoly;
                    }
                    else
                    {
                        foreach (Pair2D pair in Pair2D.GetList(holePoly.pointsList))
                        {
                            newHole.AddPoint(pair.A);

                            if (Vector2D.Distance(pair.A, collisionSlice.Last()) < Vector2D.Distance(pair.A, collisionSlice.First()))
                            {
                                collisionSlice.Reverse();
                            }

                            if (Math2D.LineIntersectSlice(pair, slice))
                            {
                                newHole.AddPoints(collisionSlice.GetPoints());
                            }
                        }
                    }

                    Polygon2D polygonA = new Polygon2D(polygon.pointsList);
                    polygonA.AddHole(newHole);

                    // Adds polygons if they are not in the hole
                    foreach (Polygon2D poly in polygon.holesList)                       // Check for errors?
                    {
                        if (poly != holePoly && polygonA.PolyInPoly(poly) == true)
                        {
                            polygonA.AddHole(poly);
                        }
                    }

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

                    result.AddPolygon(polygonA);

                    return(result);

                    // Slice Into Different Pair
                }
                else
                {
                    Polygon2D polyA    = new Polygon2D(polygon.pointsList);
                    Polygon2D newHoleA = new Polygon2D();
                    Polygon2D newHoleB = new Polygon2D();

                    List <Pair2D> iterateList = Pair2D.GetList(holePoly.pointsList);

                    bool addPoints = false;

                    foreach (Pair2D pair in iterateList)
                    {
                        List <Vector2D> intersect = Math2D.GetListLineIntersectSlice(pair, slice);

                        switch (addPoints)
                        {
                        case false:
                            if (intersect.Count > 0)
                            {
                                addPoints = true;
                            }

                            break;

                        case true:
                            newHoleA.AddPoint(pair.A);

                            if (intersect.Count > 0)
                            {
                                addPoints = false;

                                if (Vector2D.Distance(intersect[0], collisionSlice.Last()) < Vector2D.Distance(intersect[0], collisionSlice.First()))
                                {
                                    collisionSlice.Reverse();
                                }
                                newHoleA.AddPoints(collisionSlice.GetPointsInsidePlus());
                            }
                            break;
                        }
                    }

                    addPoints = true;
                    foreach (Pair2D pair in iterateList)
                    {
                        List <Vector2D> intersect = Math2D.GetListLineIntersectSlice(pair, slice);

                        switch (addPoints)
                        {
                        case false:
                            if (intersect.Count > 0)
                            {
                                addPoints = true;
                            }

                            break;

                        case true:
                            newHoleB.AddPoint(pair.A);

                            if (intersect.Count > 0)
                            {
                                addPoints = false;

                                if (Vector2D.Distance(intersect[0], collisionSlice.Last()) < Vector2D.Distance(intersect[0], collisionSlice.First()))
                                {
                                    collisionSlice.Reverse();
                                }
                                newHoleB.AddPoints(collisionSlice.GetPointsInsidePlus());
                            }
                            break;
                        }
                    }

                    if (newHoleB.GetArea() > newHoleA.GetArea())
                    {
                        Polygon2D tempPolygon = newHoleA;
                        newHoleA = newHoleB;
                        newHoleB = tempPolygon;
                    }

                    polyA.AddHole(newHoleA);

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

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

                    result.AddPolygon(polyA);

                    return(result);
                }
            }
Esempio n. 5
0
        static private Merge2D SliceWithoutHoles(Polygon2D polygon, List <Vector2D> slice, MergeCollision mergeCollision)
        {
            Merge2D result = Merge2D.Create(slice);

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

            Polygon2D currentPoly = polyA;

            List <Vector2D> slices = new List <Vector2D>(mergeCollision.GetPoints());


            foreach (Pair2D p in Pair2D.GetList(polygon.pointsList))
            {
                List <Vector2D> intersections = Math2D.GetListLineIntersectSlice(p, slice);

                if (intersections.Count() > 0)
                {
                    if (intersections.Count == 2)
                    {
                        Vector2D first = intersections.First();
                        Vector2D last  = intersections.Last();

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

                        // Add Inside Points
                        if (mergeCollision.GetPoints().Count > 0)                           // InsidePlus
                        {
                            if (Vector2D.Distance(first, mergeCollision.Last()) < Vector2D.Distance(first, mergeCollision.First()))
                            {
                                mergeCollision.Reverse();
                            }

                            currentPoly.AddPoints(mergeCollision.GetPoints());                              // InsidePlus
                        }
                        /////

                        currentPoly = polyB;

                        if (mergeCollision.GetPoints().Count > 0)                           // InsidePlus(
                        {
                            currentPoly.AddPoints(mergeCollision.GetPoints());              // InsidePlus
                        }

                        currentPoly = polyA;
                    }

                    if (intersections.Count == 1)
                    {
                        Vector2D intersection = intersections.First();

                        ///// Add Inside Points
                        if (mergeCollision.GetPoints().Count > 0)                           //InsidePlus
                        {
                            if (Vector2D.Distance(intersection, mergeCollision.Last()) < Vector2D.Distance(intersection, mergeCollision.First()))
                            {
                                mergeCollision.Reverse();
                            }

                            currentPoly.AddPoints(mergeCollision.GetPoints());                              // InsidePlus
                        }
                        /////

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

                currentPoly.AddPoint(p.B);
            }

            Polygon2D mainPoly = polyA;

            if (polyB != null && polyB.GetArea() > polyA.GetArea())
            {
                mainPoly = polyB;
            }

            result.AddPolygon(mainPoly);

            foreach (Polygon2D hole in polygon.holesList)
            {
                mainPoly.AddHole(hole);
            }

            result.AddSlice(slices);

            return(result);
        }
Esempio n. 6
0
    static private Slice2D SliceWithOneHole(Polygon2D polygon, List <Vector2D> slice, List <Vector2D> collisionSlice)
    {
        Slice2D result = Slice2D.Create(slice);

        Polygon2D holeA    = polygon.PointInHole(slice.First());
        Polygon2D holeB    = polygon.PointInHole(slice.Last());
        Polygon2D 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)
                {
                    Slicer2D.Debug.LogError("This happened when collider had a lot of paths but they were not holes");
                    return(result);
                }

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

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

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

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

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

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

                    polyB.AddPoints(list);
                }

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

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

                foreach (Pair2D pair in iterateList)
                {
                    List <Vector2D> intersect = Math2D.GetListLineIntersectSlice(pair, slice2);
                    if (intersect.Count > 0)
                    {
                        Add += intersect.Count;
                    }

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

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

                    polyC.AddPoints(list);
                }

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

                // Add holes to new created polygon
                foreach (Polygon2D 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 (Polygon2D poly in polygon.holesList)                   // Check for errors?
                {
                    if (poly != holeA && polyC.PolyInPoly(poly) == false)
                    {
                        polyA.AddHole(poly);
                    }
                }

                result.AddPolygon(polyA);

                result.AddSlice(collisionSlice);
                return(result);
            }
            else if (holePoly != null)
            {
                Polygon2D polyA = new Polygon2D();
                Polygon2D polyB = new Polygon2D(holePoly.pointsList);
                polyB.pointsList.Reverse();

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

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

                polyA.AddPoints(pointsA);

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

                    polyA.AddPoints(collisionSlice);
                }

                polyA.AddPoints(pointsB);

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

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

                result.AddPolygon(polyA);

                result.AddSlice(collisionSlice);
                return(result);
            }
        }

        return(result);
    }