enum Axis { X , Y , Both } class Vector { constructor ( public x , public y ) { } public add ( v ) { this . x += v . x ; this . y += v . y ; } public mul ( n ) { this . x *= n ; this . y *= n ; } public sub ( v ) { this . x -= v . x ; this . y -= v . y ; } public static sub ( v1 , v2 ) { return new Vector ( v1 . x - v2 . x , v1 . y - v2 . y ) ; } public static randomized ( low , high ) { return new Vector ( low . x + ( ( Math . random ( ) * high . x ) - low . x ) , low . y + ( ( Math . random ( ) * high . y ) - low . y ) ) ; } public div ( n ) { this . x /= n ; this . y /= n ; } public mag ( axis = Axis . Both ) { let [ x , y ] = [ this . x , this . y ] , total = 0 ; if ( axis !== Axis . Y ) { total += x * x ; } if ( axis !== Axis . X ) { total += y * y ; } return Math . sqrt ( total ) ; } public distanceFrom ( v , axis = Axis . Both ) { let distance = Vector . sub ( v , this ) ; return distance . mag ( axis ) ; } public normalize ( ) { let m = this . mag ( ) ; if ( m != 0 ) { this . div ( m ) ; } } public clone ( ) { return new Vector ( this . x , this . y ) ; } public isEmpty ( ) { return ( this . x === 0 && this . y === 0 ) ; } }