0.1.0 • Published 8 years ago

@mkennedy3000/grid v0.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

Grid

Typescript Immutable 2d grid with transformation functions.

Install

npm install @mkennedy3000/grid

Usage

Create

Reading the Grid

Transforms

Create

constructor

const grid = new Grid([1,2,3,4,5,6], 3, 2);
1 2 3
4 5 6

withRows()

const grid = Grid.withRows([[1,2,3],[4,5,6]]);
1 2 3
4 5 6

withCols()

const grid = Grid.withCols([[1,4],[2,5],[3,6]]);
1 2 3
4 5 6

Reading the Grid

get

const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.get(0,1); // => 4
grid.get(1,0); // => 2

height

const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.height; // => 2

numRows

const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.numRows; // => 2

rows

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; // => 3

numCols

const grid = Grid.withRows([[1,2,3],[4,5,6]]);
grid.numCols; // => 3

cols

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 6

flipX()

const grid = Grid.withRows([[1,2,3],[4,5,6]]).flipX();
1 2 3 => 3 2 1
4 5 6    6 5 4

flipY()

const grid = Grid.withRows([[1,2,3],[4,5,6]]).flipY();
1 2 3 => 4 5 6
4 5 6    1 2 3

rotate90()

const grid = Grid.withRows([[1,2,3],[4,5,6]]).rotate90();
1 2 3 => 4 1
4 5 6    5 2
         6 3

rotate180()

const grid = Grid.withRows([[1,2,3],[4,5,6]]).rotate180();
1 2 3 => 6 5 4
4 5 6    3 2 1

rotate270()

const grid = Grid.withRows([[1,2,3],[4,5,6]]).rotate270();
1 2 3 => 3 6
4 5 6    2 5
         1 4