API Docs for: v0.1.0
Show:

Vector Class

Extends Object
Defined in: src\math\Vector.js:3

A 2d Vector implementation stolen directly from mrdoob's THREE.js Vector2d

Constructor

Vector

(
  • x
  • y
)

Parameters:

  • x Number

    The x component of the vector

  • y Number

    The y component of the vector

Methods

add

(
  • vector
)
Vector chainable

Adds a vector to this one

Parameters:

  • vector Vector

    The vector to add to this one

Returns:

Vector:

Returns itself.

addScalar

(
  • scalar
)
Vector chainable

Adds a scalar value to the x and y components of this vector

Parameters:

  • scalar Number

    The scalar value to add

Returns:

Vector:

Returns itself.

addVectors

(
  • vector1
  • vector2
)
Vector chainable

Adds two vectors to each other and stores the result in this vector

Parameters:

Returns:

Vector:

Returns itself.

ceil

() Vector chainable

Ceils the vector components

Returns:

Vector:

Returns itself.

clone

() Vector

Creates a new instance of Vector, with the same components as this vector

Returns:

Vector:

Returns a new Vector with the same values

copy

(
  • vector
)
Vector chainable

Copies the passed vector's components to this vector

Parameters:

  • vector Vector

    The vector to copy the values from

Returns:

Vector:

Returns itself.

distanceTo

(
  • vector
)
Number

Calculates the distance to the passed vector

Parameters:

  • vector Vector

    The vector to check distance to

Returns:

Number:

The distance

distanceToSquared

(
  • vector
)
Number

Calculates the square distance to the passed vector

Parameters:

  • vector Vector

    The vector to check distance to

Returns:

Number:

The square distance

divideScalar

(
  • scalar
)
Vector chainable

Divides the x and y components of this vector by a scalar value

Parameters:

  • scalar Number

    The value to divide by

Returns:

Vector:

Returns itself.

dot

(
  • vector
)
Number

Performs the dot product between this vector and the passed one and returns the result

Parameters:

Returns:

Number:

Returns the dot product

equals

(
  • vector
)
Vector chainable

Checks if this vector is equal to another

Parameters:

  • vector Vector

    The vector to compare with

Returns:

Vector:

Returns itself.

floor

() Vector chainable

Floors the vector components

Returns:

Vector:

Returns itself.

getComponent

(
  • index
)
Number

Gets a component value of the vector

Parameters:

  • index Number

    The index of the component to set (0 = x, 1 = y)

Returns:

Number:

Returns the component value

length

() Number

Calculates the length of the vector

Returns:

Number:

Returns the length of the vector

lengthSq

() Number

Calculates the square length of the vector

Returns:

Number:

Returns the square length of the vector

lerp

(
  • vector
  • alpha
)
Vector chainable

Performs a linear interpolation between this vector and the passed vector

Parameters:

  • vector Vector

    The vector to interpolate with

  • alpha Number

    The amount to interpolate [0-1] or extrapolate (1-]

Returns:

Vector:

Returns itself.

max

(
  • vector
)
Vector chainable

Sets this vector components to the maximum value when compared to the passed vector's components

Parameters:

  • vector Vector

    The vector to compare to

Returns:

Vector:

Returns itself.

max

(
  • min
  • max
)
Vector chainable

Clamps the vectors components to be between min and max

Parameters:

  • min Vector

    The minimum value a component can be

  • max Vector

    The maximum value a component can be

Returns:

Vector:

Returns itself.

min

(
  • vector
)
Vector chainable

Sets this vector components to the minimum value when compared to the passed vector's components

Parameters:

  • vector Vector

    The vector to compare to

Returns:

Vector:

Returns itself.

multiplyScalar

(
  • scalar
)
Vector chainable

Multiplies the x and y components of this vector by a scalar value

Parameters:

  • scalar Number

    The value to multiply by

Returns:

Vector:

Returns itself.

negate

() Vector chainable

Negates this vector (multiplies by -1)

Returns:

Vector:

Returns itself.

normalize

() Vector

Normalizes this vector (divides by its length)

Returns:

Vector:

Returns the normalized vector

rotate

(
  • angle
  • anchor
)
Vector chainable

Rotates the vector by an arbitrary angle around an arbitrary point in space

Parameters:

  • angle Number

    The angle in radians to rotate by

  • anchor Vector

    The anchor point to rotate around

Returns:

Vector:

Returns itself.

set

(
  • x
  • y
)
Vector chainable

Sets the value of the vector

Parameters:

  • x Number

    The x component of the vector

  • y Number

    The y component of the vector

Returns:

Vector:

Returns itself.

setComponent

(
  • index
  • value
)
Vector chainable

Sets a component value of the vector

Parameters:

  • index Number

    The index of the component to set (0 = x, 1 = y)

  • value Number

    The value to set the component to

Returns:

Vector:

Returns itself.

setLength

(
  • length
)
Vector chainable

Sets the length of the vector

Parameters:

  • length Number

    The length to set this vector to

Returns:

Vector:

Returns itself.

setX

(
  • x
)
Vector chainable

Sets the X value of the vector

Parameters:

  • x Number

    The x component of the vector

Returns:

Vector:

Returns itself.

setY

(
  • y
)
Vector chainable

Sets the Y value of the vector

Parameters:

  • y Number

    The y component of the vector

Returns:

Vector:

Returns itself.

sub

(
  • vector
)
Vector chainable

Subtracts a vector from this one

Parameters:

  • vector Vector

    The vector to subtract from this one

Returns:

Vector:

Returns itself.

subVectors

(
  • vector1
  • vector2
)
Vector chainable

Subtracts two vectors from each other and stores the result in this vector

Parameters:

Returns:

Vector:

Returns itself.

toArray

() Vector

Returns an array with the components of this vector as the elements

Returns:

Vector:

Returns an array of [x,y] form

Properties

ZERO

Vector final static

A vector that is always 0,0