Class: Vector

Vector()

new Vector()

A basic 2D Vector class that provides simple algebraic functionality in the form of 2D Vectors.

We use Getters/setters for both principle properties (x & y) as well as virtual properties (rotation, length etc.).

Version:
  • 0.1.1
Author:
Source:

Members

angle :number

(getter/setter) The angle of the vector, in radians

Type:
  • number
Default Value:
  • 0
Source:

angleInDegrees :number

(getter/setter) The angle of the vector, in radians

Type:
  • number
Default Value:
  • 0
Source:

(readonly) area :number

(getter/setter) Vector area.

Type:
  • number
Source:

height :number

(getter/setter) Vector height. Alias of x

Type:
  • number
Source:

length :number

(getter/setter) The length of the vector

Type:
  • number
Default Value:
  • 0
Source:

lengthSquared :number

(getter/setter) The length of the vector presented as a square. If you're using length for comparison, this is quicker.

Type:
  • number
Default Value:
  • 0
Source:

slope :number

(getter/setter) Vector slope.

Type:
  • number
Source:

width :number

(getter/setter) Vector width. Alias of x

Type:
  • number
Source:

x :number

(getter/setter) The x value of the vector.

Type:
  • number
Default Value:
  • 0
Source:

y :number

(getter/setter) The y value of the vector.

Type:
  • number
Default Value:
  • 0
Source:

Methods

add(vector) → {Vector}

Adds one vector to another.

Parameters:
Name Type Description
vector Vector

The vector to add to this one

Source:
Returns:

Returns itself, modified

Type
Vector

addNew(vector) → {Vector}

Clones the vector and adds the vector to it instead

Parameters:
Name Type Description
vector Vector

The vector to add to this one

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

addScalar(scalar) → {Vector}

Adds a scalar to the vector, modifying both the x and y

Parameters:
Name Type Description
scalar number

The scalar to add to the vector

Source:
Returns:

Returns itself, modified

Type
Vector

addScalarNew(scalar) → {Vector}

Clones the vector and adds the scalar to it instead

Parameters:
Name Type Description
scalar number

The scalar to add to the vector

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

clone() → {Vector}

Clones the vector

Source:
Returns:

The cloned vector

Type
Vector

cross(vector) → {number}

Calculates the cross product between this and the supplied vector.

Parameters:
Name Type Description
vector Vector

The vector object against which to calculate the cross product

Source:
Returns:

The cross product of the two vectors

Type
number
Example
// returns -2
new Vector(2, -3).cross(new Vector(-4, 2))
new Vector(-4, 2).cross(new Vector(2, -3))
// returns 2
new Vector(2, -4).cross(new Vector(-3, 2))

distance(vector) → {number}

Calculates the distance between this and the supplied vector

Parameters:
Name Type Description
vector Vector

The vector to calculate the distance from

Source:
Returns:

The distance between this and the supplied vector

Type
number

distanceX(vector) → {number}

Calculates the distance on the X axis between this and the supplied vector

Parameters:
Name Type Description
vector Vector

The vector to calculate the distance from

Source:
Returns:

The distance, along the x axis, between this and the supplied vector

Type
number

distanceY(vector) → {number}

Calculated the distance on the Y axis between this and the supplied vector

Parameters:
Name Type Description
vector Vector

The vector to calculate the distance from

Source:
Returns:

The distance, along the y axis, between this and the supplied vector

Type
number

divide(vector) → {Vector}

Divides one vector by another.

Parameters:
Name Type Description
vector Vector

The vector to divide this by

Source:
Returns:

Returns itself, modified

Type
Vector

divideNew(vector) → {Vector}

Clones the vector and divides it by the vector instead

Parameters:
Name Type Description
vector Vector

The vector to divide the clone by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

divideScalar(scalar) → {Vector}

Divides the vector by a scalar.

Parameters:
Name Type Description
scalar number

The scalar to divide both x and y by

Source:
Returns:

Returns itself, modified

Type
Vector

divideScalarNew(scalar) → {Vector}

Clones the vector and divides it by the provided scalar.

Parameters:
Name Type Description
scalar number

The scalar to divide both x and y by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

dot(vector) → {number}

