| /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without modification, |
| are permitted provided that the following conditions are met: |
| |
| * Redistributions of source code must retain the above copyright notice, this |
| list of conditions and the following disclaimer. |
| * Redistributions in binary form must reproduce the above copyright notice, |
| this list of conditions and the following disclaimer in the documentation |
| and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
| |
| /** |
| * @class 2x3 Matrix |
| * @name mat2d |
| * |
| * @description |
| * A mat2d contains six elements defined as: |
| * <pre> |
| * [a, b, |
| * c, d, |
| * tx,ty] |
| * </pre> |
| * This is a short form for the 3x3 matrix: |
| * <pre> |
| * [a, b, 0 |
| * c, d, 0 |
| * tx,ty,1] |
| * </pre> |
| * The last column is ignored so the array is shorter and operations are faster. |
| */ |
| var mat2d = {}; |
| |
| /** |
| * Creates a new identity mat2d |
| * |
| * @returns {mat2d} a new 2x3 matrix |
| */ |
| mat2d.create = function() { |
| var out = new GLMAT_ARRAY_TYPE(6); |
| out[0] = 1; |
| out[1] = 0; |
| out[2] = 0; |
| out[3] = 1; |
| out[4] = 0; |
| out[5] = 0; |
| return out; |
| }; |
| |
| /** |
| * Creates a new mat2d initialized with values from an existing matrix |
| * |
| * @param {mat2d} a matrix to clone |
| * @returns {mat2d} a new 2x3 matrix |
| */ |
| mat2d.clone = function(a) { |
| var out = new GLMAT_ARRAY_TYPE(6); |
| out[0] = a[0]; |
| out[1] = a[1]; |
| out[2] = a[2]; |
| out[3] = a[3]; |
| out[4] = a[4]; |
| out[5] = a[5]; |
| return out; |
| }; |
| |
| /** |
| * Copy the values from one mat2d to another |
| * |
| * @param {mat2d} out the receiving matrix |
| * @param {mat2d} a the source matrix |
| * @returns {mat2d} out |
| */ |
| mat2d.copy = function(out, a) { |
| out[0] = a[0]; |
| out[1] = a[1]; |
| out[2] = a[2]; |
| out[3] = a[3]; |
| out[4] = a[4]; |
| out[5] = a[5]; |
| return out; |
| }; |
| |
| /** |
| * Set a mat2d to the identity matrix |
| * |
| * @param {mat2d} out the receiving matrix |
| * @returns {mat2d} out |
| */ |
| mat2d.identity = function(out) { |
| out[0] = 1; |
| out[1] = 0; |
| out[2] = 0; |
| out[3] = 1; |
| out[4] = 0; |
| out[5] = 0; |
| return out; |
| }; |
| |
| /** |
| * Inverts a mat2d |
| * |
| * @param {mat2d} out the receiving matrix |
| * @param {mat2d} a the source matrix |
| * @returns {mat2d} out |
| */ |
| mat2d.invert = function(out, a) { |
| var aa = a[0], ab = a[1], ac = a[2], ad = a[3], |
| atx = a[4], aty = a[5]; |
| |
| var det = aa * ad - ab * ac; |
| if(!det){ |
| return null; |
| } |
| det = 1.0 / det; |
| |
| out[0] = ad * det; |
| out[1] = -ab * det; |
| out[2] = -ac * det; |
| out[3] = aa * det; |
| out[4] = (ac * aty - ad * atx) * det; |
| out[5] = (ab * atx - aa * aty) * det; |
| return out; |
| }; |
| |
| /** |
| * Calculates the determinant of a mat2d |
| * |
| * @param {mat2d} a the source matrix |
| * @returns {Number} determinant of a |
| */ |
| mat2d.determinant = function (a) { |
| return a[0] * a[3] - a[1] * a[2]; |
| }; |
| |
| /** |
| * Multiplies two mat2d's |
| * |
| * @param {mat2d} out the receiving matrix |
| * @param {mat2d} a the first operand |
| * @param {mat2d} b the second operand |
| * @returns {mat2d} out |
| */ |
| mat2d.multiply = function (out, a, b) { |
| var aa = a[0], ab = a[1], ac = a[2], ad = a[3], |
| atx = a[4], aty = a[5], |
| ba = b[0], bb = b[1], bc = b[2], bd = b[3], |
| btx = b[4], bty = b[5]; |
| |
| out[0] = aa*ba + ab*bc; |
| out[1] = aa*bb + ab*bd; |
| out[2] = ac*ba + ad*bc; |
| out[3] = ac*bb + ad*bd; |
| out[4] = ba*atx + bc*aty + btx; |
| out[5] = bb*atx + bd*aty + bty; |
| return out; |
| }; |
| |
| /** |
| * Alias for {@link mat2d.multiply} |
| * @function |
| */ |
| mat2d.mul = mat2d.multiply; |
| |
| |
| /** |
| * Rotates a mat2d by the given angle |
| * |
| * @param {mat2d} out the receiving matrix |
| * @param {mat2d} a the matrix to rotate |
| * @param {Number} rad the angle to rotate the matrix by |
| * @returns {mat2d} out |
| */ |
| mat2d.rotate = function (out, a, rad) { |
| var aa = a[0], |
| ab = a[1], |
| ac = a[2], |
| ad = a[3], |
| atx = a[4], |
| aty = a[5], |
| st = Math.sin(rad), |
| ct = Math.cos(rad); |
| |
| out[0] = aa*ct + ab*st; |
| out[1] = -aa*st + ab*ct; |
| out[2] = ac*ct + ad*st; |
| out[3] = -ac*st + ct*ad; |
| out[4] = ct*atx + st*aty; |
| out[5] = ct*aty - st*atx; |
| return out; |
| }; |
| |
| /** |
| * Scales the mat2d by the dimensions in the given vec2 |
| * |
| * @param {mat2d} out the receiving matrix |
| * @param {mat2d} a the matrix to translate |
| * @param {mat2d} v the vec2 to scale the matrix by |
| * @returns {mat2d} out |
| **/ |
| mat2d.scale = function(out, a, v) { |
| var vx = v[0], vy = v[1]; |
| out[0] = a[0] * vx; |
| out[1] = a[1] * vy; |
| out[2] = a[2] * vx; |
| out[3] = a[3] * vy; |
| out[4] = a[4] * vx; |
| out[5] = a[5] * vy; |
| return out; |
| }; |
| |
| /** |
| * Translates the mat2d by the dimensions in the given vec2 |
| * |
| * @param {mat2d} out the receiving matrix |
| * @param {mat2d} a the matrix to translate |
| * @param {mat2d} v the vec2 to translate the matrix by |
| * @returns {mat2d} out |
| **/ |
| mat2d.translate = function(out, a, v) { |
| out[0] = a[0]; |
| out[1] = a[1]; |
| out[2] = a[2]; |
| out[3] = a[3]; |
| out[4] = a[4] + v[0]; |
| out[5] = a[5] + v[1]; |
| return out; |
| }; |
| |
| /** |
| * Returns a string representation of a mat2d |
| * |
| * @param {mat2d} a matrix to represent as a string |
| * @returns {String} string representation of the matrix |
| */ |
| mat2d.str = function (a) { |
| return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + |
| a[3] + ', ' + a[4] + ', ' + a[5] + ')'; |
| }; |
| |
| if(typeof(exports) !== 'undefined') { |
| exports.mat2d = mat2d; |
| } |