static private Merge2D SingleMerge(Polygon2D polygon, List <Vector2D> slice) { Merge2D result = Merge2D.Create(slice); if (polygon.PointInPoly(slice.First()) == false || polygon.PointInPoly(slice.Last()) == false) { //Debug.Log("incorrect"); return(result); } slice = new List <Vector2D> (slice); MergeCollision collisionMerge = new MergeCollision(polygon, slice); if (collisionMerge.error) { // When this happens? Debug.LogWarning("Merger2D: Unexpected Error 2"); return(result); } List <Polygon2D> intersectHoles = polygon.GetListSliceIntersectHoles(slice); switch (intersectHoles.Count) { case 0: if (collisionMerge.collisionCount == 2) { return(SliceWithoutHoles(polygon, slice, collisionMerge)); } break; case 1: break; case 2: break; default: break; } return(result); }
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); }