Vector2D

Vector2D is a light-weight 2D vector class that implements several vector operations.

Note: modifying "to" methods that do not return a new Vector2D are more efficient.

Constructor

new Vector2D(x, y)

Instantiate a new Vector2D.

Parameters:
NameTypeDescription
xNumber
yNumber

Methods

add(v) → {Vector2D}

returns a new added vector denoted by this + v.

Parameters:
NameTypeDescription
vVector2D

vector to be added

Returns:
Type: 
Vector2D

addTo(v)

adds a vector to the current vector (this = this + v).

Parameters:
NameTypeDescription
vVector2D

angleBetween(v, type) → {number}

return the angle between this and v.

Parameters:
NameTypeDefaultDescription
vVector2D
typebooleantrue

default set to true to return radians. Setting to false will return degrees.

Returns:
Type: 
number

cross(v) → {Number}

returns the "cross-product" of two vectors (this and v). Since these are 2D vectors, this is the z-coordinate of the 3D counter parts of these 2D vectors. It returns a scalar.

Parameters:
NameTypeDescription
vVector2D
Returns:
Type: 
Number

dot(v) → {Number}

returns the dot product of two vectors (this and v).

Parameters:
NameTypeDescription
vVector2D
Returns:
Type: 
Number

mag() → {Number}

returns the magnitude of the vector.

Returns:
Type: 
Number

magSqr() → {Number}

returns the magnitude squared of the vector.

Returns:
Type: 
Number

mult(a) → {Vector2D}

returns a new scalar-multiplied vector denoted by this * a.

Parameters:
NameTypeDescription
aNumber

scalar multiple

Returns:
Type: 
Vector2D

multTo(a)

multiplies a scalar to the current vector (this = this * a).

Parameters:
NameTypeDescription
aNumber

normalize() → {Vector2D}

returns a new normalized Vector2D.

Returns:
Type: 
Vector2D

normalizeTo()

normalizes the this Vector2D.

reflect(normal)

Performs the mirror reflection for this about a normal vector.

Parameters:
NameTypeDescription
normalVector2D
Returns:

Vector2D

reflectTo(normal)

Performs the mirror reflection for this about a normal vector. (modifies self)

Parameters:
NameTypeDescription
normalVector2D

sub(v) → {Vector2D}

returns a new subtracted vector denoted by this - v.

Parameters:
NameTypeDescription
vVector2D

vector to be added

Returns:
Type: 
Vector2D

subTo(v)

subtracts a vector to the current vector (this = this - v).

Parameters:
NameTypeDescription
vVector2D