enclosingRectangle method

List<int>? enclosingRectangle()

Bounding box of the dark modules, or null when the matrix is blank.

Implementation

List<int>? enclosingRectangle() {
  int left = width;
  int top = height;
  int right = -1;
  int bottom = -1;
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      if (!get(x, y)) continue;
      if (x < left) left = x;
      if (x > right) right = x;
      if (y < top) top = y;
      if (y > bottom) bottom = y;
    }
  }
  if (right < left || bottom < top) return null;
  return <int>[left, top, right - left + 1, bottom - top + 1];
}