1 | /**
|
---|
2 | * @author mrdoob / http://mrdoob.com/
|
---|
3 | * @author philogb / http://blog.thejit.org/
|
---|
4 | * @author egraether / http://egraether.com/
|
---|
5 | * @author zz85 / http://www.lab4games.net/zz85/blog
|
---|
6 | */
|
---|
7 |
|
---|
8 | THREE.Vector2 = function ( x, y ) {
|
---|
9 |
|
---|
10 | this.x = x || 0;
|
---|
11 | this.y = y || 0;
|
---|
12 |
|
---|
13 | };
|
---|
14 |
|
---|
15 | THREE.Vector2.prototype = {
|
---|
16 |
|
---|
17 | constructor: THREE.Vector2,
|
---|
18 |
|
---|
19 | set: function ( x, y ) {
|
---|
20 |
|
---|
21 | this.x = x;
|
---|
22 | this.y = y;
|
---|
23 |
|
---|
24 | return this;
|
---|
25 |
|
---|
26 | },
|
---|
27 |
|
---|
28 | setX: function ( x ) {
|
---|
29 |
|
---|
30 | this.x = x;
|
---|
31 |
|
---|
32 | return this;
|
---|
33 |
|
---|
34 | },
|
---|
35 |
|
---|
36 | setY: function ( y ) {
|
---|
37 |
|
---|
38 | this.y = y;
|
---|
39 |
|
---|
40 | return this;
|
---|
41 |
|
---|
42 | },
|
---|
43 |
|
---|
44 |
|
---|
45 | setComponent: function ( index, value ) {
|
---|
46 |
|
---|
47 | switch ( index ) {
|
---|
48 |
|
---|
49 | case 0: this.x = value; break;
|
---|
50 | case 1: this.y = value; break;
|
---|
51 | default: throw new Error( "index is out of range: " + index );
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | },
|
---|
56 |
|
---|
57 | getComponent: function ( index ) {
|
---|
58 |
|
---|
59 | switch ( index ) {
|
---|
60 |
|
---|
61 | case 0: return this.x;
|
---|
62 | case 1: return this.y;
|
---|
63 | default: throw new Error( "index is out of range: " + index );
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | },
|
---|
68 |
|
---|
69 | copy: function ( v ) {
|
---|
70 |
|
---|
71 | this.x = v.x;
|
---|
72 | this.y = v.y;
|
---|
73 |
|
---|
74 | return this;
|
---|
75 |
|
---|
76 | },
|
---|
77 |
|
---|
78 | add: function ( v, w ) {
|
---|
79 |
|
---|
80 | if ( w !== undefined ) {
|
---|
81 |
|
---|
82 | console.warn( 'DEPRECATED: Vector2\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
|
---|
83 | return this.addVectors( v, w );
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | this.x += v.x;
|
---|
88 | this.y += v.y;
|
---|
89 |
|
---|
90 | return this;
|
---|
91 |
|
---|
92 | },
|
---|
93 |
|
---|
94 | addVectors: function ( a, b ) {
|
---|
95 |
|
---|
96 | this.x = a.x + b.x;
|
---|
97 | this.y = a.y + b.y;
|
---|
98 |
|
---|
99 | return this;
|
---|
100 |
|
---|
101 | },
|
---|
102 |
|
---|
103 | addScalar: function ( s ) {
|
---|
104 |
|
---|
105 | this.x += s;
|
---|
106 | this.y += s;
|
---|
107 |
|
---|
108 | return this;
|
---|
109 |
|
---|
110 | },
|
---|
111 |
|
---|
112 | sub: function ( v, w ) {
|
---|
113 |
|
---|
114 | if ( w !== undefined ) {
|
---|
115 |
|
---|
116 | console.warn( 'DEPRECATED: Vector2\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
|
---|
117 | return this.subVectors( v, w );
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 | this.x -= v.x;
|
---|
122 | this.y -= v.y;
|
---|
123 |
|
---|
124 | return this;
|
---|
125 |
|
---|
126 | },
|
---|
127 |
|
---|
128 | subVectors: function ( a, b ) {
|
---|
129 |
|
---|
130 | this.x = a.x - b.x;
|
---|
131 | this.y = a.y - b.y;
|
---|
132 |
|
---|
133 | return this;
|
---|
134 |
|
---|
135 | },
|
---|
136 |
|
---|
137 | multiplyScalar: function ( s ) {
|
---|
138 |
|
---|
139 | this.x *= s;
|
---|
140 | this.y *= s;
|
---|
141 |
|
---|
142 | return this;
|
---|
143 |
|
---|
144 | },
|
---|
145 |
|
---|
146 | divideScalar: function ( scalar ) {
|
---|
147 |
|
---|
148 | if ( scalar !== 0 ) {
|
---|
149 |
|
---|
150 | var invScalar = 1 / scalar;
|
---|
151 |
|
---|
152 | this.x *= invScalar;
|
---|
153 | this.y *= invScalar;
|
---|
154 |
|
---|
155 | } else {
|
---|
156 |
|
---|
157 | this.x = 0;
|
---|
158 | this.y = 0;
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | return this;
|
---|
163 |
|
---|
164 | },
|
---|
165 |
|
---|
166 | min: function ( v ) {
|
---|
167 |
|
---|
168 | if ( this.x > v.x ) {
|
---|
169 |
|
---|
170 | this.x = v.x;
|
---|
171 |
|
---|
172 | }
|
---|
173 |
|
---|
174 | if ( this.y > v.y ) {
|
---|
175 |
|
---|
176 | this.y = v.y;
|
---|
177 |
|
---|
178 | }
|
---|
179 |
|
---|
180 | return this;
|
---|
181 |
|
---|
182 | },
|
---|
183 |
|
---|
184 | max: function ( v ) {
|
---|
185 |
|
---|
186 | if ( this.x < v.x ) {
|
---|
187 |
|
---|
188 | this.x = v.x;
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | if ( this.y < v.y ) {
|
---|
193 |
|
---|
194 | this.y = v.y;
|
---|
195 |
|
---|
196 | }
|
---|
197 |
|
---|
198 | return this;
|
---|
199 |
|
---|
200 | },
|
---|
201 |
|
---|
202 | clamp: function ( min, max ) {
|
---|
203 |
|
---|
204 | // This function assumes min < max, if this assumption isn't true it will not operate correctly
|
---|
205 |
|
---|
206 | if ( this.x < min.x ) {
|
---|
207 |
|
---|
208 | this.x = min.x;
|
---|
209 |
|
---|
210 | } else if ( this.x > max.x ) {
|
---|
211 |
|
---|
212 | this.x = max.x;
|
---|
213 |
|
---|
214 | }
|
---|
215 |
|
---|
216 | if ( this.y < min.y ) {
|
---|
217 |
|
---|
218 | this.y = min.y;
|
---|
219 |
|
---|
220 | } else if ( this.y > max.y ) {
|
---|
221 |
|
---|
222 | this.y = max.y;
|
---|
223 |
|
---|
224 | }
|
---|
225 |
|
---|
226 | return this;
|
---|
227 |
|
---|
228 | },
|
---|
229 |
|
---|
230 | negate: function() {
|
---|
231 |
|
---|
232 | return this.multiplyScalar( - 1 );
|
---|
233 |
|
---|
234 | },
|
---|
235 |
|
---|
236 | dot: function ( v ) {
|
---|
237 |
|
---|
238 | return this.x * v.x + this.y * v.y;
|
---|
239 |
|
---|
240 | },
|
---|
241 |
|
---|
242 | lengthSq: function () {
|
---|
243 |
|
---|
244 | return this.x * this.x + this.y * this.y;
|
---|
245 |
|
---|
246 | },
|
---|
247 |
|
---|
248 | length: function () {
|
---|
249 |
|
---|
250 | return Math.sqrt( this.x * this.x + this.y * this.y );
|
---|
251 |
|
---|
252 | },
|
---|
253 |
|
---|
254 | normalize: function () {
|
---|
255 |
|
---|
256 | return this.divideScalar( this.length() );
|
---|
257 |
|
---|
258 | },
|
---|
259 |
|
---|
260 | distanceTo: function ( v ) {
|
---|
261 |
|
---|
262 | return Math.sqrt( this.distanceToSquared( v ) );
|
---|
263 |
|
---|
264 | },
|
---|
265 |
|
---|
266 | distanceToSquared: function ( v ) {
|
---|
267 |
|
---|
268 | var dx = this.x - v.x, dy = this.y - v.y;
|
---|
269 | return dx * dx + dy * dy;
|
---|
270 |
|
---|
271 | },
|
---|
272 |
|
---|
273 | setLength: function ( l ) {
|
---|
274 |
|
---|
275 | var oldLength = this.length();
|
---|
276 |
|
---|
277 | if ( oldLength !== 0 && l !== oldLength ) {
|
---|
278 |
|
---|
279 | this.multiplyScalar( l / oldLength );
|
---|
280 | }
|
---|
281 |
|
---|
282 | return this;
|
---|
283 |
|
---|
284 | },
|
---|
285 |
|
---|
286 | lerp: function ( v, alpha ) {
|
---|
287 |
|
---|
288 | this.x += ( v.x - this.x ) * alpha;
|
---|
289 | this.y += ( v.y - this.y ) * alpha;
|
---|
290 |
|
---|
291 | return this;
|
---|
292 |
|
---|
293 | },
|
---|
294 |
|
---|
295 | equals: function( v ) {
|
---|
296 |
|
---|
297 | return ( ( v.x === this.x ) && ( v.y === this.y ) );
|
---|
298 |
|
---|
299 | },
|
---|
300 |
|
---|
301 | fromArray: function ( array ) {
|
---|
302 |
|
---|
303 | this.x = array[ 0 ];
|
---|
304 | this.y = array[ 1 ];
|
---|
305 |
|
---|
306 | return this;
|
---|
307 |
|
---|
308 | },
|
---|
309 |
|
---|
310 | toArray: function () {
|
---|
311 |
|
---|
312 | return [ this.x, this.y ];
|
---|
313 |
|
---|
314 | },
|
---|
315 |
|
---|
316 | clone: function () {
|
---|
317 |
|
---|
318 | return new THREE.Vector2( this.x, this.y );
|
---|
319 |
|
---|
320 | }
|
---|
321 |
|
---|
322 | };
|
---|