aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/math/vec2.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/math/vec2.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/math/vec2.js')
-rw-r--r--js/helper-classes/RDGE/src/core/script/math/vec2.js191
1 files changed, 191 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/math/vec2.js b/js/helper-classes/RDGE/src/core/script/math/vec2.js
new file mode 100644
index 00000000..5f1b07e3
--- /dev/null
+++ b/js/helper-classes/RDGE/src/core/script/math/vec2.js
@@ -0,0 +1,191 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7
8/**
9* vec2 = {}
10* This library contains functions for operating on 2D vectors.
11* A 2D vector can be any array containing at least 2 numeric components.
12* All of the following are valid methods for declaring a vec2:
13* var a = [0, 1];
14* var b = vec2.zero();
15* var c = vec2.up();
16*/
17vec2 = {};
18
19/**
20* vec2.string
21*/
22vec2.string = function(v) {
23 return "{ " + v[0] + ", " + v[1] + " }";
24}
25
26/**
27* vec2.verify
28* This function is provided for debugging purposes only. It is not recommended
29* to be used in performance critical areas of the code.
30*/
31vec2.verify = function(v) {
32 if (v == undefined || v.length == undefined || v.length < 2) {
33 return false;
34 }
35
36 if (typeof (v[0]) != "number" || typeof (v[1]) != "number") {
37 return false;
38 }
39
40 return true;
41}
42
43/**
44* vec2.copy
45*/
46vec2.copy = function(v) {
47 if( v.length == undefined ) {
48 return [ v, v ];
49 }
50
51 return [v[0], v[1]];
52}
53
54/**
55* vec2.inplace_copy
56*/
57vec2.inplace_copy = function(dst, src) {
58 dst[0] = src[0];
59 dst[1] = src[1];
60}
61
62/**
63* vec2.zero
64*/
65vec2.zero = function() {
66 return [0.0, 0.0];
67}
68
69/**
70* vec2.up
71*/
72vec2.up = function() {
73 return [0.0, 1.0];
74}
75
76/**
77* vec2.right
78*/
79vec2.right = function() {
80 return [1.0, 0.0];
81}
82
83/**
84* vec2.add
85*/
86vec2.add = function(a, b) {
87 return [a[0] + b[0], a[1] + b[1]];
88}
89/**
90* vec2.sub
91*/
92vec2.sub = function(a, b) {
93 return [a[0] - b[0], a[1] - b[1]];
94}
95
96/**
97* vec2.mul
98*/
99vec2.mul = function(a, b) {
100 return [ a[0] * b[0], a[1] * b[1] ];
101}
102
103/**
104* vec2.addMul
105*/
106vec2.addMul = function(a, b, s) {
107 if (s.length != undefined && s.length >= 2) {
108 return [a[0] + b[0] * s[0], a[1] + b[1] * s[1]];
109 } else {
110 return [a[0] + b[0] * s, a[1] + b[1] * s];
111 }
112}
113
114/**
115* vec2.scale
116*/
117vec2.scale = function(v, s) {
118 if (s.length != undefined && s.length >= 2) {
119 return [v[0] * s[0], v[1] * s[1]];
120 } else {
121 return [v[0] * s, v[1] * s];
122 }
123}
124
125/**
126* vec2.negate
127*/
128vec2.negate = function(v) {
129 return [ -v[0], -v[1] ];
130}
131
132/**
133* vec2.normalize
134*/
135vec2.normalize = function(v) {
136 var l = Math.sqrt(v[0] * v[0], v[1] * v[1] );
137 if ( Math.abs(1.0 - l) > 0.0001 ) {
138 var ool = 1.0 / l;
139 return [ v[0] * ool, v[1] * ool ];
140 }
141 return v;
142}
143
144/**
145* vec2.dot
146*/
147vec2.dot = function(a, b) {
148 return a[0] * b[0] + a[1] * b[1];
149}
150
151/**
152* vec2.perp
153*/
154vec2.perp = function(a) {
155 return [a[1], -a[0]];
156}
157
158/**
159* vec2.lengthSq
160*/
161vec2.lengthSq = function( v ) {
162 return v[0] * v[0] + v[1] * v[1];
163}
164
165/**
166* vec2.length
167*/
168vec2.length = function( v ) {
169 return Math.sqrt( v[0] * v[0] + v[1] * v[1] );
170}
171
172/**
173* vec2.min
174*/
175vec2.min = function(a, b) {
176 return [Math.min(a[0], b[0]), Math.min(a[1], b[1])];
177}
178
179/**
180* vec2.max
181*/
182vec2.max = function(a, b) {
183 return [Math.max(a[0], b[0]), Math.max(a[1], b[1])];
184}
185
186/**
187* vec2.clamp
188*/
189vec2.clamp = function(v, vmin, vmax) {
190 return vec2.min(vmax, vec2.max(v, vmin));
191} \ No newline at end of file