Calculates the dot product between this and a supplied vector

Parameters:
Name Type Description
vector Vector

The vector object against which to calculate the dot product

Source:
Returns:

The dot product of the two vectors

Type
number
Example
// returns -14
new Vector(2, -3).dot(new Vector(-4, 2))
new Vector(-4, 2).dot(new Vector(2, -3))
new Vector(2, -4).dot(new Vector(-3, 2))

multiply(vector) → {Vector}

Multiplies one vector by another.

Parameters:
Name Type Description
vector Vector

The vector to multiply this by

Source:
Returns:

Returns itself, modified

Type
Vector

multiplyNew(vector) → {Vector}

Clones the vector and multiplies it by the vector instead

Parameters:
Name Type Description
vector Vector

The vector to multiply the clone by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

multiplyScalar(scalar) → {Vector}

Multiplies the vector by a scalar.

Parameters:
Name Type Description
scalar number

The scalar to multiply both x and y by

Source:
Returns:

Returns itself, modified

Type
Vector

multiplyScalarNew(scalar) → {Vector}

Clones the vector and multiplies it by the provided scalar.

Parameters:
Name Type Description
scalar number

The scalar to multiply both x and y by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

normalise() → {Vector}

Normalises the vector down to a length of 1 unit

Source:
Returns:

Returns itself, modified

Type
Vector

normaliseNew() → {Vector}

Clones the vector and normalises it

Source:
Returns:

Returns a clone of itself, modified

Type
Vector

reset(x, y)

Resets the vector coordinates

Parameters:
Name Type Description
x number

The x coord

y number

The y coord

Source:

resetToVector(v)

Resets the vector coordinates to another vector object

Parameters:
Name Type Description
v Vector

The vector object to use to reset the coordinates

Source:

rotate(radian) → {Vector}

Rotates a vecor by a given amount, provided in radians.

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector by

Source:
Returns:

Returns itself, modified

Type
Vector

rotateBy()

Alias of rotate

Source:

rotateByNew()

Alias of rotateNew

Source:

rotateDeg(degrees) → {Vector}

Rotates a vecor by a given amount, provided in degrees. Converts the degree value to radians and runs the rotaet method.

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector by

Source:
Returns:

Returns itself, modified

Type
Vector

rotateDegBy()

Alias of rotateDeg

Source:

rotateDegByNew()

Alias of rotateDegNew

Source:

rotateDegNew(degrees) → {Vector}

Clones the vector and rotates it by the supplied degree value

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

rotateNew(radian) → {Vector}

Clones the vector and rotates it by the supplied radian value

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

rotateTo(radian) → {Vector}

Rotates a vector to a specific angle

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector to

Source:
Returns:

Returns itself, modified

Type
Vector

rotateToDeg(degrees) → {Vector}

Rotates a vecor to a given amount, provided in degrees. Converts the degree value to radians and runs the rotateTo method.

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector to

Source:
Returns:

Returns itself, modified

Type
Vector

rotateToDegNew(degrees) → {Vector}

Clones the vector and rotates it to the supplied degree value

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector to

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

rotateToNew(radian) → {Vector}

Clones the vector and rotates it to the supplied radian value

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector to

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

scale()

Alias of multiplyScalar

Source:

scaleNew()

Alias of multiplyScalarNew

Source:

subtract(vector) → {Vector}

Subtracts one vector from another.

Parameters:
Name Type Description
vector Vector

The vector to subtract from this one

Source:
Returns:

Returns itself, modified

Type
Vector

subtractNew(vector) → {Vector}

Clones the vector and subtracts the vector from it instead

Parameters:
Name Type Description
vector Vector

The vector to subtract from this one

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

subtractScalar(scalar) → {Vector}

Subtracts a scalar from the vector, modifying both the x and y

Parameters:
Name Type Description
scalar number

The scalar to subtract from the vector

Source:
Returns:

Returns itself, modified

Type
Vector

subtractScalarNew(scalar) → {Vector}

Clones the vector and subtracts the scalar from it instead

Parameters:
Name Type Description
scalar number

The scalar to add to the vector

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

Vector(x, y)

new Vector(x, y)

The Vector Class constructor

