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. */
24 * @class 2 Dimensional Vector
30 * Creates a new, empty vec2
32 * @returns {vec2} a new 2D vector
34 vec2.create = function() {
35 var out = new GLMAT_ARRAY_TYPE(2);
42 * Creates a new vec2 initialized with values from an existing vector
44 * @param {vec2} a vector to clone
45 * @returns {vec2} a new 2D vector
47 vec2.clone = function(a) {
48 var out = new GLMAT_ARRAY_TYPE(2);
55 * Creates a new vec2 initialized with the given values
57 * @param {Number} x X component
58 * @param {Number} y Y component
59 * @returns {vec2} a new 2D vector
61 vec2.fromValues = function(x, y) {
62 var out = new GLMAT_ARRAY_TYPE(2);
69 * Copy the values from one vec2 to another
71 * @param {vec2} out the receiving vector
72 * @param {vec2} a the source vector
75 vec2.copy = function(out, a) {
82 * Set the components of a vec2 to the given values
84 * @param {vec2} out the receiving vector
85 * @param {Number} x X component
86 * @param {Number} y Y component
89 vec2.set = function(out, x, y) {
98 * @param {vec2} out the receiving vector
99 * @param {vec2} a the first operand
100 * @param {vec2} b the second operand
101 * @returns {vec2} out
103 vec2.add = function(out, a, b) {
104 out[0] = a[0] + b[0];
105 out[1] = a[1] + b[1];
110 * Subtracts two vec2's
112 * @param {vec2} out the receiving vector
113 * @param {vec2} a the first operand
114 * @param {vec2} b the second operand
115 * @returns {vec2} out
117 vec2.subtract = function(out, a, b) {
118 out[0] = a[0] - b[0];
119 out[1] = a[1] - b[1];
124 * Alias for {@link vec2.subtract}
127 vec2.sub = vec2.subtract;
130 * Multiplies two vec2's
132 * @param {vec2} out the receiving vector
133 * @param {vec2} a the first operand
134 * @param {vec2} b the second operand
135 * @returns {vec2} out
137 vec2.multiply = function(out, a, b) {
138 out[0] = a[0] * b[0];
139 out[1] = a[1] * b[1];
144 * Alias for {@link vec2.multiply}
147 vec2.mul = vec2.multiply;
152 * @param {vec2} out the receiving vector
153 * @param {vec2} a the first operand
154 * @param {vec2} b the second operand
155 * @returns {vec2} out
157 vec2.divide = function(out, a, b) {
158 out[0] = a[0] / b[0];
159 out[1] = a[1] / b[1];
164 * Alias for {@link vec2.divide}
167 vec2.div = vec2.divide;
170 * Returns the minimum of two vec2's
172 * @param {vec2} out the receiving vector
173 * @param {vec2} a the first operand
174 * @param {vec2} b the second operand
175 * @returns {vec2} out
177 vec2.min = function(out, a, b) {
178 out[0] = Math.min(a[0], b[0]);
179 out[1] = Math.min(a[1], b[1]);
184 * Returns the maximum of two vec2's
186 * @param {vec2} out the receiving vector
187 * @param {vec2} a the first operand
188 * @param {vec2} b the second operand
189 * @returns {vec2} out
191 vec2.max = function(out, a, b) {
192 out[0] = Math.max(a[0], b[0]);
193 out[1] = Math.max(a[1], b[1]);
198 * Scales a vec2 by a scalar number
200 * @param {vec2} out the receiving vector
201 * @param {vec2} a the vector to scale
202 * @param {Number} b amount to scale the vector by
203 * @returns {vec2} out
205 vec2.scale = function(out, a, b) {
212 * Calculates the euclidian distance between two vec2's
214 * @param {vec2} a the first operand
215 * @param {vec2} b the second operand
216 * @returns {Number} distance between a and b
218 vec2.distance = function(a, b) {
221 return Math.sqrt(x*x + y*y);
225 * Alias for {@link vec2.distance}
228 vec2.dist = vec2.distance;
231 * Calculates the squared euclidian distance between two vec2's
233 * @param {vec2} a the first operand
234 * @param {vec2} b the second operand
235 * @returns {Number} squared distance between a and b
237 vec2.squaredDistance = function(a, b) {
244 * Alias for {@link vec2.squaredDistance}
247 vec2.sqrDist = vec2.squaredDistance;
250 * Calculates the length of a vec2
252 * @param {vec2} a vector to calculate length of
253 * @returns {Number} length of a
255 vec2.length = function (a) {
258 return Math.sqrt(x*x + y*y);
262 * Alias for {@link vec2.length}
265 vec2.len = vec2.length;
268 * Calculates the squared length of a vec2
270 * @param {vec2} a vector to calculate squared length of
271 * @returns {Number} squared length of a
273 vec2.squaredLength = function (a) {
280 * Alias for {@link vec2.squaredLength}
283 vec2.sqrLen = vec2.squaredLength;
286 * Negates the components of a vec2
288 * @param {vec2} out the receiving vector
289 * @param {vec2} a vector to negate
290 * @returns {vec2} out
292 vec2.negate = function(out, a) {
301 * @param {vec2} out the receiving vector
302 * @param {vec2} a vector to normalize
303 * @returns {vec2} out
305 vec2.normalize = function(out, a) {
310 //TODO: evaluate use of glm_invsqrt here?
311 len = 1 / Math.sqrt(len);
319 * Calculates the dot product of two vec2's
321 * @param {vec2} a the first operand
322 * @param {vec2} b the second operand
323 * @returns {Number} dot product of a and b
325 vec2.dot = function (a, b) {
326 return a[0] * b[0] + a[1] * b[1];
330 * Computes the cross product of two vec2's
331 * Note that the cross product must by definition produce a 3D vector
333 * @param {vec3} out the receiving vector
334 * @param {vec2} a the first operand
335 * @param {vec2} b the second operand
336 * @returns {vec3} out
338 vec2.cross = function(out, a, b) {
339 var z = a[0] * b[1] - a[1] * b[0];
346 * Performs a linear interpolation between two vec2's
348 * @param {vec2} out the receiving vector
349 * @param {vec2} a the first operand
350 * @param {vec2} b the second operand
351 * @param {Number} t interpolation amount between the two inputs
352 * @returns {vec2} out
354 vec2.lerp = function (out, a, b, t) {
357 out[0] = ax + t * (b[0] - ax);
358 out[1] = ay + t * (b[1] - ay);
363 * Transforms the vec2 with a mat2
365 * @param {vec2} out the receiving vector
366 * @param {vec2} a the vector to transform
367 * @param {mat2} m matrix to transform with
368 * @returns {vec2} out
370 vec2.transformMat2 = function(out, a, m) {
373 out[0] = m[0] * x + m[2] * y;
374 out[1] = m[1] * x + m[3] * y;
379 * Transforms the vec2 with a mat2d
381 * @param {vec2} out the receiving vector
382 * @param {vec2} a the vector to transform
383 * @param {mat2d} m matrix to transform with
384 * @returns {vec2} out
386 vec2.transformMat2d = function(out, a, m) {
389 out[0] = m[0] * x + m[2] * y + m[4];
390 out[1] = m[1] * x + m[3] * y + m[5];
395 * Transforms the vec2 with a mat3
396 * 3rd vector component is implicitly '1'
398 * @param {vec2} out the receiving vector
399 * @param {vec2} a the vector to transform
400 * @param {mat3} m matrix to transform with
401 * @returns {vec2} out
403 vec2.transformMat3 = function(out, a, m) {
406 out[0] = m[0] * x + m[3] * y + m[6];
407 out[1] = m[1] * x + m[4] * y + m[7];
412 * Transforms the vec2 with a mat4
413 * 3rd vector component is implicitly '0'
414 * 4th vector component is implicitly '1'
416 * @param {vec2} out the receiving vector
417 * @param {vec2} a the vector to transform
418 * @param {mat4} m matrix to transform with
419 * @returns {vec2} out
421 vec2.transformMat4 = function(out, a, m) {
424 out[0] = m[0] * x + m[4] * y + m[12];
425 out[1] = m[1] * x + m[5] * y + m[13];
430 * Perform some operation over an array of vec2s.
432 * @param {Array} a the array of vectors to iterate over
433 * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
434 * @param {Number} offset Number of elements to skip at the beginning of the array
435 * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
436 * @param {Function} fn Function to call for each vector in the array
437 * @param {Object} [arg] additional argument to pass to fn
441 vec2.forEach = (function() {
442 var vec = vec2.create();
444 return function(a, stride, offset, count, fn, arg) {
455 l = Math.min((count * stride) + offset, a.length);
460 for(i = offset; i < l; i += stride) {
461 vec[0] = a[i]; vec[1] = a[i+1];
463 a[i] = vec[0]; a[i+1] = vec[1];
471 * Returns a string representation of a vector
473 * @param {vec2} vec vector to represent as a string
474 * @returns {String} string representation of the vector
476 vec2.str = function (a) {
477 return 'vec2(' + a[0] + ', ' + a[1] + ')';
480 if(typeof(exports) !== 'undefined') {