decodePolygon method

List<List<List<List<int>>>> decodePolygon()

Implementation

List<List<List<List<int>>>> decodePolygon() {
  int length = 0;
  int commandId = 0;
  int x = 0;
  int y = 0;
  bool isX = true;
  List<List<List<List<int>>>> polygons = [];
  List<List<List<int>>> coords = [];
  List<List<int>> ring = [];

  this.geometryList!.forEach((commandInt) {
    if (length <= 0 || commandId == CommandID.ClosePath) {
      Command command = Command.CommandInteger(command: commandInt);

      commandId = command.id;
      length = command.count;

      if (commandId == CommandID.ClosePath) {
        coords.add(ring.reversed.toList(growable: false));
        ring = [];
      }
    } else if (commandId != CommandID.ClosePath) {
      if (isX) {
        x += Command.zigZagDecode(commandInt);
        isX = false;
      } else {
        y += Command.zigZagDecode(commandInt);
        ring.add([x, y]);
        length -= 1;
        isX = true;
      }
    }

    if (length <= 0 && commandId == CommandID.LineTo) {
      if (coords.isNotEmpty && this._isCCW(ring: ring)) {
        polygons.add(coords);
        coords = [];
      }
    }
  });

  polygons.add(coords);
  return polygons;
}