Beispiel #1
0
    public Vector2D GetPoint(DoublePair2D pair)
    {
        float rotA = (float)Vector2D.Atan2(pair.B, pair.A);
        float rotC = (float)Vector2D.Atan2(pair.B, pair.C);

        Vector2D pairA = new Vector2D(pair.A);

        pairA.Push(rotA - Mathf.PI / 2, lineWidth);
        pairA.Push(rotA, -100f);

        Vector2D pairC = new Vector2D(pair.C);

        pairC.Push(rotC + Mathf.PI / 2, lineWidth);
        pairC.Push(rotC, -100f);

        Vector2D vecA = new Vector2D(pair.B);

        vecA.Push(rotA - Mathf.PI / 2, lineWidth);
        vecA.Push(rotA, 100f);

        Vector2D vecC = new Vector2D(pair.B);

        vecC.Push(rotC + Mathf.PI / 2, lineWidth);
        vecC.Push(rotC, 100f);

        return(Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC)));
    }
    // Not finished - still has some artifacts
    static public List <Vector2D> PreparePolygon(Polygon2D polygon)
    {
        Polygon2D newPolygon = new Polygon2D();

        polygon.Normalize();

        DoublePair2D pair;

        foreach (Vector2D pB in polygon.pointsList)
        {
            int indexB = polygon.pointsList.IndexOf(pB);

            int indexA = (indexB - 1);
            if (indexA < 0)
            {
                indexA += polygon.pointsList.Count;
            }

            int indexC = (indexB + 1);
            if (indexC >= polygon.pointsList.Count)
            {
                indexC -= polygon.pointsList.Count;
            }

            pair = new DoublePair2D(polygon.pointsList[indexA], pB, polygon.pointsList[indexC]);

            float rotA = (float)Vector2D.Atan2(pair.B, pair.A);
            float rotC = (float)Vector2D.Atan2(pair.B, pair.C);

            Vector2D pairA = new Vector2D(pair.A);
            pairA.Push(rotA - Mathf.PI / 2, precision);

            Vector2D pairC = new Vector2D(pair.C);
            pairC.Push(rotC + Mathf.PI / 2, precision);

            Vector2D vecA = new Vector2D(pair.B);
            vecA.Push(rotA - Mathf.PI / 2, precision);
            vecA.Push(rotA, 10f);

            Vector2D vecC = new Vector2D(pair.B);
            vecC.Push(rotC + Mathf.PI / 2, precision);
            vecC.Push(rotC, 10f);

            Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC));

            if (result != null)
            {
                newPolygon.AddPoint(result);
            }
        }

        return(newPolygon.pointsList);
    }
Beispiel #3
0
    /// <summary>
    /// Return a list which starts with first interesction with given line
    /// </summary>
    public static List <Vector2D> GetListStartingIntersectLine(List <Vector2D> pointsList, Pair2D line)
    {
        List <Vector2D> result = new List <Vector2D> ();
        bool            start  = false;

        Pair2D   id = pair2D;
        Vector2D r;

        id.B = pointsList.Last();

        for (int x = 0; x < pointsList.Count; x++)
        {
            id.A = pointsList[x];

            r = Math2D.GetPointLineIntersectLine(id, line);
            if (start == true)
            {
                result.Add(id.B);
            }

            if (r != null)
            {
                result.Add(r);
                start = true;
            }

            id.B = id.A;
        }

        id.B = pointsList.Last();

        for (int x = 0; x < pointsList.Count; x++)
        {
            id.A = pointsList[x];

            r = Math2D.GetPointLineIntersectLine(id, line);
            if (start == true)
            {
                result.Add(id.B);
            }

            if (r != null)
            {
                result.Add(r);
                start = false;
            }

            id.B = id.A;
        }
        return(result);
    }
