Visiting cells
Simple enumeration
By.cells iterates over all cell positions. It runs left-to-right, top-to-bottom.
By default it starts at { x: 0, y: 0 }
, but this can be overrided, and you can also ask it to wrap from end to start to ensure all cells are visited.
You can also iterate by column or row , where you get the whole column/row as an array, rather than getting individual cell positions.
Creating a visitor
Visit.create lets you visit cells from a position using one of several in-built logics.
Logic | Description |
---|---|
row | left-to-right, top-to-bottom |
column | top-to-bottom, left-to-right |
neighbours | neighbours surrounding cell |
breadth | breadth-first |
depth | depth-first |
random | any random cell in grid |
random-contiguous | any random cell neighbouring an already visited cell |
create
returns a function capturing the traversal logic. This can then be used with your grid of choice.
The function in turn gives a generator for iterating over cell positions:
Options
create
takes options for controlling behaviour when the bounds of the grid are reached, amongst others.
In summary, the options are:
Example usage:
boundsWrap
is Type Grids.GridBoundsLogic . undefined is the default in most operations.
Logic | Description |
---|---|
unbounded | Cell positions allowed to go past grid row/cols |
undefined | Yields undefined when attempting to read outside of limits |
stop | Yield the nearest legal position |
wrap | Wrap around, eg go from end to start of grid |
reversed
runs visitor in reverse, where possible. This doesn’t do anything for the random visitors.
start
specifies the starting cell. By default, { x:0, y:0 }
.
visited
is sometimes useful if you want to monitor which cells have been visited. If you pass in a set, the function will use this internally. The set gets created internally otherwise, but you don’t get access to it.
Stepper
Visit.stepper runs the visitor for a certain number of steps, returning the final position. The stepper remembers its position, so it becomes a way to asynchronously step through a grid in a way which is not as easy with the generator.
For example, this allows you to get the position three cells away row-wise.