getPolys method

List<Polygon>? getPolys(
  1. Map<String, Edge> edges
)

Implementation

List<Polygon>? getPolys(Map<String, Edge> edges) {
  List<Polygon> polygons = [];
  List<String> keys = edges.keys.toList();
  if (keys.isNotEmpty) {
    List<String> pointToAdd = [edges[keys[0]]!.p1, edges[keys[0]]!.p2];
    edges.remove(keys[0]);

    while (edges.isNotEmpty) {
      bool contin = false;

      for (String edge in edges.keys) {
        if (edges[edge]!.containsPoint(pointToAdd.first)) {
          String point = edges[edge]!.getNextPoint(pointToAdd.first);
          pointToAdd.insert(0, point);
          edges.remove(edge);
          contin = true;
          break;
        } else if (edges[edge]!.containsPoint(pointToAdd.last)) {
          String point = edges[edge]!.getNextPoint(pointToAdd.last);
          pointToAdd.add(point);
          edges.remove(edge);
          contin = true;
          break;
        }
      }

      if (!contin) {
        polygons.add(Polygon(pointToAdd));
        List<String> keys = edges.keys.toList();
        pointToAdd = [edges[keys[0]]!.p1, edges[keys[0]]!.p2];
        edges.remove(0);
      }
    }
    polygons.add(Polygon(pointToAdd));
    return polygons;
  }
  return null;
}