1.0.0 • Published 5 years ago

grid2d v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

grid2d

Build Status

NPM

Calculate and manipulate 2-dimensional grids in node and browser.

Usage

grid2d provides utilities for working with grids as plain-objects. This library is written in TypeScript to be self-documenting.

import * as grid2d from 'grid2d';

//Grid, Cell, Point and Position are plain-object structs
//If you are writing javascript, just leave out the ": <type>" parts:
let grid : Grid = {
	columns: 4,
	rows: 3
};

const p : Position = {
	column: 3,
	row: 2
};

//grids are row-major by default
let i : number = grid2d.cellIndex(grid, p); //<- 11

//a grid without a width or height will default to 1
const x : number = grid2d.xForColumn(grid, 2); //<- 0.5


//this will create Cells for the selected columns (2-4) and rows (1-2)
const cells: Cell[] = grid2d.createCellsBetween({ columns: 4, rows: 5 }, { column: 2, row: 1 }, { column: 4, row: 2 });
// result is:
// [ { x: 0.5, y: 0.2, width: 0.25, height: 0.2 },
//  { x: 0.5, y: 0.4, width: 0.25, height: 0.2 },
//   { x: 0.75, y: 0.2, width: 0.25, height: 0.2 },
//   { x: 0.75, y: 0.4, width: 0.25, height: 0.2 },
//   { x: 1, y: 0.2, width: 0.25, height: 0.2 },
//   { x: 1, y: 0.4, width: 0.25, height: 0.2 } ]

API

Interfaces

interface Point {
    x?: number;
    y?: number;
}

interface Cell extends Point {
    width?: number;
    height?: number;
}

interface Grid extends Cell {
    columns: number;
    rows: number;
    paddingLeft?: number;
    paddingRight?: number;
    paddingTop?: number;
    paddingBottom?: number;
    outerPadding?: boolean;
    rowMajor?:boolean;
}

interface Position {
    column: number;
    row: number;
}

Grid Re-work

bounds(cells:Cell[]| Grid) => Cell;

cells((grid: Grid, arr?: Cell[]) => Cell[];

cellsRange(grid: Grid, posStart_columnStart: Position | number, posStop_columnStop: Position | number, [rowStart: number, rowStop: number]) => Cell[];

cellForIndex(grid: Grid, index: number, cell?: Cell) => Cell;

cellForPosition(grid: Grid, c: number | Position, r?: number | Cell, cell?: Cell) => Cell;

closestCell(grid: Grid, point: Point) => Cell;

closestCellPosition(grid: Grid, point: Point) => Position;

closestCellIndex(grid: Grid, point: Point) => number;

cellIndex(grid: Grid, c: number | Position, r?: number) => number;

cellPosition(grid: Grid, i: number | Cell): Position;

cellWidth(grid: Grid) => number;

cellHeight(grid: Grid) => number;

xForColumn(grid: Grid, n: number) => number;

yForRow(grid: Grid, n: number) => number;

intersectsCell(grid: Grid, point: Point) => Cell;

intersectsCellPosition(grid: Grid, point: Point) => Position;

intersectsCellIndex(grid: Grid, point: Point) => number;

scale(grid: Grid, scaleX: number, scaleY: number, scaledGrid?: Grid) => Grid;

Grid

cellBounds(grid: Grid) => Cell;

createCells((grid: Grid, arr?: Cell[]) => Cell[];

createCellsBetween(grid: Grid, posStart_columnStart: Position | number, posStop_columnStop: Position | number, [rowStart: number, rowStop: number]) => Cell[];

createCellForIndex(grid: Grid, index: number, cell?: Cell) => Cell;

createCellForPosition(grid: Grid, c: number | Position, r?: number | Cell, cell?: Cell) => Cell;

closestCell(grid: Grid, point: Point) => Cell;

closestCellPosition(grid: Grid, point: Point) => Position;

closestCellIndex(grid: Grid, point: Point) => number;

cellIndex(grid: Grid, c: number | Position, r?: number) => number;

cellPosition(grid: Grid, i: number | Cell): Position;

cellWidth(grid: Grid) => number;

cellHeight(grid: Grid) => number;

xForColumn(grid: Grid, n: number) => number;

yForRow(grid: Grid, n: number) => number;

intersectsCell(grid: Grid, point: Point) => Cell;

intersectsCellPosition(grid: Grid, point: Point) => Position;

intersectsCellIndex(grid: Grid, point: Point) => number;

scale(grid: Grid, scaleX: number, scaleY: number, scaledGrid?: Grid) => Grid;

Cell

contains(cell: Cell | Grid, point: Point) => boolean;

bottomLeft(cell: Cell) => Point;

bottomRight(cell: Cell) => Point;

center(cell: Cell) => Point;

topLeft(cell: Cell) => Point;

topRight(cell: Cell) => Point;

cellsIntersect(a: Cell, b:Cell) => boolean;

shiftCells(grid: any, params: any) => any;

Position

sortByGridPosition(a: Position, b: Position) => number;

License

MIT, see LICENSE.md for details.

2.0.0-alpha1

5 years ago

1.0.0

6 years ago

0.1.0

9 years ago