ListGrid<E>.generate constructor

ListGrid<E>.generate(
  1. int width,
  2. int height,
  3. E generator(
    1. Pos pos
    ), {
  4. required E empty,
})

Creates a new list grid with the given width and height.

Each element in the grid is initialized by calling generator with the position of the element. The empty element is used as the default value.

The width and height must be positive.

Example

final grid = ListGrid.generate(2, 2, (pos) => pos.x + pos.y, empty: 0);

print(grid.get(Pos(0, 0))); // 0
print(grid.get(Pos(1, 0))); // 1

Implementation

factory ListGrid.generate(
  int width,
  int height,
  E Function(Pos pos) generator, {
  required E empty,
}) {
  checkPositive(width, 'width');
  checkPositive(height, 'height');
  final list = List<E>.generate(width * height, (index) {
    return generator(Pos.fromRowMajor(index, width: width));
  });
  return _ListGrid(width, height, empty, list);
}