Vector

Class representing a Vector.

Constructor

new Vector(x, y)

Create a vector from coordinates.

Parameters:
NameTypeDefaultDescription
xnumber0

x

ynumber0

y

Classes

Vector

Methods

add(vector) → {Vector}

Add a vector to this vector.

Parameters:
NameTypeDescription
vectorVector
Returns:
Type: 
Vector

clone() → {Vector}

Make a copy of this vector.

Returns:
Type: 
Vector

distance(vector) → {number}

Calculate the distance to another vector from this vector

Parameters:
NameTypeDescription
vectorVector
Returns:
Type: 
number

equals(vector) → {boolean}

Check if this vector is equivalent to another vector.

Parameters:
NameTypeDescription
vectorVector
Returns:
Type: 
boolean

getAngle() → {number}

Get the angle of this vector with respect to the positive x-axis.

Returns:

Angle in radians

Type: 
number

getMagnitude() → {number}

Get the magnitude of this vector.

Returns:

The magnitude (Euclidean distance) of this vector

Type: 
number

isOnLine(line) → {boolean}

Check if this vector is a point on a Line.

Parameters:
NameTypeDescription
lineLine
Returns:
Type: 
boolean

multiply(scalar) → {Vector}

Multiply this vector by a scalar.

Parameters:
NameTypeDescription
scalarnumber
Returns:
Type: 
Vector

nearestNeighbour(array, n) → {Array.<Vector>}

Finds the n nearest neighbours to this vector, from a given array of vectors.

Parameters:
NameTypeDescription
arrayArray.<Vector>

An array of Vectors to check.

nnumber

The number of neighbours to find (must be > 0).

Returns:
Type: 
Array.<Vector>

normalize() → {Vector}

Normalize this vector by setting it's magnitude to 1.

Returns:
Type: 
Vector

rotate(angle) → {Vector}

Rotate this vector by a specified angle.

Parameters:
NameTypeDescription
anglenumber

The angle to rotate the vector by, in radians.

Returns:

This vector after rotation.

Type: 
Vector

setMagnitude(magnitude) → {Vector}

Set the magnitude of this vector.

Parameters:
NameTypeDescription
magnitudenumber
Returns:
Type: 
Vector

subtract(vector) → {Vector}

Subtract a vector from this vector.

Parameters:
NameTypeDescription
vectorVector
Returns:
Type: 
Vector

(static) add(v1, v2) → {Vector}

Add two vectors.

Parameters:
NameTypeDescription
v1Vector
v2Vector
Returns:
Type: 
Vector

(static) cross(v1, v2) → {number}

Calculate the cross product of two 2D vectors.

Parameters:
NameTypeDescription
v1Vector
v2Vector
Returns:
Type: 
number

(static) distance(v1, v2) → {number}

Calculate the distance between two vectors.

Parameters:
NameTypeDescription
v1Vector
v2Vector
Returns:
Type: 
number

(static) dot(v1, v2) → {number}

Calculate the dot product of two vectors.

Parameters:
NameTypeDescription
v1Vector
v2Vector
Returns:
Type: 
number

(static) fromAngle(angle, magnitudeopt) → {Vector}

Create a vector from an angle.

Parameters:
NameTypeAttributesDefaultDescription
anglenumber

The angle of the vector in radians.

magnitudenumber<optional>
1

The magnitude of the vector.

Returns:
Type: 
Vector

(static) fromArray(array) → {Vector}

Create a vector from an array.

Parameters:
NameTypeDescription
arrayarray

[x, y]

Returns:
Type: 
Vector

(static) lerp(v1, v2, amount) → {Vector}

Linear interpolation between vectors.

Parameters:
NameTypeDescription
v1Vector
v2Vector
amountnumber
Returns:
Type: 
Vector

(static) subtract(v1, v2) → {Vector}

Subtract two vectors.

Parameters:
NameTypeDescription
v1Vector
v2Vector
Returns:
Type: 
Vector