/** * This script provides a library for performing affine matrix operations * inspired by the [glMatrix library](http://glmatrix.net/) developed by * Toji and SinisterChipmunk. * * Unlike glMatrix, this library does not have operations for vectors. * However, my VectorMath script provides a library providing many kinds of * common vector operations. * * This project has no behavior on its own, but its functions are used by * other scripts to do some cool things, particular for math involving 2D and * 3D geometry. */ var MatrixMath = (function() { /** * An NxN square matrix, represented as a 2D array of numbers in column-major * order. For example, mat[3][2] would get the value in column 3 and row 2. * order. * @typedef {number[][]} Matrix */ /** * An N-degree vector. * @typedef {number[]} Vector */ /** * Gets the adjugate of a matrix, the tranpose of its cofactor matrix. * @param {Matrix} mat * @return {Matrix} */ function adjoint(mat) { var cofactorMat = MatrixMath.cofactorMatrix(mat); return MatrixMath.transpose(cofactorMat); } /** * Produces a clone of an NxN square matrix. * @param {Matrix} mat * @return {Matrix} */ function clone(mat) { return _.map(mat, function(column) { return _.map(column, function(value) { return value; }); }); } /** * Gets the cofactor of a matrix at a specified column and row. * @param {Matrix} mat * @param {uint} col * @param {uint} row * @return {number} */ function cofactor(mat, col, row) { return Math.pow(-1, col+row)*MatrixMath.minor(mat, col, row); } /** * Gets the cofactor matrix of a matrix. * @param {Matrix} mat * @return {Matrix} */ function cofactorMatrix(mat) { var result = []; var size = MatrixMath.size(mat); for(var col=0; col tolerance) return false; } } return true; } /** * Produces an identity matrix of some size. * @param {uint} size * @return {Matrix} */ function identity(size) { var mat = []; for(var col=0; col