0.1.0 • Published 9 years ago
@mkennedy3000/grid v0.1.0
Grid
Typescript Immutable 2d grid with transformation functions.
Install
npm install @mkennedy3000/gridUsage
Create
constructor
const grid = new Grid([1,2,3,4,5,6], 3, 2);1 2 3
4 5 6withRows()
const grid = Grid.withRows([[1,2,3],[4,5,6]]);1 2 3
4 5 6withCols()
const grid = Grid.withCols([[1,4],[2,5],[3,6]]);1 2 3
4 5 6Reading the Grid
get
const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.get(0,1); // => 4
grid.get(1,0); // => 2height
const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.height; // => 2numRows
const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.numRows; // => 2rows
const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.rows; // => [[1,2,3],[4,5,6]]width
const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.width; // => 3numCols
const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.numCols; // => 3cols
const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.numRows; // => [[1,4],[2,5],[3,6]]Transforms
Transforms return an entirely new Grid. The grid the transform was called on is unaffected.
transpose()
const grid = Grid.withRows([[1,2,3],[4,5,6]]).transpose().transpose();1 2 3 => 1 4 => 1 2 3
4 5 6 2 5 4 5 6
3 6flipX()
const grid = Grid.withRows([[1,2,3],[4,5,6]]).flipX();1 2 3 => 3 2 1
4 5 6 6 5 4flipY()
const grid = Grid.withRows([[1,2,3],[4,5,6]]).flipY();1 2 3 => 4 5 6
4 5 6 1 2 3rotate90()
const grid = Grid.withRows([[1,2,3],[4,5,6]]).rotate90();1 2 3 => 4 1
4 5 6 5 2
6 3rotate180()
const grid = Grid.withRows([[1,2,3],[4,5,6]]).rotate180();1 2 3 => 6 5 4
4 5 6 3 2 1rotate270()
const grid = Grid.withRows([[1,2,3],[4,5,6]]).rotate270();1 2 3 => 3 6
4 5 6 2 5
1 40.1.0
9 years ago