class Vector { x : number ; y : number ; constructor ( x = 0 , y = 0 ) { this . x = x ; this . y = y ; } add ( vector ) { this . x += vector . x ; this . y += vector . y ; return this ; } subtract ( vector ) { this . x -= vector . x ; this . y -= vector . y ; return this ; } addScalar ( scalar ) { this . x += scalar ; this . y += scalar ; return this ; } divideScalar ( scalar ) { this . x = this . x / scalar ; this . y = this . y / scalar ; return this ; } multiplyScalar ( scalar ) { this . x = this . x * scalar ; this . y = this . y * scalar ; return this ; } getMagnitude ( ) { return Math . sqrt ( this . x * this . x + this . y * this . y ) ; } getAngle ( ) { return Math . atan2 ( this . y , this . x ) ; } clone ( ) { return new Vector ( this . x , this . y ) ; } toUnit ( ) { const magnitude = this . getMagnitude ( ) ; if ( magnitude ) { return this . clone ( ) . divideScalar ( magnitude ) ; } return this . clone ( ) ; } static from ( angle , magnitude ) { return new Vector ( magnitude * Math . cos ( angle ) , magnitude * Math . sin ( angle ) ) ; } static sum ( vectors ) { let v = new Vector ( 0 , 0 ) ; vectors . forEach ( vector => { v . add ( vector ) ; } ) ; return v ; } } export default Vector ;