getRect<E> function
Returns each element within bounds
, exclusive of the bottom-right edge.
The get
function is called with each position within the bounds, and the
return value is lazily yielded.
It is the caller's responsibility to ensure that the bounds are valid or the behavior is undefined. This function is intended as a low-level utility for grid-like structures; consider asserting or clamping the bounds if necessary before calling this function.
The result is in row-major order.
Example
For the below examples, we use a simple 2-dimensional list as the grid-like structure.
Consider package:sector
for a complete
implementation.
All elements in a grid
final grid = [
[1, 2, 3],
[4, 5, 6],
];
final allElemennts = getRect(
Rect.fromWH(3, 2),
(pos) => grid[pos.y][pos.x],
);
print(allElemennts); // => [1, 2, 3, 4, 5, 6]
Sub-elements in a grid
final subElements = getRect(
Rect.fromLTWH(1, 0, 2, 1),
(pos) => grid[pos.y][pos.x],
);
print(subElements); // => [2, 3]
Implementation
Iterable<E> getRect<E>(Rect bounds, E Function(Pos) get) {
return bounds.positions.map(get);
}