Beispiel #4
0
    /// <summary>
    /// Return a list which starts with first interesction with given line
    /// </summary>
    public static List <Vector2D> GetListStartingIntersectLine(List <Vector2D> pointsList, Pair2D line)
    {
        List <Vector2D> result = new List <Vector2D> ();
        bool            start  = false;

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

        foreach (Pair2D p in pointsPairList)
        {
            Vector2D r = Math2D.GetPointLineIntersectLine(p, line);
            if (start == true)
            {
                result.Add(p.A);
            }

            if (r != null)
            {
                result.Add(r);
                start = true;
            }
        }

        foreach (Pair2D p in pointsPairList)
        {
            Vector2D r = Math2D.GetPointLineIntersectLine(p, line);
            if (start == true)
            {
                result.Add(p.A);
            }

            if (r != null)
            {
                result.Add(r);
                start = false;
            }
        }
        return(result);
    }
Beispiel #5
0
    // Not finished - still has some artifacts
    static public List <Vector2D> PreparePolygon(Polygon2D polygon)
    {
        Polygon2D newPolygon = new Polygon2D();

        polygon.Normalize();

        foreach (DoublePair2D pair in DoublePair2D.GetList(polygon.pointsList))
        {
            float rotA = Vector2D.Atan2(pair.B, pair.A);
            float rotC = Vector2D.Atan2(pair.B, pair.C);

            Vector2D pairA = new Vector2D(pair.A);
            pairA.Push(rotA - Mathf.PI / 2, precision);

            Vector2D pairC = new Vector2D(pair.C);
            pairC.Push(rotC + Mathf.PI / 2, precision);

            Vector2D vecA = new Vector2D(pair.B);
            vecA.Push(rotA - Mathf.PI / 2, precision);
            vecA.Push(rotA, 10f);

            Vector2D vecC = new Vector2D(pair.B);
            vecC.Push(rotC + Mathf.PI / 2, precision);
            vecC.Push(rotC, 10f);

            Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC));

            if (result != null)
            {
                newPolygon.AddPoint(result);
                //	} else {
                //Debug.LogError("Null");
            }
        }
        return(newPolygon.pointsList);
    }
Beispiel #6
0
    static private Slice2D SliceWithOneHole(Polygon2D polygon, Pair2D slice, Polygon2D holeA, Polygon2D holeB)
    {
        Slice2D result = Slice2D.Create(null, slice);

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

            Polygon2D currentPoly = polyB;

            foreach (Pair2D pair in Pair2D.GetList(holeA.pointsList))
            {
                Vector2D point = Math2D.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)
        {
            Polygon2D holePoly = (holeA != null) ? holeA : holeB;

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

                polyB.pointsList.Reverse();

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

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

                result.AddPolygon(polyA);
                return(result);
            }
        }
        return(result);
    }
Beispiel #7
0
    static private Slice2D SliceWithoutHoles(Polygon2D polygon, Pair2D slice)
    {
        Slice2D result = Slice2D.Create(null, slice);

        if (polygon.LineIntersectHoles(slice).Count > 0)
        {
            // When does this happen? - Only when using point slicer!!!
            if (Slicer2D.Debug.enabled)
            {
                Debug.LogWarning("Slicer2D: Slice Intersect Holes (Point Slicer?)");
            }
            return(result);
        }

        Polygon2D polyA = new Polygon2D();
        Polygon2D polyB = new Polygon2D();

        Polygon2D currentPoly = polyA;

        int collisionCount = 0;

        Pair2D id = Pair2D.Zero();

        id.A = polygon.pointsList.Last();

        for (int i = 0; i < polygon.pointsList.Count; i++)
        {
            id.B = polygon.pointsList[i];

            Vector2D intersection = Math2D.GetPointLineIntersectLine(id, slice);

            if (intersection != null)
            {
                polyA.AddPoint(intersection);
                polyB.AddPoint(intersection);

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

                collisionCount++;
            }
            currentPoly.AddPoint(id.B);

            id.A = id.B;
        }

        if (collisionCount == 2)            // ' Is it convex or concave split?
        {
            if (polyA.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyA);
            }

            if (polyB.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyB);
            }

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

            return(result);
        }

        if (collisionCount == 3)
        {
            if (Slicer2D.Debug.enabled)
            {
                Debug.LogWarning("Slicer2D: Linear Slice with 3 points");
            }
        }

        if (collisionCount == 333)            // Complicated Slices With Holes
        {
            if (Slicer2D.Debug.enabled)
            {
                Debug.LogWarning("Slicer2D: Slice " + collisionCount);
            }

            if (polyA.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyA);
            }

            if (polyB.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyB);
            }

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

            return(result);
        }
        return(result);
    }
