Пример #1
0
        // Slice From Point
        static public Slice2D SliceFromPoint(Polygon2D polygon, Vector2D point, float rotation)
        {
            Slice2D result = Slice2D.Create(null, point, rotation);

            // Normalize into clockwise
            polygon.Normalize();

            Vector2D sliceA = new Vector2D(point);
            Vector2D sliceB = new Vector2D(point);

            sliceA.Push(rotation, 1e+10f / 2);
            sliceB.Push(rotation, -1e+10f / 2);

            if (polygon.PointInPoly(point) == false)
            {
                return(result);
            }

            // Getting the list of intersections
            List <Vector2D> intersectionsA = polygon.GetListLineIntersectPoly(new Pair2D(point, sliceA));
            List <Vector2D> intersectionsB = polygon.GetListLineIntersectPoly(new Pair2D(point, sliceB));

            // Sorting intersections from one point
            if (intersectionsA.Count > 0 && intersectionsB.Count > 0)
            {
                sliceA = Vector2DList.GetListSortedToPoint(intersectionsA, point) [0];
                sliceB = Vector2DList.GetListSortedToPoint(intersectionsB, point) [0];
            }
            else
            {
                return(result);
            }

            List <Pair2D> collisionList = new List <Pair2D>();

            collisionList.Add(new Pair2D(sliceA, sliceB));

            result.AddPolygon(polygon);

            foreach (Pair2D id in collisionList)
            {
                result.AddCollision(id.A);
                result.AddCollision(id.B);

                // Sclice line points generated from intersections list
                Vector2D vec0 = new Vector2D(id.A);
                Vector2D vec1 = new Vector2D(id.B);

                double rot = Vector2D.Atan2(vec0, vec1);

                // Slightly pushing slice line so it intersect in all cases
                vec0.Push(rot, LinearSlicer.precision);
                vec1.Push(rot, -LinearSlicer.precision);

                // For each in polygons list attempt convex split
                List <Polygon2D> temp = new List <Polygon2D>(result.GetPolygons());               // necessary?
                foreach (Polygon2D poly in temp)
                {
                    // NO, that's the problem
                    Slice2D resultList = LinearSlicer.Slice(poly, new Pair2D(vec0, vec1));

                    if (resultList.GetPolygons().Count > 0)
                    {
                        if (resultList.slices.Count > 0)
                        {
                            foreach (List <Vector2D> i in resultList.slices)
                            {
                                result.AddSlice(i);
                            }
                        }

                        foreach (Polygon2D i in resultList.GetPolygons())
                        {
                            result.AddPolygon(i);
                        }

                        // If it's possible to perform splice, remove parent polygon from result list
                        result.RemovePolygon(poly);
                    }
                }
            }
            result.RemovePolygon(polygon);
            return(result);
        }
Пример #2
0
        static private Slice2D MultipleSlice(Polygon2D polygon, List <Vector2D> slice)
        {
            Slice2D result = Slice2D.Create(null, slice);

            List <ComplexSlicerSplit> slices = ComplexSlicerSplit.GetSplitSlices(polygon, slice);

            if (slices.Count < 1)
            {
                return(result);
            }

            // Adjusting split lines before performing convex split
            result.AddPolygon(polygon);

            foreach (ComplexSlicerSplit id in slices)
            {
                if (id.points.Count > 1)
                {
                    foreach (Vector2D p in id.points)
                    {
                        result.AddCollision(p);
                    }

                    // Sclice line points generated from intersections list
                    Vector2D vec0 = id.points.First();
                    vec0.Push(Vector2D.Atan2(vec0, id.points[1]), precision);

                    Vector2D vec1 = id.points.Last();
                    vec1.Push(Vector2D.Atan2(vec1, id.points[id.points.Count - 2]), precision);

                    // For each in polygons list attempt convex split
                    List <Polygon2D> resultPolygons = new List <Polygon2D>(result.GetPolygons());                   // necessary?
                    foreach (Polygon2D poly in resultPolygons)
                    {
                        Slice2D resultList = SingleSlice(poly, id);

                        if (resultList.GetPolygons().Count > 0)
                        {
                            if (resultList.slices.Count > 0)
                            {
                                foreach (List <Vector2D> resultSlice in resultList.slices)
                                {
                                    result.AddSlice(resultSlice);
                                }
                            }

                            foreach (Polygon2D resultPoly in resultList.GetPolygons())
                            {
                                result.AddPolygon(resultPoly);
                            }

                            // If it's possible to perform convex split, remove parent polygon from result list
                            result.RemovePolygon(poly);
                        }
                    }
                }
            }

            //Debug.Log("1 " + slices[0].Count + " " + slices[0][0].ToVector2() + " " + slices[0][1].ToVector2());
            //Debug.Log("2 " + slices[1].Count + " " + slices[1][0].ToVector2() + " " + slices[1][1].ToVector2());

            // if exist then remove slice was not performed
            result.RemovePolygon(polygon);
            return(result);
        }
Пример #3
0
        static private Slice2D MultipleSlice(Polygon2D polygon, Pair2D slice)
        {
            Slice2D result = Slice2D.Create(null, slice);

            List <Pair2D> slices = GetSplitSlices(polygon, slice);

            //Debug.Log(slices.Count);

            //Debug.Log(slices[0].A.ToVector2() + " " + slices[0].B.ToVector2());
            //Debug.Log(slices[1].A.ToVector2() + " " + slices[1].B.ToVector2());

            if (slices.Count < 1)
            {
                return(result);
            }

            result.AddPolygon(polygon);

            // Slice line points generated from intersections list
            foreach (Pair2D id in slices)
            {
                result.AddCollision(id.A);
                result.AddCollision(id.B);

                Vector2D vec0 = new Vector2D(id.A);
                Vector2D vec1 = new Vector2D(id.B);

                double rot = Vector2D.Atan2(vec0, vec1);

                // Slightly pushing slice line so it intersect in all cases
                vec0.Push(rot, precision);
                vec1.Push(rot, -precision);

                Pair2D line = new Pair2D(vec0, vec1);

                // For each in polygons list attempt convex split
                foreach (Polygon2D poly in (new List <Polygon2D>(result.GetPolygons())))
                {
                    Slice2D resultList = SingleSlice(poly, line);

                    if (resultList.GetPolygons().Count > 0)
                    {
                        if (resultList.slices.Count > 0)
                        {
                            foreach (List <Vector2D> i in resultList.slices)
                            {
                                result.AddSlice(i);
                            }
                        }

                        result.AddSlice(line);

                        foreach (Polygon2D i in resultList.GetPolygons())
                        {
                            result.AddPolygon(i);
                        }

                        // If it's possible to perform splice, remove currently sliced polygon from result list
                        result.RemovePolygon(poly);
                    }
                }
            }

            result.RemovePolygon(polygon);
            return(result);
        }