UNPKG

6.78 kBJavaScriptView Raw
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
8function Vector2( x, y ) {
9
10 this.x = x || 0;
11 this.y = y || 0;
12
13}
14
15Object.defineProperties( Vector2.prototype, {
16
17 "width": {
18
19 get: function () {
20
21 return this.x;
22
23 },
24
25 set: function ( value ) {
26
27 this.x = value;
28
29 }
30
31 },
32
33 "height": {
34
35 get: function () {
36
37 return this.y;
38
39 },
40
41 set: function ( value ) {
42
43 this.y = value;
44
45 }
46
47 }
48
49} );
50
51Object.assign( Vector2.prototype, {
52
53 isVector2: true,
54
55 set: function ( x, y ) {
56
57 this.x = x;
58 this.y = y;
59
60 return this;
61
62 },
63
64 setScalar: function ( scalar ) {
65
66 this.x = scalar;
67 this.y = scalar;
68
69 return this;
70
71 },
72
73 setX: function ( x ) {
74
75 this.x = x;
76
77 return this;
78
79 },
80
81 setY: function ( y ) {
82
83 this.y = y;
84
85 return this;
86
87 },
88
89 setComponent: function ( index, value ) {
90
91 switch ( index ) {
92
93 case 0: this.x = value; break;
94 case 1: this.y = value; break;
95 default: throw new Error( 'index is out of range: ' + index );
96
97 }
98
99 return this;
100
101 },
102
103 getComponent: function ( index ) {
104
105 switch ( index ) {
106
107 case 0: return this.x;
108 case 1: return this.y;
109 default: throw new Error( 'index is out of range: ' + index );
110
111 }
112
113 },
114
115 clone: function () {
116
117 return new this.constructor( this.x, this.y );
118
119 },
120
121 copy: function ( v ) {
122
123 this.x = v.x;
124 this.y = v.y;
125
126 return this;
127
128 },
129
130 add: function ( v, w ) {
131
132 if ( w !== undefined ) {
133
134 console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
135 return this.addVectors( v, w );
136
137 }
138
139 this.x += v.x;
140 this.y += v.y;
141
142 return this;
143
144 },
145
146 addScalar: function ( s ) {
147
148 this.x += s;
149 this.y += s;
150
151 return this;
152
153 },
154
155 addVectors: function ( a, b ) {
156
157 this.x = a.x + b.x;
158 this.y = a.y + b.y;
159
160 return this;
161
162 },
163
164 addScaledVector: function ( v, s ) {
165
166 this.x += v.x * s;
167 this.y += v.y * s;
168
169 return this;
170
171 },
172
173 sub: function ( v, w ) {
174
175 if ( w !== undefined ) {
176
177 console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
178 return this.subVectors( v, w );
179
180 }
181
182 this.x -= v.x;
183 this.y -= v.y;
184
185 return this;
186
187 },
188
189 subScalar: function ( s ) {
190
191 this.x -= s;
192 this.y -= s;
193
194 return this;
195
196 },
197
198 subVectors: function ( a, b ) {
199
200 this.x = a.x - b.x;
201 this.y = a.y - b.y;
202
203 return this;
204
205 },
206
207 multiply: function ( v ) {
208
209 this.x *= v.x;
210 this.y *= v.y;
211
212 return this;
213
214 },
215
216 multiplyScalar: function ( scalar ) {
217
218 this.x *= scalar;
219 this.y *= scalar;
220
221 return this;
222
223 },
224
225 divide: function ( v ) {
226
227 this.x /= v.x;
228 this.y /= v.y;
229
230 return this;
231
232 },
233
234 divideScalar: function ( scalar ) {
235
236 return this.multiplyScalar( 1 / scalar );
237
238 },
239
240 applyMatrix3: function ( m ) {
241
242 var x = this.x, y = this.y;
243 var e = m.elements;
244
245 this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ];
246 this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ];
247
248 return this;
249
250 },
251
252 min: function ( v ) {
253
254 this.x = Math.min( this.x, v.x );
255 this.y = Math.min( this.y, v.y );
256
257 return this;
258
259 },
260
261 max: function ( v ) {
262
263 this.x = Math.max( this.x, v.x );
264 this.y = Math.max( this.y, v.y );
265
266 return this;
267
268 },
269
270 clamp: function ( min, max ) {
271
272 // assumes min < max, componentwise
273
274 this.x = Math.max( min.x, Math.min( max.x, this.x ) );
275 this.y = Math.max( min.y, Math.min( max.y, this.y ) );
276
277 return this;
278
279 },
280
281 clampScalar: function () {
282
283 var min = new Vector2();
284 var max = new Vector2();
285
286 return function clampScalar( minVal, maxVal ) {
287
288 min.set( minVal, minVal );
289 max.set( maxVal, maxVal );
290
291 return this.clamp( min, max );
292
293 };
294
295 }(),
296
297 clampLength: function ( min, max ) {
298
299 var length = this.length();
300
301 return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
302
303 },
304
305 floor: function () {
306
307 this.x = Math.floor( this.x );
308 this.y = Math.floor( this.y );
309
310 return this;
311
312 },
313
314 ceil: function () {
315
316 this.x = Math.ceil( this.x );
317 this.y = Math.ceil( this.y );
318
319 return this;
320
321 },
322
323 round: function () {
324
325 this.x = Math.round( this.x );
326 this.y = Math.round( this.y );
327
328 return this;
329
330 },
331
332 roundToZero: function () {
333
334 this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
335 this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
336
337 return this;
338
339 },
340
341 negate: function () {
342
343 this.x = - this.x;
344 this.y = - this.y;
345
346 return this;
347
348 },
349
350 dot: function ( v ) {
351
352 return this.x * v.x + this.y * v.y;
353
354 },
355
356 lengthSq: function () {
357
358 return this.x * this.x + this.y * this.y;
359
360 },
361
362 length: function () {
363
364 return Math.sqrt( this.x * this.x + this.y * this.y );
365
366 },
367
368 manhattanLength: function () {
369
370 return Math.abs( this.x ) + Math.abs( this.y );
371
372 },
373
374 normalize: function () {
375
376 return this.divideScalar( this.length() || 1 );
377
378 },
379
380 angle: function () {
381
382 // computes the angle in radians with respect to the positive x-axis
383
384 var angle = Math.atan2( this.y, this.x );
385
386 if ( angle < 0 ) angle += 2 * Math.PI;
387
388 return angle;
389
390 },
391
392 distanceTo: function ( v ) {
393
394 return Math.sqrt( this.distanceToSquared( v ) );
395
396 },
397
398 distanceToSquared: function ( v ) {
399
400 var dx = this.x - v.x, dy = this.y - v.y;
401 return dx * dx + dy * dy;
402
403 },
404
405 manhattanDistanceTo: function ( v ) {
406
407 return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
408
409 },
410
411 setLength: function ( length ) {
412
413 return this.normalize().multiplyScalar( length );
414
415 },
416
417 lerp: function ( v, alpha ) {
418
419 this.x += ( v.x - this.x ) * alpha;
420 this.y += ( v.y - this.y ) * alpha;
421
422 return this;
423
424 },
425
426 lerpVectors: function ( v1, v2, alpha ) {
427
428 return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
429
430 },
431
432 equals: function ( v ) {
433
434 return ( ( v.x === this.x ) && ( v.y === this.y ) );
435
436 },
437
438 fromArray: function ( array, offset ) {
439
440 if ( offset === undefined ) offset = 0;
441
442 this.x = array[ offset ];
443 this.y = array[ offset + 1 ];
444
445 return this;
446
447 },
448
449 toArray: function ( array, offset ) {
450
451 if ( array === undefined ) array = [];
452 if ( offset === undefined ) offset = 0;
453
454 array[ offset ] = this.x;
455 array[ offset + 1 ] = this.y;
456
457 return array;
458
459 },
460
461 fromBufferAttribute: function ( attribute, index, offset ) {
462
463 if ( offset !== undefined ) {
464
465 console.warn( 'THREE.Vector2: offset has been removed from .fromBufferAttribute().' );
466
467 }
468
469 this.x = attribute.getX( index );
470 this.y = attribute.getY( index );
471
472 return this;
473
474 },
475
476 rotateAround: function ( center, angle ) {
477
478 var c = Math.cos( angle ), s = Math.sin( angle );
479
480 var x = this.x - center.x;
481 var y = this.y - center.y;
482
483 this.x = x * c - y * s + center.x;
484 this.y = x * s + y * c + center.y;
485
486 return this;
487
488 }
489
490} );
491
492
493export { Vector2 };