Beispiel #8
0
        // Create Polygon Inside? Extended Method?
        public static Slice2D SlicePolygonInside(Polygon2D polygon, List <Vector2D> slice)
        {
            Slice2D result = Slice2D.Create(null, slice);

            if (Slicer2D.complexSliceType != Slicer2D.SliceType.SliceHole)
            {
                return(result);
            }

            List <List <Vector2D> > checkSlices = new List <List <Vector2D> >();
            List <Vector2D>         curSlice    = null;

            foreach (Vector2D p in slice)
            {
                if (polygon.PointInPoly(p))
                {
                    if (curSlice == null)
                    {
                        curSlice = new List <Vector2D>();
                        checkSlices.Add(curSlice);
                    }
                    curSlice.Add(p);
                }
                else
                {
                    curSlice = null;
                }
            }

            //bool createPoly = false;
            List <Polygon2D> newPolygons = new List <Polygon2D>();
            Polygon2D        newPoly     = null;

            foreach (List <Vector2D> checkSlice in checkSlices)
            {
                foreach (Pair2D pairA in Pair2D.GetList(checkSlice, false))
                {
                    foreach (Pair2D pairB in Pair2D.GetList(checkSlice, false))
                    {
                        Vector2D intersection = Math2D.GetPointLineIntersectLine(pairA, pairB);
                        if (intersection != null && (pairA.A != pairB.A && pairA.B != pairB.B && pairA.A != pairB.B && pairA.B != pairB.A))
                        {
                            if (newPoly == null)
                            {
                                newPoly = new Polygon2D();
                                newPolygons.Add(newPoly);
                                newPoly.AddPoint(intersection);
                            }
                            else
                            {
                                //newPoly.AddPoint (intersection);
                                newPoly = null;
                            }
                        }
                    }
                    if (newPoly != null)
                    {
                        newPoly.AddPoint(pairA.B);
                    }
                }
            }

            foreach (Polygon2D poly in new List <Polygon2D>(newPolygons))
            {
                if (poly.pointsList.Count < 3)
                {
                    newPolygons.Remove(poly);
                    continue;
                }
            }

            if (newPolygons.Count > 0)
            {
                result.AddPolygon(polygon);

                foreach (Polygon2D poly in newPolygons)
                {
                    List <Polygon2D> polys = new List <Polygon2D> (polygon.holesList);
                    foreach (Polygon2D hole in polys)
                    {
                        if (poly.PolyInPoly(hole) == true)
                        {
                            polygon.holesList.Remove(hole);
                            poly.AddHole(hole);
                        }
                    }

                    polygon.AddHole(poly);
                }

                return(result);
            }

            return(result);
        }
    static public List <Vector2D> Get(Polygon2D polygon, float multiplier = 1f)
    {
        Polygon2D newPolygon = new Polygon2D();

        polygon.Normalize();

        Vector2D result;

        double   rotA, rotC;
        Vector2D pB;

        for (int i = 0; i < polygon.pointsList.Count; i++)
        {
            pB = polygon.pointsList[i];

            int indexB = polygon.pointsList.IndexOf(pB);

            int indexA = (indexB - 1);
            if (indexA < 0)
            {
                indexA += polygon.pointsList.Count;
            }

            int indexC = (indexB + 1);
            if (indexC >= polygon.pointsList.Count)
            {
                indexC -= polygon.pointsList.Count;
            }

            pair.A = polygon.pointsList[indexA];
            pair.B = pB;
            pair.C = polygon.pointsList[indexC];

            rotA = Vector2D.Atan2(pair.B, pair.A);
            rotC = Vector2D.Atan2(pair.B, pair.C);

            pairA.x = pair.A.x;
            pairA.y = pair.A.y;
            pairA.Push(rotA - Mathf.PI / 2, precision * multiplier);

            pairC.x = pair.C.x;
            pairC.y = pair.C.y;
            pairC.Push(rotC + Mathf.PI / 2, precision * multiplier);

            vecA.x = pair.B.x;
            vecA.y = pair.B.y;
            vecA.Push(rotA - Mathf.PI / 2, precision * multiplier);
            vecA.Push(rotA, 100f);

            vecC.x = pair.B.x;
            vecC.y = pair.B.y;
            vecC.Push(rotC + Mathf.PI / 2, precision * multiplier);
            vecC.Push(rotC, 100f);

            pair0.A = pairA;
            pair0.B = vecA;

            pair1.A = pairC;
            pair1.B = vecC;

            result = Math2D.GetPointLineIntersectLine(pair0, pair1);

            if (result != null)
            {
                newPolygon.AddPoint(result);
            }
        }

        return(newPolygon.pointsList);
    }