Parameters:
Name Type Description
x number

The x coord

y number

The y coord

Source:

Members

angle :number

(getter/setter) The angle of the vector, in radians

Type:
  • number
Default Value:
  • 0
Source:

angleInDegrees :number

(getter/setter) The angle of the vector, in radians

Type:
  • number
Default Value:
  • 0
Source:

(readonly) area :number

(getter/setter) Vector area.

Type:
  • number
Source:

height :number

(getter/setter) Vector height. Alias of x

Type:
  • number
Source:

length :number

(getter/setter) The length of the vector

Type:
  • number
Default Value:
  • 0
Source:

lengthSquared :number

(getter/setter) The length of the vector presented as a square. If you're using length for comparison, this is quicker.

Type:
  • number
Default Value:
  • 0
Source:

slope :number

(getter/setter) Vector slope.

Type:
  • number
Source:

width :number

(getter/setter) Vector width. Alias of x

Type:
  • number
Source:

x :number

(getter/setter) The x value of the vector.

Type:
  • number
Default Value:
  • 0
Source:

y :number

(getter/setter) The y value of the vector.

Type:
  • number
Default Value:
  • 0
Source:

Methods

add(vector) → {Vector}

Adds one vector to another.

Parameters:
Name Type Description
vector Vector

The vector to add to this one

Source:
Returns:

Returns itself, modified

Type
Vector

addNew(vector) → {Vector}

Clones the vector and adds the vector to it instead

Parameters:
Name Type Description
vector Vector

The vector to add to this one

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

addScalar(scalar) → {Vector}

Adds a scalar to the vector, modifying both the x and y

Parameters:
Name Type Description
scalar number

The scalar to add to the vector

Source:
Returns:

Returns itself, modified

Type
Vector

addScalarNew(scalar) → {Vector}

Clones the vector and adds the scalar to it instead

Parameters:
Name Type Description
scalar number

The scalar to add to the vector

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

clone() → {Vector}

Clones the vector

Source:
Returns:

The cloned vector

Type
Vector

cross(vector) → {number}

Calculates the cross product between this and the supplied vector.

Parameters:
Name Type Description
vector Vector

The vector object against which to calculate the cross product

Source:
Returns:

The cross product of the two vectors

Type
number
Example
// returns -2
new Vector(2, -3).cross(new Vector(-4, 2))
new Vector(-4, 2).cross(new Vector(2, -3))
// returns 2
new Vector(2, -4).cross(new Vector(-3, 2))

distance(vector) → {number}

Calculates the distance between this and the supplied vector

Parameters:
Name Type Description
vector Vector

The vector to calculate the distance from

Source:
Returns:

The distance between this and the supplied vector

Type
number

distanceX(vector) → {number}

Calculates the distance on the X axis between this and the supplied vector

Parameters:
Name Type Description
vector Vector

The vector to calculate the distance from

Source:
Returns:

The distance, along the x axis, between this and the supplied vector

Type
number

distanceY(vector) → {number}

Calculated the distance on the Y axis between this and the supplied vector

Parameters:
Name Type Description
vector Vector

The vector to calculate the distance from

Source:
Returns:

The distance, along the y axis, between this and the supplied vector

Type
number

divide(vector) → {Vector}

Divides one vector by another.

Parameters:
Name Type Description
vector Vector

The vector to divide this by

Source:
Returns:

Returns itself, modified

Type
Vector

divideNew(vector) → {Vector}

Clones the vector and divides it by the vector instead

Parameters:
Name Type Description
vector Vector

The vector to divide the clone by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

divideScalar(scalar) → {Vector}

Divides the vector by a scalar.

Parameters:
Name Type Description
scalar number

The scalar to divide both x and y by

Source:
Returns:

Returns itself, modified

Type
Vector

divideScalarNew(scalar) → {Vector}

Clones the vector and divides it by the provided scalar.

Parameters:
Name Type Description
scalar number

The scalar to divide both x and y by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

dot(vector) → {number}

Calculates the dot product between this and a supplied vector

Parameters:
Name Type Description
vector Vector

The vector object against which to calculate the dot product

Source:
Returns:

The dot product of the two vectors

