import { IClone } from "./IClone" ; import { MathUtil } from "./MathUtil" ; /**
 * Describes a 2D-vector.
 */ export class Vector2 implements IClone { internal static readonly _zero = new Vector2 ( 0.0 , 0.0 ) ; internal static readonly _one = new Vector2 ( 1.0 , 1.0 ) ; param left param right param out static add ( left , right , out ) { out . x = left . x + right . x ; out . y = left . y + right . y ; } param left param right param out static subtract ( left , right , out ) { out . x = left . x - right . x ; out . y = left . y - right . y ; } param left param right param out static multiply ( left , right , out ) { out . x = left . x * right . x ; out . y = left . y * right . y ; } param left param right param out static divide ( left , right , out ) { out . x = left . x / right . x ; out . y = left . y / right . y ; } param left param right returns static dot ( left , right ) { return left . x * right . x + left . y * right . y ; } param left param right returns static distance ( left , right ) { const x = right . x - left . x ; const y = right . y - left . y ; return Math . sqrt ( x * x + y * y ) ; } param left param right returns static distanceSquared ( left , right ) { const x = right . x - left . x ; const y = right . y - left . y ; return x * x + y * y ; } param left param right returns static equals ( left , right ) { return MathUtil . equals ( left . x , right . x ) && MathUtil . equals ( left . y , right . y ) ; } param left param right param t param out static lerp ( left , right , t , out ) { const { x , y } = left ; out . x = x + ( right . x - x ) * t ; out . y = y + ( right . y - y ) * t ; } param left param right param out static max ( left , right , out ) { out . x = Math . max ( left . x , right . x ) ; out . y = Math . max ( left . y , right . y ) ; } param left param right param out static min ( left , right , out ) { out . x = Math . min ( left . x , right . x ) ; out . y = Math . min ( left . y , right . y ) ; } param left param out static negate ( left , out ) { out . x = - left . x ; out . y = - left . y ; } param left param out static normalize ( left , out ) { const { x , y } = left ; let len = Math . sqrt ( x * x + y * y ) ; if ( len > MathUtil . zeroTolerance ) { len = 1 / len ; out . x = x * len ; out . y = y * len ; } } param left param s param out static scale ( left , s , out ) { out . x = left . x * s ; out . y = left . y * s ; } /** The x component of the vector. */ x : number ; /** The y component of the vector. */ y : number ; param x param y constructor ( x = 0 , y = 0 ) { this . x = x ; this . y = y ; } param x param y returns setValue ( x , y ) { this . x = x ; this . y = y ; return this ; } param array param offset returns setValueByArray ( array , offset = 0 ) { this . x = array [ offset ] ; this . y = array [ offset + 1 ] ; return this ; } param right returns add ( right ) { this . x += right . x ; this . y += right . y ; return this ; } param right returns subtract ( right ) { this . x -= right . x ; this . y -= right . y ; return this ; } param right returns multiply ( right ) { this . x *= right . x ; this . y *= right . y ; return this ; } param right returns divide ( right ) { this . x /= right . x ; this . y /= right . y ; return this ; } returns length ( ) { const { x , y } = this ; return Math . sqrt ( x * x + y * y ) ; } returns lengthSquared ( ) { const { x , y } = this ; return x * x + y * y ; } returns negate ( ) { this . x = - this . x ; this . y = - this . y ; return this ; } returns normalize ( ) { Vector2 . normalize ( this , this ) ; return this ; } param s returns scale ( s ) { this . x *= s ; this . y *= s ; return this ; } param out param outOffset toArray ( out , outOffset = 0 ) { out [ outOffset ] = this . x ; out [ outOffset + 1 ] = this . y ; } returns clone ( ) { return new Vector2 ( this . x , this . y ) ; } param out returns cloneTo ( out ) { out . x = this . x ; out . y = this . y ; return out ; } }