Beispiel #10
0
            static public Slice2D Slice(Polygon2D polygon, Pair2D slice)
            {
                Slice2D result = Slice2D.Create(null, slice);

                if (polygon.LineIntersectHoles(slice).Count > 0)
                {
                    // When does this happen? - Only when using point slicer!!!

                    Debug.LogWarning("Slicer2D: Slice Intersect Holes (Point Slicer?)");

                    return(result);
                }

                Polygon2D polyA = new Polygon2D();
                Polygon2D polyB = new Polygon2D();

                Polygon2D currentPoly = polyA;

                int collisionCount = 0;

                Pair2D id = Pair2D.Zero();

                id.A = polygon.pointsList.Last();

                int vertexCollisionCount = 0;

                for (int i = 0; i < polygon.pointsList.Count; i++)
                {
                    id.B = polygon.pointsList[i];

                    Vector2D intersection = Math2D.GetPointLineIntersectLine(id, slice);

                    if (intersection != null)
                    {
                        //float distance = 1;

                        bool vertexCollision = false;

                        // Why vertex code when there is separate class for it?

                        // Last Point
                        if (polyA.pointsList.Count > 0 && Vector2D.Distance(intersection, polyA.pointsList.Last()) < precision)
                        {
                            vertexCollision = true;
                        }

                        if (polyB.pointsList.Count > 0 && Vector2D.Distance(intersection, polyB.pointsList.Last()) < precision)
                        {
                            vertexCollision = true;
                        }

                        // First Point
                        if (polyA.pointsList.Count > 0 && Vector2D.Distance(intersection, polyA.pointsList.First()) < precision)
                        {
                            vertexCollision = true;
                        }

                        if (polyB.pointsList.Count > 0 && Vector2D.Distance(intersection, polyB.pointsList.First()) < precision)
                        {
                            vertexCollision = true;
                        }

                        if (vertexCollision == false)
                        {
                            polyA.AddPoint(intersection);
                            polyB.AddPoint(intersection);

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

                            collisionCount++;
                        }
                        else
                        {
                            Debug.Log("vertex collision");

                            vertexCollisionCount += 1;
                        }
                    }


                    currentPoly.AddPoint(id.B);

                    id.A = id.B;
                }

                //Debug.Log(vertexCollisionCount + " " + collisionCount);

                switch (collisionCount)
                {
                case 2:
                    if (polyA.pointsList.Count() >= 3)
                    {
                        result.AddPolygon(polyA);
                    }

                    if (polyB.pointsList.Count() >= 3)
                    {
                        result.AddPolygon(polyB);
                    }

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

                    return(result);

                default:

                    Debug.LogWarning("Slicer2D: Linear Slice with " + collisionCount + " collision points");

                    break;
                }

                /*
                 * if (collisionCount == 333) {  // Complicated Slices With Holes
                 *      if (Slicer2D.Debug.enabled) {
                 *              Debug.LogWarning("Slicer2D: Slice " + collisionCount);
                 *      }
                 *
                 *      if (polyA.pointsList.Count () >= 3) {
                 *              result.AddPolygon (polyA);
                 *      }
                 *
                 *      if (polyB.pointsList.Count () >= 3) {
                 *              result.AddPolygon (polyB);
                 *      }
                 *
                 *      foreach (Polygon2D poly in result.GetPolygons()) {
                 *              foreach (Polygon2D hole in polygon.holesList) {
                 *                      if (poly.PolyInPoly (hole) == true) {
                 *                              poly.AddHole (hole);
                 *                      }
                 *              }
                 *      }
                 *
                 *      return(result);
                 * }*/

                return(result);
            }
