public static List <List <Vector2> > Simplify(List <Vector2> vertices) { var polygons = new List <List <Vector2> >(); var vertexCount = vertices.Count; for (var edgeOneIndex = 0; edgeOneIndex < vertexCount - 2; edgeOneIndex++) { var lastEdgeIndex = vertexCount - 1; var edgeOne = new Segment(vertices[edgeOneIndex], vertices[(edgeOneIndex + 1) % vertexCount]); //First index is neighbor with last index if (edgeOneIndex == 0) { lastEdgeIndex--; } for (var edgeTwoIndex = edgeOneIndex + 2; edgeTwoIndex <= lastEdgeIndex; edgeTwoIndex++) { var edgeTwo = new Segment(vertices[edgeTwoIndex], vertices[(edgeTwoIndex + 1) % vertexCount]); if (Segment.Intersect(edgeOne, edgeTwo, out var intersectionPoint)) { var subPolygonOne = new List <Vector2>(); for (var i = 0; i <= edgeOneIndex; i++) { subPolygonOne.Add(vertices[i]); } subPolygonOne.Add(intersectionPoint); for (var i = edgeTwoIndex + 1; i < vertexCount; i++) { subPolygonOne.Add(vertices[i]); } var subPolygonTwo = new List <Vector2>(); subPolygonTwo.Add(intersectionPoint); for (var i = edgeOneIndex + 1; i <= edgeTwoIndex; i++) { subPolygonTwo.Add(vertices[i]); } var simplifiedSubPolygonOne = Simplify(subPolygonOne); var simplifiedSubPolygonTwo = Simplify(subPolygonTwo); for (var i = 0; i < simplifiedSubPolygonOne.Count; i++) { polygons.Add(simplifiedSubPolygonOne[i]); } for (var i = 0; i < simplifiedSubPolygonTwo.Count; i++) { polygons.Add(simplifiedSubPolygonTwo[i]); } return(polygons); } } } polygons.Add(vertices); return(polygons); }
private static bool TryFindASurgerySegment(List <Vector2> vertices, List <List <Vector2> > holes, int holeIndex, ref int holeVertexIndex, Segment otherSurgerySegment, HashSet <int> vertexIndexSet, out SurgerySegment surgerySegment) { var holeCount = holes.Count; var hole = holes[holeIndex]; var holeVertexCount = hole.Count; for (; holeVertexIndex < holeVertexCount; holeVertexIndex++) { var holeVertex = hole[holeVertexIndex]; var vertexCount = vertices.Count; for (var vertIndex = 0; vertIndex < vertexCount; vertIndex++) { if (vertexIndexSet.Contains(vertIndex)) { continue; } var vertex = vertices[vertIndex]; var segment = new Segment(holeVertex, vertex); var intersection = Segment.Intersect(segment, otherSurgerySegment); if (intersection) { continue; } var nextHoleVertexIndex = (holeVertexIndex + 1) % holeVertexCount; for (var j = 0; j < holeVertexCount - 2; j++) { var index = (nextHoleVertexIndex + j) % holeVertexCount; var comparedSegment = new Segment(hole[index], hole[(index + 1) % holeVertexCount]); if (Segment.Intersect(segment, comparedSegment)) { intersection = true; break; } } if (intersection) { continue; } for (var j = 0; j < holeCount; j++) { if (j == holeIndex) { continue; } var comparedHole = holes[j]; if (Segment.Intersect(segment, comparedHole)) { intersection = true; break; } } if (intersection) { continue; } var nextVertexIndex = (vertIndex + 1) % vertexCount; for (var j = 0; j < vertexCount - 2; j++) { var index = (nextVertexIndex + j) % vertexCount; var comparedSegment = new Segment(vertices[index], vertices[(index + 1) % vertexCount]); if (Segment.Intersect(segment, comparedSegment)) { intersection = true; break; } } if (!intersection) { // Debug.DrawLine(segment.p, segment.q, Color.red, 10); vertexIndexSet.Add(vertIndex); surgerySegment = new SurgerySegment(holeVertexIndex, vertIndex); return(true); } } } surgerySegment = new SurgerySegment(); return(false); }