|
@@ -1,85 +1,77 @@
|
|
|
-function Vector2( x = 0, y = 0 ) {
|
|
|
+class Vector2 {
|
|
|
|
|
|
- this.x = x;
|
|
|
- this.y = y;
|
|
|
+ constructor( x = 0, y = 0 ) {
|
|
|
|
|
|
-}
|
|
|
-
|
|
|
-Object.defineProperties( Vector2.prototype, {
|
|
|
-
|
|
|
- "width": {
|
|
|
-
|
|
|
- get: function () {
|
|
|
-
|
|
|
- return this.x;
|
|
|
+ this.x = x;
|
|
|
+ this.y = y;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- set: function ( value ) {
|
|
|
+ get width() {
|
|
|
|
|
|
- this.x = value;
|
|
|
+ return this.x;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+ set width( value ) {
|
|
|
|
|
|
- "height": {
|
|
|
+ this.x = value;
|
|
|
|
|
|
- get: function () {
|
|
|
+ }
|
|
|
|
|
|
- return this.y;
|
|
|
+ get height() {
|
|
|
|
|
|
- },
|
|
|
+ return this.y;
|
|
|
|
|
|
- set: function ( value ) {
|
|
|
+ }
|
|
|
|
|
|
- this.y = value;
|
|
|
+ set height( value ) {
|
|
|
|
|
|
- }
|
|
|
+ this.y = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
-} );
|
|
|
+ get isVector2() {
|
|
|
|
|
|
-Object.assign( Vector2.prototype, {
|
|
|
+ return true
|
|
|
|
|
|
- isVector2: true,
|
|
|
+ }
|
|
|
|
|
|
- set: function ( x, y ) {
|
|
|
+ set( x, y ) {
|
|
|
|
|
|
this.x = x;
|
|
|
this.y = y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- setScalar: function ( scalar ) {
|
|
|
+ setScalar( scalar ) {
|
|
|
|
|
|
this.x = scalar;
|
|
|
this.y = scalar;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- setX: function ( x ) {
|
|
|
+ setX( x ) {
|
|
|
|
|
|
this.x = x;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- setY: function ( y ) {
|
|
|
+ setY( y ) {
|
|
|
|
|
|
this.y = y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- setComponent: function ( index, value ) {
|
|
|
+ setComponent( index, value ) {
|
|
|
|
|
|
switch ( index ) {
|
|
|
|
|
@@ -91,9 +83,9 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- getComponent: function ( index ) {
|
|
|
+ getComponent( index ) {
|
|
|
|
|
|
switch ( index ) {
|
|
|
|
|
@@ -103,24 +95,24 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
}
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- clone: function () {
|
|
|
+ clone() {
|
|
|
|
|
|
return new this.constructor( this.x, this.y );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- copy: function ( v ) {
|
|
|
+ copy( v ) {
|
|
|
|
|
|
this.x = v.x;
|
|
|
this.y = v.y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- add: function ( v, w ) {
|
|
|
+ add( v, w ) {
|
|
|
|
|
|
if ( w !== undefined ) {
|
|
|
|
|
@@ -134,36 +126,36 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- addScalar: function ( s ) {
|
|
|
+ addScalar( s ) {
|
|
|
|
|
|
this.x += s;
|
|
|
this.y += s;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- addVectors: function ( a, b ) {
|
|
|
+ addVectors( a, b ) {
|
|
|
|
|
|
this.x = a.x + b.x;
|
|
|
this.y = a.y + b.y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- addScaledVector: function ( v, s ) {
|
|
|
+ addScaledVector( v, s ) {
|
|
|
|
|
|
this.x += v.x * s;
|
|
|
this.y += v.y * s;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- sub: function ( v, w ) {
|
|
|
+ sub( v, w ) {
|
|
|
|
|
|
if ( w !== undefined ) {
|
|
|
|
|
@@ -177,60 +169,60 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- subScalar: function ( s ) {
|
|
|
+ subScalar( s ) {
|
|
|
|
|
|
this.x -= s;
|
|
|
this.y -= s;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- subVectors: function ( a, b ) {
|
|
|
+ subVectors( a, b ) {
|
|
|
|
|
|
this.x = a.x - b.x;
|
|
|
this.y = a.y - b.y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- multiply: function ( v ) {
|
|
|
+ multiply( v ) {
|
|
|
|
|
|
this.x *= v.x;
|
|
|
this.y *= v.y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- multiplyScalar: function ( scalar ) {
|
|
|
+ multiplyScalar( scalar ) {
|
|
|
|
|
|
this.x *= scalar;
|
|
|
this.y *= scalar;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- divide: function ( v ) {
|
|
|
+ divide( v ) {
|
|
|
|
|
|
this.x /= v.x;
|
|
|
this.y /= v.y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- divideScalar: function ( scalar ) {
|
|
|
+ divideScalar( scalar ) {
|
|
|
|
|
|
return this.multiplyScalar( 1 / scalar );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- applyMatrix3: function ( m ) {
|
|
|
+ applyMatrix3( m ) {
|
|
|
|
|
|
const x = this.x, y = this.y;
|
|
|
const e = m.elements;
|
|
@@ -240,27 +232,27 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- min: function ( v ) {
|
|
|
+ min( v ) {
|
|
|
|
|
|
this.x = Math.min( this.x, v.x );
|
|
|
this.y = Math.min( this.y, v.y );
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- max: function ( v ) {
|
|
|
+ max( v ) {
|
|
|
|
|
|
this.x = Math.max( this.x, v.x );
|
|
|
this.y = Math.max( this.y, v.y );
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- clamp: function ( min, max ) {
|
|
|
+ clamp( min, max ) {
|
|
|
|
|
|
// assumes min < max, componentwise
|
|
|
|
|
@@ -269,107 +261,107 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- clampScalar: function ( minVal, maxVal ) {
|
|
|
+ clampScalar( minVal, maxVal ) {
|
|
|
|
|
|
this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
|
|
|
this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- clampLength: function ( min, max ) {
|
|
|
+ clampLength( min, max ) {
|
|
|
|
|
|
const length = this.length();
|
|
|
|
|
|
return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- floor: function () {
|
|
|
+ floor() {
|
|
|
|
|
|
this.x = Math.floor( this.x );
|
|
|
this.y = Math.floor( this.y );
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- ceil: function () {
|
|
|
+ ceil() {
|
|
|
|
|
|
this.x = Math.ceil( this.x );
|
|
|
this.y = Math.ceil( this.y );
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- round: function () {
|
|
|
+ round() {
|
|
|
|
|
|
this.x = Math.round( this.x );
|
|
|
this.y = Math.round( this.y );
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- roundToZero: function () {
|
|
|
+ roundToZero() {
|
|
|
|
|
|
this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
|
|
|
this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- negate: function () {
|
|
|
+ negate() {
|
|
|
|
|
|
this.x = - this.x;
|
|
|
this.y = - this.y;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- dot: function ( v ) {
|
|
|
+ dot( v ) {
|
|
|
|
|
|
return this.x * v.x + this.y * v.y;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- cross: function ( v ) {
|
|
|
+ cross( v ) {
|
|
|
|
|
|
return this.x * v.y - this.y * v.x;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- lengthSq: function () {
|
|
|
+ lengthSq() {
|
|
|
|
|
|
return this.x * this.x + this.y * this.y;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- length: function () {
|
|
|
+ length() {
|
|
|
|
|
|
return Math.sqrt( this.x * this.x + this.y * this.y );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- manhattanLength: function () {
|
|
|
+ manhattanLength() {
|
|
|
|
|
|
return Math.abs( this.x ) + Math.abs( this.y );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- normalize: function () {
|
|
|
+ normalize() {
|
|
|
|
|
|
return this.divideScalar( this.length() || 1 );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- angle: function () {
|
|
|
+ angle() {
|
|
|
|
|
|
// computes the angle in radians with respect to the positive x-axis
|
|
|
|
|
@@ -377,58 +369,58 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return angle;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- distanceTo: function ( v ) {
|
|
|
+ distanceTo( v ) {
|
|
|
|
|
|
return Math.sqrt( this.distanceToSquared( v ) );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- distanceToSquared: function ( v ) {
|
|
|
+ distanceToSquared( v ) {
|
|
|
|
|
|
const dx = this.x - v.x, dy = this.y - v.y;
|
|
|
return dx * dx + dy * dy;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- manhattanDistanceTo: function ( v ) {
|
|
|
+ manhattanDistanceTo( v ) {
|
|
|
|
|
|
return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- setLength: function ( length ) {
|
|
|
+ setLength( length ) {
|
|
|
|
|
|
return this.normalize().multiplyScalar( length );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- lerp: function ( v, alpha ) {
|
|
|
+ lerp( v, alpha ) {
|
|
|
|
|
|
this.x += ( v.x - this.x ) * alpha;
|
|
|
this.y += ( v.y - this.y ) * alpha;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- lerpVectors: function ( v1, v2, alpha ) {
|
|
|
+ lerpVectors( v1, v2, alpha ) {
|
|
|
|
|
|
this.x = v1.x + ( v2.x - v1.x ) * alpha;
|
|
|
this.y = v1.y + ( v2.y - v1.y ) * alpha;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- equals: function ( v ) {
|
|
|
+ equals( v ) {
|
|
|
|
|
|
return ( ( v.x === this.x ) && ( v.y === this.y ) );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- fromArray: function ( array, offset ) {
|
|
|
+ fromArray( array, offset ) {
|
|
|
|
|
|
if ( offset === undefined ) offset = 0;
|
|
|
|
|
@@ -437,9 +429,9 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- toArray: function ( array, offset ) {
|
|
|
+ toArray( array, offset ) {
|
|
|
|
|
|
if ( array === undefined ) array = [];
|
|
|
if ( offset === undefined ) offset = 0;
|
|
@@ -449,9 +441,9 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return array;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- fromBufferAttribute: function ( attribute, index, offset ) {
|
|
|
+ fromBufferAttribute( attribute, index, offset ) {
|
|
|
|
|
|
if ( offset !== undefined ) {
|
|
|
|
|
@@ -464,9 +456,9 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- rotateAround: function ( center, angle ) {
|
|
|
+ rotateAround( center, angle ) {
|
|
|
|
|
|
const c = Math.cos( angle ), s = Math.sin( angle );
|
|
|
|
|
@@ -478,9 +470,9 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- random: function () {
|
|
|
+ random() {
|
|
|
|
|
|
this.x = Math.random();
|
|
|
this.y = Math.random();
|
|
@@ -489,7 +481,6 @@ Object.assign( Vector2.prototype, {
|
|
|
|
|
|
}
|
|
|
|
|
|
-} );
|
|
|
-
|
|
|
+}
|
|
|
|
|
|
export { Vector2 };
|