Beispiel #11
0
            static public Slice2D Slice(Polygon2D polygon, Pair2D slice, Vector2D vertex)
            {
                Slice2D result = Slice2D.Create(null, slice);

                polygon.pointsList = Vector2DList.GetListStartingPoint(polygon.pointsList, vertex);

                Polygon2D polyA = new Polygon2D();
                Polygon2D polyB = new Polygon2D();

                Polygon2D currentPoly = polyA;

                int collisionCount = 0;

                Pair2D id = Pair2D.Zero();

                id.A = polygon.pointsList.Last();

                for (int i = 0; i < polygon.pointsList.Count; i++)
                {
                    id.B = polygon.pointsList[i];

                    Vector2D intersection = Math2D.GetPointLineIntersectLine(id, slice);

                    if (intersection != null)                       // && Vector2D.Distance(intersection, vertex) < 0.001f

                    {
                        if (polyA.pointsList.Count < 1 || Vector2D.Distance(intersection, polyA.pointsList.Last()) > precision)
                        {
                            polyA.AddPoint(intersection);
                        }

                        if (polyB.pointsList.Count < 1 || Vector2D.Distance(intersection, polyB.pointsList.Last()) > precision)
                        {
                            polyB.AddPoint(intersection);
                        }


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

                        collisionCount++;
                    }

                    if (currentPoly.pointsList.Count < 1 || Vector2D.Distance(id.B, currentPoly.pointsList.Last()) > precision)
                    {
                        currentPoly.AddPoint(id.B);
                    }

                    id.A = id.B;
                }

                switch (collisionCount)
                {
                case 1:
                case 2:
                case 3:
                    if (polyA.pointsList.Count() >= 3)
                    {
                        result.AddPolygon(polyA);
                    }

                    if (polyB.pointsList.Count() >= 3)
                    {
                        result.AddPolygon(polyB);
                    }

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

                    return(result);

                default:

                    Debug.LogWarning("Slicer2D: Vertex Linear Slice with " + collisionCount + " collision points " + slice.ToString());

                    break;
                }

                return(result);
            }
    public List <Vector2D> GetPointsList(float multiplier = 1f)
    {
        if (pointsList == null)
        {
            Debug.LogError("Complex Cut generation issue");
            return(new List <Vector2D>());
        }
        float sizeM  = size * multiplier;
        float sizeM2 = 2 * sizeM;

        List <Vector2D> list = new List <Vector2D>(pointsList);

        if (list.Count < 2)
        {
            return(new List <Vector2D>());
        }

        List <Vector2D> newPointsListA = new List <Vector2D>();
        List <Vector2D> newPointsListB = new List <Vector2D>();

        if (list.Count > 2)
        {
            foreach (DoublePair2D pair in DoublePair2D.GetList(list))
            {
                float rotA = (float)Vector2D.Atan2(pair.B, pair.A);
                float rotC = (float)Vector2D.Atan2(pair.B, pair.C);

                Vector2D pairA = new Vector2D(pair.A);
                pairA.Push(rotA - Mathf.PI / 2, sizeM);

                Vector2D pairC = new Vector2D(pair.C);
                pairC.Push(rotC + Mathf.PI / 2, sizeM);

                Vector2D vecA = new Vector2D(pair.B);
                vecA.Push(rotA - Mathf.PI / 2, sizeM);
                vecA.Push(rotA, 10f);

                Vector2D vecC = new Vector2D(pair.B);
                vecC.Push(rotC + Mathf.PI / 2, sizeM);
                vecC.Push(rotC, 10f);

                Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC));

                if (result != null)
                {
                    newPointsListA.Add(result);
                }
            }

            if (newPointsListA.Count > 2)
            {
                newPointsListA.Remove(newPointsListA.First());
                newPointsListA.Remove(newPointsListA.Last());
            }


            foreach (DoublePair2D pair in DoublePair2D.GetList(list, false))
            {
                float rotA = (float)Vector2D.Atan2(pair.B, pair.A);
                float rotC = (float)Vector2D.Atan2(pair.B, pair.C);

                Vector2D pairA = new Vector2D(pair.A);
                pairA.Push(rotA - Mathf.PI / 2, -sizeM);

                Vector2D pairC = new Vector2D(pair.C);
                pairC.Push(rotC + Mathf.PI / 2, -sizeM);

                Vector2D vecA = new Vector2D(pair.B);
                vecA.Push(rotA - Mathf.PI / 2, -sizeM);
                vecA.Push(rotA, 10f);

                Vector2D vecC = new Vector2D(pair.B);
                vecC.Push(rotC + Mathf.PI / 2, -sizeM);
                vecC.Push(rotC, 10f);

                Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC));

                if (result != null)
                {
                    newPointsListB.Add(result);
                }
            }

            if (newPointsListB.Count > 2)
            {
                newPointsListB.Remove(newPointsListB.First());
                newPointsListB.Remove(newPointsListB.Last());
            }
        }

        List <Vector2D> newPointsList = new List <Vector2D>();

        foreach (Vector2D p in newPointsListA)
        {
            newPointsList.Add(p);
        }

        Vector2D prevA = new Vector2D(list.ElementAt(list.Count - 2));
        Vector2D pA    = new Vector2D(list.Last());

        pA.Push(Vector2D.Atan2(pA, prevA) - Mathf.PI / 6, sizeM2);
        newPointsList.Add(pA);

        pA = new Vector2D(list.Last());
        pA.Push(Vector2D.Atan2(pA, prevA) + Mathf.PI / 6, sizeM2);
        newPointsList.Add(pA);

        newPointsListB.Reverse();
        foreach (Vector2D p in newPointsListB)
        {
            newPointsList.Add(p);
        }

        Vector2D prevB = new Vector2D(list.ElementAt(1));
        Vector2D pB    = new Vector2D(list.First());

        pB.Push(Vector2D.Atan2(pB, prevB) - Mathf.PI / 6, sizeM2);
        newPointsList.Add(pB);

        pB = new Vector2D(list.First());
        pB.Push(Vector2D.Atan2(pB, prevB) + Mathf.PI / 6, sizeM2);
        newPointsList.Add(pB);

        return(newPointsList);
    }
    // Create Polygon Inside? Extended Method?
    static private Slice2D SlicePolygonInside(Polygon2D polygon, List <Vector2D> slice)
    {
        Slice2D result = Slice2D.Create(slice);

        if (Slicer2D.complexSliceType != Slicer2D.SliceType.SliceHole)
        {
            return(result);
        }

        Polygon2D newPoly = new Polygon2D();

        bool createPoly = false;

        foreach (Pair2D pairA in Pair2D.GetList(slice, false))
        {
            foreach (Pair2D pairB in Pair2D.GetList(slice, false))
            {
                Vector2D intersection = Math2D.GetPointLineIntersectLine(pairA, pairB);
                if (intersection != null && (pairA.A != pairB.A && pairA.B != pairB.B && pairA.A != pairB.B && pairA.B != pairB.A))
                {
                    createPoly = !createPoly;
                    newPoly.AddPoint(intersection);
                }
            }
            if (createPoly == true)
            {
                newPoly.AddPoint(pairA.B);
            }
        }

        bool inHoles = false;

        foreach (Polygon2D hole in polygon.holesList)
        {
            if (hole.PolyInPoly(newPoly) == true)
            {
                inHoles = true;
            }
        }

        if (inHoles == false && newPoly.pointsList.Count > 2 && polygon.PolyInPoly(newPoly) == true)
        {
            polygon.AddHole(newPoly);

            List <Polygon2D> polys = new List <Polygon2D> (polygon.holesList);
            foreach (Polygon2D hole in polys)
            {
                if (newPoly.PolyInPoly(hole) == true)
                {
                    polygon.holesList.Remove(hole);
                    newPoly.AddHole(hole);
                }
            }

            result.AddPolygon(polygon);

            return(result);
        }

        return(result);
    }
    public Vector2List GetPointsList(float multiplier = 1f)
    {
        float sizeM  = size * multiplier;
        float sizeM2 = 2 * sizeM;

        Vector2List list = pointsList;

        if (list.Count() < 2)
        {
            return(new Vector2List(true));
        }

        List <Vector2> newPointsListA = new List <Vector2>();
        List <Vector2> newPointsListB = new List <Vector2>();

        Pair2D pair0 = Pair2D.Zero();
        Pair2D pair1 = Pair2D.Zero();

        if (list.Count() > 2)
        {
            List <DoublePair2> pairList = DoublePair2.GetList(list);

            foreach (DoublePair2 pair in pairList)
            {
                float rotA = pair.b.Atan2(pair.a);
                float rotC = pair.b.Atan2(pair.c);

                Vector2 pairA = pair.a;
                pairA = pairA.Push(rotA - Mathf.PI / 2, sizeM);

                Vector2 pairC = pair.c;
                pairC = pairC.Push(rotC + Mathf.PI / 2, sizeM);

                Vector2 vecA = pair.b;
                vecA = vecA.Push(rotA - Mathf.PI / 2, sizeM);
                vecA = vecA.Push(rotA, 10f);

                Vector2 vecC = pair.b;
                vecC = vecC.Push(rotC + Mathf.PI / 2, sizeM);
                vecC = vecC.Push(rotC, 10f);

                pair0.A.x = pairA.x;
                pair0.A.y = pairA.y;
                pair0.B.x = vecA.x;
                pair0.B.y = vecA.y;

                pair1.A.x = pairC.x;
                pair1.A.y = pairC.y;
                pair1.B.x = vecC.x;
                pair1.B.y = vecC.y;

                Vector2D result = Math2D.GetPointLineIntersectLine(pair0, pair1);

                if (result != null)
                {
                    newPointsListA.Add(result.ToVector2());
                }
            }

            if (newPointsListA.Count > 2)
            {
                newPointsListA.Remove(newPointsListA.First());
                newPointsListA.Remove(newPointsListA.Last());
            }

            foreach (DoublePair2 pair in pairList)
            {
                float rotA = pair.b.Atan2(pair.a);
                float rotC = pair.b.Atan2(pair.c);

                Vector2 pairA = pair.a;
                pairA = pairA.Push(rotA - Mathf.PI / 2, -sizeM);

                Vector2 pairC = pair.c;
                pairC = pairC.Push(rotC + Mathf.PI / 2, -sizeM);

                Vector2 vecA = pair.b;
                vecA = vecA.Push(rotA - Mathf.PI / 2, -sizeM);
                vecA = vecA.Push(rotA, 10f);

                Vector2 vecC = pair.b;
                vecC = vecC.Push(rotC + Mathf.PI / 2, -sizeM);
                vecC = vecC.Push(rotC, 10f);

                pair0.A.x = pairA.x;
                pair0.A.y = pairA.y;
                pair0.B.x = vecA.x;
                pair0.B.y = vecA.y;

                pair1.A.x = pairC.x;
                pair1.A.y = pairC.y;
                pair1.B.x = vecC.x;
                pair1.B.y = vecC.y;

                Vector2D result = Math2D.GetPointLineIntersectLine(pair0, pair1);

                if (result != null)
                {
                    newPointsListB.Add(result.ToVector2());
                }
            }

            if (newPointsListB.Count > 2)
            {
                newPointsListB.Remove(newPointsListB.First());
                newPointsListB.Remove(newPointsListB.Last());
            }
        }

        Vector2List newPointsList = new Vector2List(true);

        foreach (Vector2 p in newPointsListA)
        {
            newPointsList.Add(p);
        }

        Vector2 prevA = list.points.ElementAt(list.points.Count - 2);

        Vector2 pA = list.Last();

        pA = pA.Push(pA.Atan2(prevA) - Mathf.PI / 6, sizeM2);
        newPointsList.Add(pA);

        pA = list.Last();
        pA = pA.Push(pA.Atan2(prevA) + Mathf.PI / 6, sizeM2);
        newPointsList.Add(pA);

        newPointsListB.Reverse();

        foreach (Vector2 p in newPointsListB)
        {
            newPointsList.Add(p);
        }

        Vector2 prevB = list.points.ElementAt(1);

        Vector2 pB = list.First();

        pB = pB.Push(pB.Atan2(prevB) - Mathf.PI / 6, sizeM2);
        newPointsList.Add(pB);

        pB = list.First();
        pB = pB.Push(pB.Atan2(prevB) + Mathf.PI / 6, sizeM2);
        newPointsList.Add(pB);

        return(newPointsList);
    }
