Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Vector2D

The 2D vector class.

Hierarchy

  • Vector2D

Index

Constructors

constructor

  • new Vector2D(x: number, y: number): Vector2D
  • Parameters

    • x: number

      The 'x' component of the vector

    • y: number

      The 'y' component of the vector

    Returns Vector2D

Properties

Private x

x: number

The 'x' component of the vector

Private y

y: number

The 'y' component of the vector

Get Vector Attribute Methods

getAngle

  • getAngle(): number
  • Get the angle in radians of this vector (relative to the origin)

    Returns number

getLength

  • getLength(): number
  • Get the length of this vector

    Returns number

getLengthSquared

  • getLengthSquared(): number
  • Get the square of the length of this vector

    Returns number

getX

  • getX(): number
  • Get the 'x' component of this vector

    Returns number

getY

  • getY(): number
  • Get the 'y' component of this vector

    Returns number

Immutable Operations Methods

add

  • Add the components of another vector to this vector and return the result as a new vector (without modifying either input vector)

    Parameters

    Returns Vector2D

normalVector

  • Get a vector that is normal (counter-clockwise perpendicular) to this vector (with the same length as this vector)

    Returns Vector2D

scale

  • Scale the components of this vector by a scalar and return the result as a new vector (without modifying this vector)

    Parameters

    • scalar: number

    Returns Vector2D

subtract

  • Subtract the components of another vector from this vector and return the result as a new vector (without modifying either input vector)

    Parameters

    Returns Vector2D

unitVector

Mutable Operations Methods

mutableAdd

  • Add the components of another vector to this vector and store the result in the original vector

    Parameters

    Returns void

mutableNormalVector

  • mutableNormalVector(): void
  • Calculate a normal vector (counter-clockwise perpendicular) to this vector (with the same length as this vector) and store the result in the original vector

    Returns void

mutableScale

  • mutableScale(scalar: number): void
  • Scale the components of this vector by a scalar and store the result in the original vector

    Parameters

    • scalar: number

    Returns void

mutableSubtract

  • Subtract the components of another vector from this vector and store the result in the original vector

    Parameters

    Returns void

mutableUnitVector

  • mutableUnitVector(): void
  • Calculate the unit vector to this vector and store the result in the original vector

    Returns void

Other Methods

getAngleBetween

  • Get the angle of another vector from this vector

    Parameters

    Returns number

getCrossProduct

  • Get the cross product of this vector and another vector. Assuming the cross product of two 2D vectors is, U x V = Ux * Vy - Uy * Vx.

    Parameters

    Returns number

getDistanceFrom

  • Get the scalar distance of this vector from another vector

    Parameters

    Returns number

getDistanceFromSquared

  • getDistanceFromSquared(v: Vector2D): number
  • Get the square of the scalar distance of this vector from another vector

    Parameters

    Returns number

getDotProduct

  • Get the dot product of this vector and another vector

    Parameters

    Returns number

Set Vector Methods

setAngle

  • setAngle(angle: number): void
  • Set the angle (in radians) of this vector without modifying the length

    Parameters

    • angle: number

      The angle (in radians) to set this vector to

    Returns void

setFromComponents

  • setFromComponents(x: number, y: number): void
  • Set the 'x' and 'y' components of this vector to the specified values

    Parameters

    • x: number

      The value that the 'x' component will be set to

    • y: number

      The value that the 'y' component will be set to

    Returns void

setFromVector

  • Set the components of this vector based on the components of another vector

    Parameters

    Returns void

setLength

  • setLength(length: number): void
  • Set the length of this vector without modifying the angle

    Parameters

    • length: number

      The length to set this vector to

    Returns void

setToZero

  • setToZero(): void
  • Set the components of this vector to zero

    Returns void

setX

  • setX(x: number): void
  • Set the 'x' component of the vector

    Parameters

    • x: number

      The value that the 'x' component will be set to

    Returns void

setY

  • setY(y: number): void
  • Set the 'y' component of the vector

    Parameters

    • y: number

      The value that the 'y' component will be set to

    Returns void

Utility Methods

clone

isEqual

  • Check if this vector has the same 'x' and 'y' components as another vector

    Parameters

    Returns boolean

isZero

  • isZero(): boolean
  • Checks whether this vector is zero (ie the 'x' and 'y' components are equal to zero)

    Returns boolean

toJSON

  • toJSON(): { x: number; y: number }
  • Returns a JSON representation of this vector in the form {"x": x-component, "y": y-component}

    Returns { x: number; y: number }

    • x: number
    • y: number

toString

  • toString(): string
  • Returns a string representation of this vector in the form "(x-component, y-component)"

    Returns string

Generated using TypeDoc