npm.io
0.0.1-2 • Published 2 years ago

@pomle/grid

Licence
MIT
Version
0.0.1-2
Deps
0
Size
54 kB
Vulns
0
Weekly
0
Stars
1

Grid

A JavaScript and TypeScript lib for working with grids of any size and any dimension.

Usage

Write a number to each cell in a 2x2 grid

import { Grid } from '@pomle/grid';

const grid = new Grid<number, 'x' | 'y'>({ x: 2, y: 2 });

let count = 0;
for (const coord of grid) {
  count += 1;
  grid.set(coord, count);
}
Dimensions can use any name
import { Grid } from '@pomle/grid';

const grid = new Grid({ width: 320, height: 240 });
Dimensions can use number of dimensions
import { Grid } from '@pomle/grid';

const grid = new Grid({ a: 10, b: 10, c: 10, d: 10 });
grid.set({ a: 1, b: 1, c: 1, d: 1 }, 'one');
grid.set({ a: 2, b: 2, c: 2, d: 2 }, 'two');
Neighbours can be explored
import { Grid } from '@pomle/grid';

const grid = new Grid({ x: 3, y: 3 });
for (const coord of grid.neighbours({ x: 1, y: 1 })) {
  console.log(coord); // Prints cartesian product of {x: 0, y: 0} to {x: 2, y: 2}
}

Keywords