Type
number
Example
// returns -14
new Vector(2, -3).dot(new Vector(-4, 2))
new Vector(-4, 2).dot(new Vector(2, -3))
new Vector(2, -4).dot(new Vector(-3, 2))

multiply(vector) → {Vector}

Multiplies one vector by another.

Parameters:
Name Type Description
vector Vector

The vector to multiply this by

Source:
Returns:

Returns itself, modified

Type
Vector

multiplyNew(vector) → {Vector}

Clones the vector and multiplies it by the vector instead

Parameters:
Name Type Description
vector Vector

The vector to multiply the clone by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

multiplyScalar(scalar) → {Vector}

Multiplies the vector by a scalar.

Parameters:
Name Type Description
scalar number

The scalar to multiply both x and y by

Source:
Returns:

Returns itself, modified

Type
Vector

multiplyScalarNew(scalar) → {Vector}

Clones the vector and multiplies it by the provided scalar.

Parameters:
Name Type Description
scalar number

The scalar to multiply both x and y by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

normalise() → {Vector}

Normalises the vector down to a length of 1 unit

Source:
Returns:

Returns itself, modified

Type
Vector

normaliseNew() → {Vector}

Clones the vector and normalises it

Source:
Returns:

Returns a clone of itself, modified

Type
Vector

reset(x, y)

Resets the vector coordinates

Parameters:
Name Type Description
x number

The x coord

y number

The y coord

Source:

resetToVector(v)

Resets the vector coordinates to another vector object

Parameters:
Name Type Description
v Vector

The vector object to use to reset the coordinates

Source:

rotate(radian) → {Vector}

Rotates a vecor by a given amount, provided in radians.

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector by

Source:
Returns:

Returns itself, modified

Type
Vector

rotateBy()

Alias of rotate

Source:

rotateByNew()

Alias of rotateNew

Source:

rotateDeg(degrees) → {Vector}

Rotates a vecor by a given amount, provided in degrees. Converts the degree value to radians and runs the rotaet method.

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector by

Source:
Returns:

Returns itself, modified

Type
Vector

rotateDegBy()

Alias of rotateDeg

Source:

rotateDegByNew()

Alias of rotateDegNew

Source:

rotateDegNew(degrees) → {Vector}

Clones the vector and rotates it by the supplied degree value

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

rotateNew(radian) → {Vector}

Clones the vector and rotates it by the supplied radian value

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector by

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

rotateTo(radian) → {Vector}

Rotates a vector to a specific angle

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector to

Source:
Returns:

Returns itself, modified

Type
Vector

rotateToDeg(degrees) → {Vector}

Rotates a vecor to a given amount, provided in degrees. Converts the degree value to radians and runs the rotateTo method.

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector to

Source:
Returns:

Returns itself, modified

Type
Vector

rotateToDegNew(degrees) → {Vector}

Clones the vector and rotates it to the supplied degree value

Parameters:
Name Type Description
degrees number

The angle, in degrees, to rotate the vector to

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

rotateToNew(radian) → {Vector}

Clones the vector and rotates it to the supplied radian value

Parameters:
Name Type Description
radian number

The angle, in radians, to rotate the vector to

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

scale()

Alias of multiplyScalar

Source:

scaleNew()

Alias of multiplyScalarNew

Source:

subtract(vector) → {Vector}

Subtracts one vector from another.

Parameters:
Name Type Description
vector Vector

The vector to subtract from this one

Source:
Returns:

Returns itself, modified

Type
Vector

subtractNew(vector) → {Vector}

Clones the vector and subtracts the vector from it instead

Parameters:
Name Type Description
vector Vector

The vector to subtract from this one

Source:
Returns:

Returns the clone of itself, modified

Type
Vector

subtractScalar(scalar) → {Vector}

Subtracts a scalar from the vector, modifying both the x and y

Parameters:
Name Type Description
scalar number

The scalar to subtract from the vector

Source:
Returns:

Returns itself, modified

Type
Vector

subtractScalarNew(scalar) → {Vector}

Clones the vector and subtracts the scalar from it instead

Parameters:
Name Type Description
scalar number

The scalar to add to the vector

Source:
Returns:

Returns the clone of itself, modified

Type
Vector