sortRectangles static method

void sortRectangles(
  1. List<IntRect> list, {
  2. double threshold = 5.0,
})

Sorts a list of IntRect objects based on their vertical and horizontal positions.

This method first compares the vertical center positions of rectangles. If two rectangles are approximately on the same line (within the specified threshold), it sorts them from left to right based on their horizontal position. Otherwise, it sorts them from top to bottom.

Parameters: list: The list of IntRect objects to sort. threshold: The maximum vertical distance (in pixels) for rectangles to be considered on the same line. Defaults to 5.0.

Implementation

static void sortRectangles(List<IntRect> list, {double threshold = 5.0}) {
  list.sort((a, b) {
    // If the vertical difference is within the threshold, treat them as the same row
    if ((a.center.y - b.center.y).abs() <= threshold) {
      return a.center.x.compareTo(
        b.center.x,
      ); // Sort by X-axis if on the same line
    }
    return a.center.y.compareTo(b.center.y); // Otherwise, sort by Y-axis
  });
}