BoundingBox.merge constructor
BoundingBox.merge(
- Iterable<
BoundingBox> boxes
Creates a new BoundingBox that encompasses all the bounding boxes in the provided iterable.
This factory constructor takes an iterable of BoundingBox instances and merges them into a single BoundingBox that covers all the areas defined by the individual bounding boxes. If the iterable is empty, it returns a flexible BoundingBox with all coordinates set to null.
The merging process involves calculating the minimum and maximum X and Y coordinates across all provided bounding boxes. If any bounding box in the iterable is unbounded (i.e., has null for any coordinate), the resulting bounding box will also be unbounded in that direction.
Example:
var box1 = BoundingBox(minX: 1, maxX: 3, minY: 1, maxY: 3);
var box2 = BoundingBox(minX: 2, maxX: 5, minY: 2, maxY: 5);
var mergedBox = BoundingBox.merge([box1, box2]);
print(mergedBox.minX); // Output: 1
print(mergedBox.maxX); // Output: 5
print(mergedBox.minY); // Output: 1
print(mergedBox.maxY); // Output: 5
Parameters:
boxes
: An iterable of BoundingBox instances to be merged.
Returns: A new BoundingBox instance that is the union of all bounding boxes in the iterable.
Implementation
factory BoundingBox.merge(Iterable<BoundingBox> boxes) => boxes.fold(
const BoundingBox.flexible(),
(combined, bounds) => combined.mergeWith(bounds),
);