gmatrix
v1.6.0
Published
A simple library for geometrically manipulating matrices
Downloads
3
Readme
gmatrix
A simple library for geometrically manipulating matrices
Install
npm install gmatrix
Usage
For Node:
const matrix = require("gmatrix");
For browsers:
<script src="/node_modules/gmatrix/dist/gmatrix.min.js"></script>
In either case, create a matrix:
const myMatrix = matrix([
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]
]);
// myMatrix is now equipped with the geometric functions detailed below
toJSON
const json = myMatrix.toJSON();
// json is a two dimensional array of the matrix
width
const width = myMatrix.width();
// width is 5
height
const height = myMatrix.height();
// height is 5
get
// by index
const value = myMatrix.get(3);
// 2
// or by row & column index
const value = myMatrix.get(0, 3);
// 3
equals
const a = matrix([
[1, 2],
[3, 4]
]);
const b = matrix([
[5, 6],
[7, 8]
]);
const equality = a.equals(b);
// false
clone
const clone = myMatrix.clone();
// clone is a new matrix that's a copy of the original
subMatrix
const sub = myMatrix.subMatrix(5, 2, 2);
// sub is a new 2x2 matrix starting from index 4
// [[5, 6],
// [10, 11]]
surrounding
const surrounding = myMatrix.surrounding(12);
// surrounding is a new 3x3 matrix containing the elements that surrounded index 4
// [[6, 7, 8],
// [11, 12, 13],
// [16, 17, 18]]
forEach
myMatrix.forEach((element, index, rowIndex, columnIndex) => {
// do something with element
// given element's index, row index, and column index
// return false to break early
});
forEachRow
myMatrix.forEachRow((row, rowIndex) => {
// do something with row of elements
// given row's index
// return false to break early
});
map
const results = myMatrix.map((element, index, rowIndex, columnIndex) => {
// similar to forEach but returns a 2d array of results based on what you return in the callback
// given element's index, row index, and column index
return element + 1;
});
// results is 2d array of each return
// [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], etc...]
flatMap
const results = myMatrix.flatMap((element, index, rowIndex, columnIndex) => {
// similar to forEach but returns a flattened array of results based on what you return in the callback
// given element's index, row index, and column index
return element; // by returning element we will be creating an flattened array of the matrix values
});
// results is flattened array of each return
// [0, 1, 2, 3, 4, 5, 6, 7, 8, etc...]
rotateLeft
const rotatedMatrix = myMatrix.rotateLeft();
// rotatedMatrix is a new matrix rotated 90 degrees to the left
rotateRight
const rotatedMatrix = myMatrix.rotateRight();
// rotatedMatrix is a new matrix rotated 90 degrees to the right
rotate180
const rotatedMatrix = myMatrix.rotate180();
// rotatedMatrix is a new matrix rotated 180 degrees
flipHorizontal
const flippedMatrix = myMatrix.flipHorizontal();
// flippedMatrix is a new matrix flipped over the horizontal axis
flipVertical
const flippedMatrix = myMatrix.flipVertical();
// flippedMatrix is a new matrix flipped over the vertical axis