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. */
30 * Creates a new identity mat2
32 * @returns {mat2} a new 2x2 matrix
34 mat2.create = function() {
35 var out = new GLMAT_ARRAY_TYPE(4);
44 * Creates a new mat2 initialized with values from an existing matrix
46 * @param {mat2} a matrix to clone
47 * @returns {mat2} a new 2x2 matrix
49 mat2.clone = function(a) {
50 var out = new GLMAT_ARRAY_TYPE(4);
59 * Copy the values from one mat2 to another
61 * @param {mat2} out the receiving matrix
62 * @param {mat2} a the source matrix
65 mat2.copy = function(out, a) {
74 * Set a mat2 to the identity matrix
76 * @param {mat2} out the receiving matrix
79 mat2.identity = function(out) {
88 * Transpose the values of a mat2
90 * @param {mat2} out the receiving matrix
91 * @param {mat2} a the source matrix
94 mat2.transpose = function(out, a) {
95 // If we are transposing ourselves we can skip a few steps but have to cache some values
113 * @param {mat2} out the receiving matrix
114 * @param {mat2} a the source matrix
115 * @returns {mat2} out
117 mat2.invert = function(out, a) {
118 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
120 // Calculate the determinant
121 det = a0 * a3 - a2 * a1;
137 * Calculates the adjugate of a mat2
139 * @param {mat2} out the receiving matrix
140 * @param {mat2} a the source matrix
141 * @returns {mat2} out
143 mat2.adjoint = function(out, a) {
144 // Caching this value is nessecary if out == a
155 * Calculates the determinant of a mat2
157 * @param {mat2} a the source matrix
158 * @returns {Number} determinant of a
160 mat2.determinant = function (a) {
161 return a[0] * a[3] - a[2] * a[1];
165 * Multiplies two mat2's
167 * @param {mat2} out the receiving matrix
168 * @param {mat2} a the first operand
169 * @param {mat2} b the second operand
170 * @returns {mat2} out
172 mat2.multiply = function (out, a, b) {
173 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];
174 var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
175 out[0] = a0 * b0 + a1 * b2;
176 out[1] = a0 * b1 + a1 * b3;
177 out[2] = a2 * b0 + a3 * b2;
178 out[3] = a2 * b1 + a3 * b3;
183 * Alias for {@link mat2.multiply}
186 mat2.mul = mat2.multiply;
189 * Rotates a mat2 by the given angle
191 * @param {mat2} out the receiving matrix
192 * @param {mat2} a the matrix to rotate
193 * @param {Number} rad the angle to rotate the matrix by
194 * @returns {mat2} out
196 mat2.rotate = function (out, a, rad) {
197 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
200 out[0] = a0 * c + a1 * s;
201 out[1] = a0 * -s + a1 * c;
202 out[2] = a2 * c + a3 * s;
203 out[3] = a2 * -s + a3 * c;
208 * Scales the mat2 by the dimensions in the given vec2
210 * @param {mat2} out the receiving matrix
211 * @param {mat2} a the matrix to rotate
212 * @param {vec2} v the vec2 to scale the matrix by
213 * @returns {mat2} out
215 mat2.scale = function(out, a, v) {
216 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
217 v0 = v[0], v1 = v[1];
226 * Returns a string representation of a mat2
228 * @param {mat2} mat matrix to represent as a string
229 * @returns {String} string representation of the matrix
231 mat2.str = function (a) {
232 return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
235 if(typeof(exports) !== 'undefined') {