├── LICENSE.md ├── README.md ├── adjoint.js ├── clone.js ├── copy.js ├── create.js ├── determinant.js ├── frob.js ├── fromMat2.js ├── fromMat4.js ├── fromQuat.js ├── identity.js ├── index.js ├── invert.js ├── multiply.js ├── normalFromMat4.js ├── package.json ├── rotate.js ├── scale.js ├── str.js ├── translate.js └── transpose.js /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Brandon Jones, Colin MacKenzie IV, Hugh Kennedy 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 16 | 2. Altered source versions must be plainly marked as such, and must not 17 | be misrepresented as being the original software. 18 | 19 | 3. This notice may not be removed or altered from any source distribution. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gl-mat3 [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 2 | 3 | Part of a fork of [@toji](http://github.com/toji)'s 4 | [gl-matrix](http://github.com/toji/gl-matrix) split into smaller pieces: this 5 | package contains `glMatrix.mat3`. 6 | 7 | ## Usage 8 | 9 | [![NPM](https://nodei.co/npm/gl-mat3.png)](https://nodei.co/npm/gl-mat3/) 10 | 11 | ### `mat3 = require('gl-mat3')` 12 | 13 | Will load all of the module's functionality and expose it on a single 14 | object. Note that any of the methods may also be required directly 15 | from their files. 16 | 17 | For example, the following are equivalent: 18 | 19 | ``` javascript 20 | var scale = require('gl-mat3').scale 21 | var scale = require('gl-mat3/scale') 22 | ``` 23 | 24 | ## API 25 | 26 | - [mat3.adjoint()](#mat3adjointoutmat3amat3) 27 | - [mat3.clone()](#mat3cloneamat3) 28 | - [mat3.copy()](#mat3copyoutmat3amat3) 29 | - [mat3.create()](#mat3create) 30 | - [mat3.determinant()](#mat3determinantamat3) 31 | - [mat3.frob()](#mat3frobamat3) 32 | - [mat3.fromMat2d()](#mat3frommat2doutmat3amat2d) 33 | - [mat3.fromMat4()](#mat3frommat4outmat3amat4) 34 | - [mat3.fromQuat()](#mat3fromquatoutmat3qquat) 35 | - [mat3.identity()](#mat3identityoutmat3) 36 | - [mat3.invert()](#mat3invertoutmat3amat3) 37 | - [mat3.multiply()](#mat3multiplyoutmat3amat3bmat3) 38 | - [mat3.normalFromMat4()](#mat3normalfrommat4outmat3amat4) 39 | - [mat3.rotate()](#mat3rotateoutmat3amat3radnumber) 40 | - [mat3.scale()](#mat3scaleoutmat3amat3vvec2) 41 | - [mat3.str()](#mat3strmatmat3) 42 | - [mat3.translate()](#mat3translateoutmat3amat3vvec2) 43 | - [mat3.transpose()](#mat3transposeoutmat3amat3) 44 | 45 | ## mat3.adjoint(out:mat3, a:mat3) 46 | 47 | Calculates the adjugate of a mat3 48 | 49 | ## mat3.clone(a:mat3) 50 | 51 | Creates a new mat3 initialized with values from an existing matrix 52 | 53 | ## mat3.copy(out:mat3, a:mat3) 54 | 55 | Copy the values from one mat3 to another 56 | 57 | ## mat3.create() 58 | 59 | Creates a new identity mat3 60 | 61 | ## mat3.determinant(a:mat3) 62 | 63 | Calculates the determinant of a mat3 64 | 65 | ## mat3.frob(a:mat3) 66 | 67 | Returns Frobenius norm of a mat3 68 | 69 | ## mat3.fromMat2d(out:mat3, a:mat2d) 70 | 71 | Copies the values from a mat2d into a mat3 72 | 73 | ## mat3.fromMat4(out:mat3, a:mat4) 74 | 75 | Copies the upper-left 3x3 values into the given mat3. 76 | 77 | ## mat3.fromQuat(out:mat3, q:quat) 78 | 79 | Calculates a 3x3 matrix from the given quaternion 80 | 81 | ## mat3.identity(out:mat3) 82 | 83 | Set a mat3 to the identity matrix 84 | 85 | ## mat3.invert(out:mat3, a:mat3) 86 | 87 | Inverts a mat3 88 | 89 | ## mat3.multiply(out:mat3, a:mat3, b:mat3) 90 | 91 | Multiplies two mat3's 92 | 93 | ## mat3.normalFromMat4(out:mat3, a:mat4) 94 | 95 | Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix 96 | 97 | ## mat3.rotate(out:mat3, a:mat3, rad:Number) 98 | 99 | Rotates a mat3 by the given angle 100 | 101 | ## mat3.scale(out:mat3, a:mat3, v:vec2) 102 | 103 | Scales the mat3 by the dimensions in the given vec2 104 | 105 | ## mat3.str(mat:mat3) 106 | 107 | Returns a string representation of a mat3 108 | 109 | ## mat3.translate(out:mat3, a:mat3, v:vec2) 110 | 111 | Translate a mat3 by the given vector 112 | 113 | ## mat3.transpose(out:mat3, a:mat3) 114 | 115 | Transpose the values of a mat3 116 | 117 | ## License 118 | 119 | [zlib](http://en.wikipedia.org/wiki/Zlib_License). See [LICENSE.md](http://github.com/hughsk/gl-mat3/blob/master/LICENSE.md) for details. 120 | -------------------------------------------------------------------------------- /adjoint.js: -------------------------------------------------------------------------------- 1 | module.exports = adjoint 2 | 3 | /** 4 | * Calculates the adjugate of a mat3 5 | * 6 | * @alias mat3.adjoint 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the source matrix 9 | * @returns {mat3} out 10 | */ 11 | function adjoint(out, a) { 12 | var a00 = a[0], a01 = a[1], a02 = a[2] 13 | var a10 = a[3], a11 = a[4], a12 = a[5] 14 | var a20 = a[6], a21 = a[7], a22 = a[8] 15 | 16 | out[0] = (a11 * a22 - a12 * a21) 17 | out[1] = (a02 * a21 - a01 * a22) 18 | out[2] = (a01 * a12 - a02 * a11) 19 | out[3] = (a12 * a20 - a10 * a22) 20 | out[4] = (a00 * a22 - a02 * a20) 21 | out[5] = (a02 * a10 - a00 * a12) 22 | out[6] = (a10 * a21 - a11 * a20) 23 | out[7] = (a01 * a20 - a00 * a21) 24 | out[8] = (a00 * a11 - a01 * a10) 25 | 26 | return out 27 | } 28 | -------------------------------------------------------------------------------- /clone.js: -------------------------------------------------------------------------------- 1 | module.exports = clone 2 | 3 | /** 4 | * Creates a new mat3 initialized with values from an existing matrix 5 | * 6 | * @alias mat3.clone 7 | * @param {mat3} a matrix to clone 8 | * @returns {mat3} a new 3x3 matrix 9 | */ 10 | function clone(a) { 11 | var out = new Float32Array(9) 12 | out[0] = a[0] 13 | out[1] = a[1] 14 | out[2] = a[2] 15 | out[3] = a[3] 16 | out[4] = a[4] 17 | out[5] = a[5] 18 | out[6] = a[6] 19 | out[7] = a[7] 20 | out[8] = a[8] 21 | return out 22 | } 23 | -------------------------------------------------------------------------------- /copy.js: -------------------------------------------------------------------------------- 1 | module.exports = copy 2 | 3 | /** 4 | * Copy the values from one mat3 to another 5 | * 6 | * @alias mat3.copy 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the source matrix 9 | * @returns {mat3} out 10 | */ 11 | function copy(out, a) { 12 | out[0] = a[0] 13 | out[1] = a[1] 14 | out[2] = a[2] 15 | out[3] = a[3] 16 | out[4] = a[4] 17 | out[5] = a[5] 18 | out[6] = a[6] 19 | out[7] = a[7] 20 | out[8] = a[8] 21 | return out 22 | } 23 | -------------------------------------------------------------------------------- /create.js: -------------------------------------------------------------------------------- 1 | module.exports = create 2 | 3 | /** 4 | * Creates a new identity mat3 5 | * 6 | * @alias mat3.create 7 | * @returns {mat3} a new 3x3 matrix 8 | */ 9 | function create() { 10 | var out = new Float32Array(9) 11 | out[0] = 1 12 | out[1] = 0 13 | out[2] = 0 14 | out[3] = 0 15 | out[4] = 1 16 | out[5] = 0 17 | out[6] = 0 18 | out[7] = 0 19 | out[8] = 1 20 | return out 21 | } 22 | -------------------------------------------------------------------------------- /determinant.js: -------------------------------------------------------------------------------- 1 | module.exports = determinant 2 | 3 | /** 4 | * Calculates the determinant of a mat3 5 | * 6 | * @alias mat3.determinant 7 | * @param {mat3} a the source matrix 8 | * @returns {Number} determinant of a 9 | */ 10 | function determinant(a) { 11 | var a00 = a[0], a01 = a[1], a02 = a[2] 12 | var a10 = a[3], a11 = a[4], a12 = a[5] 13 | var a20 = a[6], a21 = a[7], a22 = a[8] 14 | 15 | return a00 * (a22 * a11 - a12 * a21) 16 | + a01 * (a12 * a20 - a22 * a10) 17 | + a02 * (a21 * a10 - a11 * a20) 18 | } 19 | -------------------------------------------------------------------------------- /frob.js: -------------------------------------------------------------------------------- 1 | module.exports = frob 2 | 3 | /** 4 | * Returns Frobenius norm of a mat3 5 | * 6 | * @alias mat3.frob 7 | * @param {mat3} a the matrix to calculate Frobenius norm of 8 | * @returns {Number} Frobenius norm 9 | */ 10 | function frob(a) { 11 | return Math.sqrt( 12 | a[0]*a[0] 13 | + a[1]*a[1] 14 | + a[2]*a[2] 15 | + a[3]*a[3] 16 | + a[4]*a[4] 17 | + a[5]*a[5] 18 | + a[6]*a[6] 19 | + a[7]*a[7] 20 | + a[8]*a[8] 21 | ) 22 | } 23 | -------------------------------------------------------------------------------- /fromMat2.js: -------------------------------------------------------------------------------- 1 | module.exports = fromMat2d 2 | 3 | /** 4 | * Copies the values from a mat2d into a mat3 5 | * 6 | * @alias mat3.fromMat2d 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat2d} a the matrix to copy 9 | * @returns {mat3} out 10 | **/ 11 | function fromMat2d(out, a) { 12 | out[0] = a[0] 13 | out[1] = a[1] 14 | out[2] = 0 15 | 16 | out[3] = a[2] 17 | out[4] = a[3] 18 | out[5] = 0 19 | 20 | out[6] = a[4] 21 | out[7] = a[5] 22 | out[8] = 1 23 | 24 | return out 25 | } 26 | -------------------------------------------------------------------------------- /fromMat4.js: -------------------------------------------------------------------------------- 1 | module.exports = fromMat4 2 | 3 | /** 4 | * Copies the upper-left 3x3 values into the given mat3. 5 | * 6 | * @alias mat3.fromMat4 7 | * @param {mat3} out the receiving 3x3 matrix 8 | * @param {mat4} a the source 4x4 matrix 9 | * @returns {mat3} out 10 | */ 11 | function fromMat4(out, a) { 12 | out[0] = a[0] 13 | out[1] = a[1] 14 | out[2] = a[2] 15 | out[3] = a[4] 16 | out[4] = a[5] 17 | out[5] = a[6] 18 | out[6] = a[8] 19 | out[7] = a[9] 20 | out[8] = a[10] 21 | return out 22 | } 23 | -------------------------------------------------------------------------------- /fromQuat.js: -------------------------------------------------------------------------------- 1 | module.exports = fromQuat 2 | 3 | /** 4 | * Calculates a 3x3 matrix from the given quaternion 5 | * 6 | * @alias mat3.fromQuat 7 | * @param {mat3} out mat3 receiving operation result 8 | * @param {quat} q Quaternion to create matrix from 9 | * 10 | * @returns {mat3} out 11 | */ 12 | function fromQuat(out, q) { 13 | var x = q[0] 14 | var y = q[1] 15 | var z = q[2] 16 | var w = q[3] 17 | 18 | var x2 = x + x 19 | var y2 = y + y 20 | var z2 = z + z 21 | 22 | var xx = x * x2 23 | var yx = y * x2 24 | var yy = y * y2 25 | var zx = z * x2 26 | var zy = z * y2 27 | var zz = z * z2 28 | var wx = w * x2 29 | var wy = w * y2 30 | var wz = w * z2 31 | 32 | out[0] = 1 - yy - zz 33 | out[3] = yx - wz 34 | out[6] = zx + wy 35 | 36 | out[1] = yx + wz 37 | out[4] = 1 - xx - zz 38 | out[7] = zy - wx 39 | 40 | out[2] = zx - wy 41 | out[5] = zy + wx 42 | out[8] = 1 - xx - yy 43 | 44 | return out 45 | } 46 | -------------------------------------------------------------------------------- /identity.js: -------------------------------------------------------------------------------- 1 | module.exports = identity 2 | 3 | /** 4 | * Set a mat3 to the identity matrix 5 | * 6 | * @alias mat3.identity 7 | * @param {mat3} out the receiving matrix 8 | * @returns {mat3} out 9 | */ 10 | function identity(out) { 11 | out[0] = 1 12 | out[1] = 0 13 | out[2] = 0 14 | out[3] = 0 15 | out[4] = 1 16 | out[5] = 0 17 | out[6] = 0 18 | out[7] = 0 19 | out[8] = 1 20 | return out 21 | } 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | adjoint: require('./adjoint') 3 | , clone: require('./clone') 4 | , copy: require('./copy') 5 | , create: require('./create') 6 | , determinant: require('./determinant') 7 | , frob: require('./frob') 8 | , fromMat2: require('./fromMat2') 9 | , fromMat4: require('./fromMat4') 10 | , fromQuat: require('./fromQuat') 11 | , identity: require('./identity') 12 | , invert: require('./invert') 13 | , multiply: require('./multiply') 14 | , normalFromMat4: require('./normalFromMat4') 15 | , rotate: require('./rotate') 16 | , scale: require('./scale') 17 | , str: require('./str') 18 | , translate: require('./translate') 19 | , transpose: require('./transpose') 20 | } 21 | -------------------------------------------------------------------------------- /invert.js: -------------------------------------------------------------------------------- 1 | module.exports = invert 2 | 3 | /** 4 | * Inverts a mat3 5 | * 6 | * @alias mat3.invert 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the source matrix 9 | * @returns {mat3} out 10 | */ 11 | function invert(out, a) { 12 | var a00 = a[0], a01 = a[1], a02 = a[2] 13 | var a10 = a[3], a11 = a[4], a12 = a[5] 14 | var a20 = a[6], a21 = a[7], a22 = a[8] 15 | 16 | var b01 = a22 * a11 - a12 * a21 17 | var b11 = -a22 * a10 + a12 * a20 18 | var b21 = a21 * a10 - a11 * a20 19 | 20 | // Calculate the determinant 21 | var det = a00 * b01 + a01 * b11 + a02 * b21 22 | 23 | if (!det) return null 24 | det = 1.0 / det 25 | 26 | out[0] = b01 * det 27 | out[1] = (-a22 * a01 + a02 * a21) * det 28 | out[2] = (a12 * a01 - a02 * a11) * det 29 | out[3] = b11 * det 30 | out[4] = (a22 * a00 - a02 * a20) * det 31 | out[5] = (-a12 * a00 + a02 * a10) * det 32 | out[6] = b21 * det 33 | out[7] = (-a21 * a00 + a01 * a20) * det 34 | out[8] = (a11 * a00 - a01 * a10) * det 35 | 36 | return out 37 | } 38 | -------------------------------------------------------------------------------- /multiply.js: -------------------------------------------------------------------------------- 1 | module.exports = multiply 2 | 3 | /** 4 | * Multiplies two mat3's 5 | * 6 | * @alias mat3.multiply 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the first operand 9 | * @param {mat3} b the second operand 10 | * @returns {mat3} out 11 | */ 12 | function multiply(out, a, b) { 13 | var a00 = a[0], a01 = a[1], a02 = a[2] 14 | var a10 = a[3], a11 = a[4], a12 = a[5] 15 | var a20 = a[6], a21 = a[7], a22 = a[8] 16 | 17 | var b00 = b[0], b01 = b[1], b02 = b[2] 18 | var b10 = b[3], b11 = b[4], b12 = b[5] 19 | var b20 = b[6], b21 = b[7], b22 = b[8] 20 | 21 | out[0] = b00 * a00 + b01 * a10 + b02 * a20 22 | out[1] = b00 * a01 + b01 * a11 + b02 * a21 23 | out[2] = b00 * a02 + b01 * a12 + b02 * a22 24 | 25 | out[3] = b10 * a00 + b11 * a10 + b12 * a20 26 | out[4] = b10 * a01 + b11 * a11 + b12 * a21 27 | out[5] = b10 * a02 + b11 * a12 + b12 * a22 28 | 29 | out[6] = b20 * a00 + b21 * a10 + b22 * a20 30 | out[7] = b20 * a01 + b21 * a11 + b22 * a21 31 | out[8] = b20 * a02 + b21 * a12 + b22 * a22 32 | 33 | return out 34 | } 35 | -------------------------------------------------------------------------------- /normalFromMat4.js: -------------------------------------------------------------------------------- 1 | module.exports = normalFromMat4 2 | 3 | /** 4 | * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix 5 | * 6 | * @alias mat3.normalFromMat4 7 | * @param {mat3} out mat3 receiving operation result 8 | * @param {mat4} a Mat4 to derive the normal matrix from 9 | * 10 | * @returns {mat3} out 11 | */ 12 | function normalFromMat4(out, a) { 13 | var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3] 14 | var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7] 15 | var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11] 16 | var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15] 17 | 18 | var b00 = a00 * a11 - a01 * a10 19 | var b01 = a00 * a12 - a02 * a10 20 | var b02 = a00 * a13 - a03 * a10 21 | var b03 = a01 * a12 - a02 * a11 22 | var b04 = a01 * a13 - a03 * a11 23 | var b05 = a02 * a13 - a03 * a12 24 | var b06 = a20 * a31 - a21 * a30 25 | var b07 = a20 * a32 - a22 * a30 26 | var b08 = a20 * a33 - a23 * a30 27 | var b09 = a21 * a32 - a22 * a31 28 | var b10 = a21 * a33 - a23 * a31 29 | var b11 = a22 * a33 - a23 * a32 30 | 31 | // Calculate the determinant 32 | var det = b00 * b11 33 | - b01 * b10 34 | + b02 * b09 35 | + b03 * b08 36 | - b04 * b07 37 | + b05 * b06 38 | 39 | if (!det) return null 40 | det = 1.0 / det 41 | 42 | out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det 43 | out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det 44 | out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det 45 | 46 | out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det 47 | out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det 48 | out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det 49 | 50 | out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det 51 | out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det 52 | out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det 53 | 54 | return out 55 | } 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gl-mat3", 3 | "version": "2.0.0", 4 | "description": "gl-matrix's mat3, split into smaller pieces", 5 | "main": "index.js", 6 | "license": { 7 | "type": "zlib", 8 | "url": "http://github.com/gl-modules/gl-mat3/blob/master/LICENSE.md" 9 | }, 10 | "scripts": { 11 | "get-docs": "cat *.js | dox --api" 12 | }, 13 | "contributors": [ 14 | "Brandon Jones ", 15 | "Colin MacKenzie IV ", 16 | "Hugh Kennedy (http://hughsk.io/)" 17 | ], 18 | "dependencies": {}, 19 | "devDependencies": { 20 | "dox": "git://github.com/hughsk/dox#api-context" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "git://github.com/gl-modules/gl-mat3.git" 25 | }, 26 | "keywords": [ 27 | "gl-matrix", 28 | "matrix", 29 | "maths" 30 | ], 31 | "homepage": "https://github.com/gl-modules/gl-mat3", 32 | "bugs": { 33 | "url": "https://github.com/gl-modules/gl-mat3/issues" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /rotate.js: -------------------------------------------------------------------------------- 1 | module.exports = rotate 2 | 3 | /** 4 | * Rotates a mat3 by the given angle 5 | * 6 | * @alias mat3.rotate 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the matrix to rotate 9 | * @param {Number} rad the angle to rotate the matrix by 10 | * @returns {mat3} out 11 | */ 12 | function rotate(out, a, rad) { 13 | var a00 = a[0], a01 = a[1], a02 = a[2] 14 | var a10 = a[3], a11 = a[4], a12 = a[5] 15 | var a20 = a[6], a21 = a[7], a22 = a[8] 16 | 17 | var s = Math.sin(rad) 18 | var c = Math.cos(rad) 19 | 20 | out[0] = c * a00 + s * a10 21 | out[1] = c * a01 + s * a11 22 | out[2] = c * a02 + s * a12 23 | 24 | out[3] = c * a10 - s * a00 25 | out[4] = c * a11 - s * a01 26 | out[5] = c * a12 - s * a02 27 | 28 | out[6] = a20 29 | out[7] = a21 30 | out[8] = a22 31 | 32 | return out 33 | } 34 | -------------------------------------------------------------------------------- /scale.js: -------------------------------------------------------------------------------- 1 | module.exports = scale 2 | 3 | /** 4 | * Scales the mat3 by the dimensions in the given vec2 5 | * 6 | * @alias mat3.scale 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the matrix to rotate 9 | * @param {vec2} v the vec2 to scale the matrix by 10 | * @returns {mat3} out 11 | **/ 12 | function scale(out, a, v) { 13 | var x = v[0] 14 | var y = v[1] 15 | 16 | out[0] = x * a[0] 17 | out[1] = x * a[1] 18 | out[2] = x * a[2] 19 | 20 | out[3] = y * a[3] 21 | out[4] = y * a[4] 22 | out[5] = y * a[5] 23 | 24 | out[6] = a[6] 25 | out[7] = a[7] 26 | out[8] = a[8] 27 | 28 | return out 29 | } 30 | -------------------------------------------------------------------------------- /str.js: -------------------------------------------------------------------------------- 1 | module.exports = str 2 | 3 | /** 4 | * Returns a string representation of a mat3 5 | * 6 | * @alias mat3.str 7 | * @param {mat3} mat matrix to represent as a string 8 | * @returns {String} string representation of the matrix 9 | */ 10 | function str(a) { 11 | return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + 12 | a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + 13 | a[6] + ', ' + a[7] + ', ' + a[8] + ')' 14 | } 15 | -------------------------------------------------------------------------------- /translate.js: -------------------------------------------------------------------------------- 1 | module.exports = translate 2 | 3 | /** 4 | * Translate a mat3 by the given vector 5 | * 6 | * @alias mat3.translate 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the matrix to translate 9 | * @param {vec2} v vector to translate by 10 | * @returns {mat3} out 11 | */ 12 | function translate(out, a, v) { 13 | var a00 = a[0], a01 = a[1], a02 = a[2] 14 | var a10 = a[3], a11 = a[4], a12 = a[5] 15 | var a20 = a[6], a21 = a[7], a22 = a[8] 16 | var x = v[0], y = v[1] 17 | 18 | out[0] = a00 19 | out[1] = a01 20 | out[2] = a02 21 | 22 | out[3] = a10 23 | out[4] = a11 24 | out[5] = a12 25 | 26 | out[6] = x * a00 + y * a10 + a20 27 | out[7] = x * a01 + y * a11 + a21 28 | out[8] = x * a02 + y * a12 + a22 29 | 30 | return out 31 | } 32 | -------------------------------------------------------------------------------- /transpose.js: -------------------------------------------------------------------------------- 1 | module.exports = transpose 2 | 3 | /** 4 | * Transpose the values of a mat3 5 | * 6 | * @alias mat3.transpose 7 | * @param {mat3} out the receiving matrix 8 | * @param {mat3} a the source matrix 9 | * @returns {mat3} out 10 | */ 11 | function transpose(out, a) { 12 | // If we are transposing ourselves we can skip a few steps but have to cache some values 13 | if (out === a) { 14 | var a01 = a[1], a02 = a[2], a12 = a[5] 15 | out[1] = a[3] 16 | out[2] = a[6] 17 | out[3] = a01 18 | out[5] = a[7] 19 | out[6] = a02 20 | out[7] = a12 21 | } else { 22 | out[0] = a[0] 23 | out[1] = a[3] 24 | out[2] = a[6] 25 | out[3] = a[1] 26 | out[4] = a[4] 27 | out[5] = a[7] 28 | out[6] = a[2] 29 | out[7] = a[5] 30 | out[8] = a[8] 31 | } 32 | 33 | return out 34 | } 35 | --------------------------------------------------------------------------------