static public Slice2D Explode(Polygon2D polygon, int explosionSlices = 0) { if (explosionSlices < 1) { explosionSlices = Slicer2DSettings.GetExplosionSlices(); } Slice2D result = Slice2D.Create(null, Slice2DType.Explode); Rect polyRect = polygon.GetBounds(); result.AddPolygon(polygon); int tries = 0; while (result.GetPolygons().Count < explosionSlices) { foreach (Polygon2D p in new List <Polygon2D>(result.GetPolygons())) { Slice2D newResult = SliceFromPoint(p, new Vector2D(polyRect.x + UnityEngine.Random.Range(0, polyRect.width), polyRect.y + UnityEngine.Random.Range(0, polyRect.height)), UnityEngine.Random.Range(0, Mathf.PI * 2)); /* * Polygon2D smallest = Polygon2DList.GetSmallest(newResult.GetPolygons()); * if (smallest != null) { * Debug.Log(smallest.GetArea()); * * if (smallest.GetArea() < 1) { * continue; * } * }*/ if (newResult.GetPolygons().Count > 0) { if (newResult.slices.Count > 0) { foreach (List <Vector2D> i in newResult.slices) { result.AddSlice(i); } } foreach (Polygon2D poly in newResult.GetPolygons()) { result.AddPolygon(poly); } result.RemovePolygon(p); } } tries += 1; if (tries > 20) { return(result); } } return(result); }
static public Slice2D ExplodeByPoint(Polygon2D polygon, Vector2D point, int explosionSlices = 0) { if (explosionSlices < 1) { explosionSlices = Slicer2DSettings.GetExplosionSlices(); } Slice2D result = Slice2D.Create(null, point); if (polygon.PointInPoly(point) == false) { return(result); } float sliceRot = UnityEngine.Random.Range(0, 360); Polygon2D hole = Polygon2D.Create(Polygon2D.PolygonType.Rectangle, 0.1f); hole = hole.ToOffset(point); polygon.AddHole(hole); result.AddPolygon(polygon); int tries = 0; while (result.GetPolygons().Count < explosionSlices) { foreach (Polygon2D p in new List <Polygon2D>(result.GetPolygons())) { sliceRot += UnityEngine.Random.Range(15, 45) * Mathf.Deg2Rad; Vector2D v = new Vector2D(point); v.Push(sliceRot, 0.4f); Slice2D newResult = SliceFromPoint(p, v, sliceRot); if (newResult.GetPolygons().Count > 0) { if (newResult.slices.Count > 0) { foreach (List <Vector2D> i in newResult.slices) { result.AddSlice(i); } } foreach (Polygon2D poly in newResult.GetPolygons()) { result.AddPolygon(poly); } result.RemovePolygon(p); } } tries += 1; if (tries > 20) { return(result); } } return(result); }