Beispiel #15
0
    static private Slice2D SliceWithoutHoles(Polygon2D polygon, Pair2D slice)
    {
        Slice2D result = Slice2D.Create(slice);

        if (polygon.LineIntersectHoles(slice).Count > 0)
        {
            Slicer2D.Debug.LogError("Slice Intersect Holes (Point Slicer?)");              // When does this happen? - Only when simple slicer is set - point slicer!!!
            return(result);
        }

        Polygon2D polyA = new Polygon2D();
        Polygon2D polyB = new Polygon2D();

        Polygon2D currentPoly = polyA;

        int collisionCount = 0;

        foreach (Pair2D p in Pair2D.GetList(polygon.pointsList))
        {
            Vector2D intersection = Math2D.GetPointLineIntersectLine(p, slice);
            if (intersection != null)
            {
                polyA.AddPoint(intersection);
                polyB.AddPoint(intersection);

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

                collisionCount++;
            }
            currentPoly.AddPoint(p.B);
        }

        if (collisionCount == 2)            // ' Is it concave split?
        {
            if (polyA.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyA);
            }

            if (polyB.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyB);
            }

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

            return(result);
        }
        if (collisionCount == 3)
        {
            Slicer2D.Debug.LogError("Linear Slice with 3 points");
        }

        if (collisionCount == 333)            // Complicated Slices With Holes
        {
            Slicer2D.Debug.Log("Slice " + collisionCount);

            if (polyA.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyA);
            }

            if (polyB.pointsList.Count() >= 3)
            {
                result.AddPolygon(polyB);
            }

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

            return(result);
        }
        return(result);
    }
