1 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
3 Redistribution and use in source and binary forms, with or without modification,
4 are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright notice, this
7 list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice,
9 this list of conditions and the following disclaimer in the documentation
10 and/or other materials provided with the distribution.
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28 * A mat2d contains six elements defined as:
34 * This is a short form for the 3x3 matrix:
40 * The last column is ignored so the array is shorter and operations are faster.
45 * Creates a new identity mat2d
47 * @returns {mat2d} a new 2x3 matrix
49 mat2d.create = function() {
50 var out = new GLMAT_ARRAY_TYPE(6);
61 * Creates a new mat2d initialized with values from an existing matrix
63 * @param {mat2d} a matrix to clone
64 * @returns {mat2d} a new 2x3 matrix
66 mat2d.clone = function(a) {
67 var out = new GLMAT_ARRAY_TYPE(6);
78 * Copy the values from one mat2d to another
80 * @param {mat2d} out the receiving matrix
81 * @param {mat2d} a the source matrix
82 * @returns {mat2d} out
84 mat2d.copy = function(out, a) {
95 * Set a mat2d to the identity matrix
97 * @param {mat2d} out the receiving matrix
98 * @returns {mat2d} out
100 mat2d.identity = function(out) {
113 * @param {mat2d} out the receiving matrix
114 * @param {mat2d} a the source matrix
115 * @returns {mat2d} out
117 mat2d.invert = function(out, a) {
118 var aa = a[0], ab = a[1], ac = a[2], ad = a[3],
119 atx = a[4], aty = a[5];
121 var det = aa * ad - ab * ac;
131 out[4] = (ac * aty - ad * atx) * det;
132 out[5] = (ab * atx - aa * aty) * det;
137 * Calculates the determinant of a mat2d
139 * @param {mat2d} a the source matrix
140 * @returns {Number} determinant of a
142 mat2d.determinant = function (a) {
143 return a[0] * a[3] - a[1] * a[2];
147 * Multiplies two mat2d's
149 * @param {mat2d} out the receiving matrix
150 * @param {mat2d} a the first operand
151 * @param {mat2d} b the second operand
152 * @returns {mat2d} out
154 mat2d.multiply = function (out, a, b) {
155 var aa = a[0], ab = a[1], ac = a[2], ad = a[3],
156 atx = a[4], aty = a[5],
157 ba = b[0], bb = b[1], bc = b[2], bd = b[3],
158 btx = b[4], bty = b[5];
160 out[0] = aa*ba + ab*bc;
161 out[1] = aa*bb + ab*bd;
162 out[2] = ac*ba + ad*bc;
163 out[3] = ac*bb + ad*bd;
164 out[4] = ba*atx + bc*aty + btx;
165 out[5] = bb*atx + bd*aty + bty;
170 * Alias for {@link mat2d.multiply}
173 mat2d.mul = mat2d.multiply;
177 * Rotates a mat2d by the given angle
179 * @param {mat2d} out the receiving matrix
180 * @param {mat2d} a the matrix to rotate
181 * @param {Number} rad the angle to rotate the matrix by
182 * @returns {mat2d} out
184 mat2d.rotate = function (out, a, rad) {
194 out[0] = aa*ct + ab*st;
195 out[1] = -aa*st + ab*ct;
196 out[2] = ac*ct + ad*st;
197 out[3] = -ac*st + ct*ad;
198 out[4] = ct*atx + st*aty;
199 out[5] = ct*aty - st*atx;
204 * Scales the mat2d by the dimensions in the given vec2
206 * @param {mat2d} out the receiving matrix
207 * @param {mat2d} a the matrix to translate
208 * @param {mat2d} v the vec2 to scale the matrix by
209 * @returns {mat2d} out
211 mat2d.scale = function(out, a, v) {
212 var vx = v[0], vy = v[1];
223 * Translates the mat2d by the dimensions in the given vec2
225 * @param {mat2d} out the receiving matrix
226 * @param {mat2d} a the matrix to translate
227 * @param {mat2d} v the vec2 to translate the matrix by
228 * @returns {mat2d} out
230 mat2d.translate = function(out, a, v) {
235 out[4] = a[4] + v[0];
236 out[5] = a[5] + v[1];
241 * Returns a string representation of a mat2d
243 * @param {mat2d} a matrix to represent as a string
244 * @returns {String} string representation of the matrix
246 mat2d.str = function (a) {
247 return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
248 a[3] + ', ' + a[4] + ', ' + a[5] + ')';
251 if(typeof(exports) !== 'undefined') {
252 exports.mat2d = mat2d;