diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/math/vec2.js')
-rwxr-xr-x | js/helper-classes/RDGE/src/core/script/math/vec2.js | 229 |
1 files changed, 115 insertions, 114 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 index 5f1b07e3..9cdfb89f 100755 --- a/js/helper-classes/RDGE/src/core/script/math/vec2.js +++ b/js/helper-classes/RDGE/src/core/script/math/vec2.js | |||
@@ -6,186 +6,187 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
6 | 6 | ||
7 | 7 | ||
8 | /** | 8 | /** |
9 | * vec2 = {} | 9 | * RDGE.vec2 = {} |
10 | * This library contains functions for operating on 2D vectors. | 10 | * This library contains functions for operating on 2D vectors. |
11 | * A 2D vector can be any array containing at least 2 numeric components. | 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: | 12 | * All of the following are valid methods for declaring a RDGE.vec2: |
13 | * var a = [0, 1]; | 13 | * var a = [0, 1]; |
14 | * var b = vec2.zero(); | 14 | * var b = RDGE.vec2.zero(); |
15 | * var c = vec2.up(); | 15 | * var c = RDGE.vec2.up(); |
16 | */ | 16 | */ |
17 | vec2 = {}; | 17 | var RDGE = RDGE || {}; |
18 | RDGE.vec2 = {}; | ||
18 | 19 | ||
19 | /** | 20 | /** |
20 | * vec2.string | 21 | * RDGE.vec2.string |
21 | */ | 22 | */ |
22 | vec2.string = function(v) { | 23 | RDGE.vec2.string = function (v) { |
23 | return "{ " + v[0] + ", " + v[1] + " }"; | 24 | return "{ " + v[0] + ", " + v[1] + " }"; |
24 | } | 25 | }; |
25 | 26 | ||
26 | /** | 27 | /** |
27 | * vec2.verify | 28 | * RDGE.vec2.verify |
28 | * This function is provided for debugging purposes only. It is not recommended | 29 | * This function is provided for debugging purposes only. It is not recommended |
29 | * to be used in performance critical areas of the code. | 30 | * to be used in performance critical areas of the code. |
30 | */ | 31 | */ |
31 | vec2.verify = function(v) { | 32 | RDGE.vec2.verify = function (v) { |
32 | if (v == undefined || v.length == undefined || v.length < 2) { | 33 | if (v == undefined || v.length == undefined || v.length < 2) { |
33 | return false; | 34 | return false; |
34 | } | 35 | } |
35 | 36 | ||
36 | if (typeof (v[0]) != "number" || typeof (v[1]) != "number") { | 37 | if (typeof (v[0]) != "number" || typeof (v[1]) != "number") { |
37 | return false; | 38 | return false; |
38 | } | 39 | } |
39 | 40 | ||
40 | return true; | 41 | return true; |
41 | } | 42 | }; |
42 | 43 | ||
43 | /** | 44 | /** |
44 | * vec2.copy | 45 | * RDGE.vec2.copy |
45 | */ | 46 | */ |
46 | vec2.copy = function(v) { | 47 | RDGE.vec2.copy = function (v) { |
47 | if( v.length == undefined ) { | 48 | if (v.length == undefined) { |
48 | return [ v, v ]; | 49 | return [v, v]; |
49 | } | 50 | } |
50 | 51 | ||
51 | return [v[0], v[1]]; | 52 | return [v[0], v[1]]; |
52 | } | 53 | }; |
53 | 54 | ||
54 | /** | 55 | /** |
55 | * vec2.inplace_copy | 56 | * RDGE.vec2.inplace_copy |
56 | */ | 57 | */ |
57 | vec2.inplace_copy = function(dst, src) { | 58 | RDGE.vec2.inplace_copy = function (dst, src) { |
58 | dst[0] = src[0]; | 59 | dst[0] = src[0]; |
59 | dst[1] = src[1]; | 60 | dst[1] = src[1]; |
60 | } | 61 | }; |
61 | 62 | ||
62 | /** | 63 | /** |
63 | * vec2.zero | 64 | * RDGE.vec2.zero |
64 | */ | 65 | */ |
65 | vec2.zero = function() { | 66 | RDGE.vec2.zero = function () { |
66 | return [0.0, 0.0]; | 67 | return [0.0, 0.0]; |
67 | } | 68 | }; |
68 | 69 | ||
69 | /** | 70 | /** |
70 | * vec2.up | 71 | * RDGE.vec2.up |
71 | */ | 72 | */ |
72 | vec2.up = function() { | 73 | RDGE.vec2.up = function () { |
73 | return [0.0, 1.0]; | 74 | return [0.0, 1.0]; |
74 | } | 75 | }; |
75 | 76 | ||
76 | /** | 77 | /** |
77 | * vec2.right | 78 | * RDGE.vec2.right |
78 | */ | 79 | */ |
79 | vec2.right = function() { | 80 | RDGE.vec2.right = function () { |
80 | return [1.0, 0.0]; | 81 | return [1.0, 0.0]; |
81 | } | 82 | }; |
82 | 83 | ||
83 | /** | 84 | /** |
84 | * vec2.add | 85 | * RDGE.vec2.add |
85 | */ | 86 | */ |
86 | vec2.add = function(a, b) { | 87 | RDGE.vec2.add = function (a, b) { |
87 | return [a[0] + b[0], a[1] + b[1]]; | 88 | return [a[0] + b[0], a[1] + b[1]]; |
88 | } | 89 | }; |
89 | /** | 90 | /** |
90 | * vec2.sub | 91 | * RDGE.vec2.sub |
91 | */ | 92 | */ |
92 | vec2.sub = function(a, b) { | 93 | RDGE.vec2.sub = function (a, b) { |
93 | return [a[0] - b[0], a[1] - b[1]]; | 94 | return [a[0] - b[0], a[1] - b[1]]; |
94 | } | 95 | }; |
95 | 96 | ||
96 | /** | 97 | /** |
97 | * vec2.mul | 98 | * RDGE.vec2.mul |
98 | */ | 99 | */ |
99 | vec2.mul = function(a, b) { | 100 | RDGE.vec2.mul = function (a, b) { |
100 | return [ a[0] * b[0], a[1] * b[1] ]; | 101 | return [a[0] * b[0], a[1] * b[1]]; |
101 | } | 102 | }; |
102 | 103 | ||
103 | /** | 104 | /** |
104 | * vec2.addMul | 105 | * RDGE.vec2.addMul |
105 | */ | 106 | */ |
106 | vec2.addMul = function(a, b, s) { | 107 | RDGE.vec2.addMul = function (a, b, s) { |
107 | if (s.length != undefined && s.length >= 2) { | 108 | if (s.length != undefined && s.length >= 2) { |
108 | return [a[0] + b[0] * s[0], a[1] + b[1] * s[1]]; | 109 | return [a[0] + b[0] * s[0], a[1] + b[1] * s[1]]; |
109 | } else { | 110 | } else { |
110 | return [a[0] + b[0] * s, a[1] + b[1] * s]; | 111 | return [a[0] + b[0] * s, a[1] + b[1] * s]; |
111 | } | 112 | } |
112 | } | 113 | }; |
113 | 114 | ||
114 | /** | 115 | /** |
115 | * vec2.scale | 116 | * RDGE.vec2.scale |
116 | */ | 117 | */ |
117 | vec2.scale = function(v, s) { | 118 | RDGE.vec2.scale = function (v, s) { |
118 | if (s.length != undefined && s.length >= 2) { | 119 | if (s.length != undefined && s.length >= 2) { |
119 | return [v[0] * s[0], v[1] * s[1]]; | 120 | return [v[0] * s[0], v[1] * s[1]]; |
120 | } else { | 121 | } else { |
121 | return [v[0] * s, v[1] * s]; | 122 | return [v[0] * s, v[1] * s]; |
122 | } | 123 | } |
123 | } | 124 | }; |
124 | 125 | ||
125 | /** | 126 | /** |
126 | * vec2.negate | 127 | * RDGE.vec2.negate |
127 | */ | 128 | */ |
128 | vec2.negate = function(v) { | 129 | RDGE.vec2.negate = function (v) { |
129 | return [ -v[0], -v[1] ]; | 130 | return [-v[0], -v[1]]; |
130 | } | 131 | }; |
131 | 132 | ||
132 | /** | 133 | /** |
133 | * vec2.normalize | 134 | * RDGE.vec2.normalize |
134 | */ | 135 | */ |
135 | vec2.normalize = function(v) { | 136 | RDGE.vec2.normalize = function (v) { |
136 | var l = Math.sqrt(v[0] * v[0], v[1] * v[1] ); | 137 | var l = Math.sqrt(v[0] * v[0], v[1] * v[1]); |
137 | if ( Math.abs(1.0 - l) > 0.0001 ) { | 138 | if (Math.abs(1.0 - l) > 0.0001) { |
138 | var ool = 1.0 / l; | 139 | var ool = 1.0 / l; |
139 | return [ v[0] * ool, v[1] * ool ]; | 140 | return [v[0] * ool, v[1] * ool]; |
140 | } | 141 | } |
141 | return v; | 142 | return v; |
142 | } | 143 | }; |
143 | 144 | ||
144 | /** | 145 |