Beispiel #16
0
    static public Polygon2 PreparePolygon(Polygon2 polygon, float size)
    {
        Polygon2D newPolygon = new Polygon2D();

        DoublePair2 pair  = new DoublePair2(Vector2.zero, Vector2.zero, Vector2.zero);
        Vector2D    pairA = Vector2D.Zero();
        Vector2D    pairC = Vector2D.Zero();
        Vector2D    vecA  = Vector2D.Zero();
        Vector2D    vecC  = Vector2D.Zero();

        for (int i = 0; i < polygon.points.Length; i++)
        {
            Vector2 pB = polygon.points[i];

            int indexB = i;

            int indexA = (indexB - 1);
            if (indexA < 0)
            {
                indexA += polygon.points.Length;
            }

            int indexC = (indexB + 1);
            if (indexC >= polygon.points.Length)
            {
                indexC -= polygon.points.Length;
            }

            pair.A = polygon.points[indexA];
            pair.B = pB;
            pair.C = polygon.points[indexC];

            float rotA = pair.B.Atan2(pair.A);
            float rotC = pair.B.Atan2(pair.C);

            pairA.x = pair.A.x;
            pairA.y = pair.A.y;
            pairA.Push(rotA - Mathf.PI / 2, -size);

            pairC.x = pair.C.x;
            pairC.y = pair.C.y;
            pairC.Push(rotC + Mathf.PI / 2, -size);

            vecA.x = pair.B.x;
            vecA.y = pair.B.y;
            vecA.Push(rotA - Mathf.PI / 2, -size);
            vecA.Push(rotA, 110f);

            vecC.x = pair.B.x;
            vecC.y = pair.B.y;
            vecC.Push(rotC + Mathf.PI / 2, -size);
            vecC.Push(rotC, 110f);

            Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC));

            if (result != null)
            {
                newPolygon.AddPoint(result);
            }
        }

        return(new Polygon2(newPolygon));
    }