Grid

Utility class for 2D patterns

constructor

new Grid(w,h)

  • w: int width
  • h: int height

grid.set(x,y,v)

  • x: col index
  • y: row index
  • v: truthy or falsy value

grid.get(x,y)

  • x: col index
  • y: row index

grid.used(x,y)

  • x: col index
  • y: row index

grid.tiles(v)

Returns: [x,y] iterator with value matching v

  • if v == null, returns all used coordinates
  • if v == true, returns all on coordinates
  • if v == false, returns all off coordinates
import { Grid } from 'qrsart'

let grid = new Grid(10,10)
grid.set(4,4,1)
grid.set(4,3,0)
grid.set(3,3,true)
grid.set(3,4,false)

for(let [x,y] of grid.tiles()){
  ctx.fillStyle = grid.get(x,y) ? 'red' : 'blue'
  ctx.fillRect(x,y,1,1)
}

Grid.union(...grids)

Returns: Grid

Combine all used coordinates. If any are true, cell becomes true/

Grid.subtract(og,...grids)

Returns: Grid

Removes used coordinates in any of grids from og.

Grid.intersect(og,...grids)

Returns: Grid

Only include common cells.