Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Vector

A 2D vector on a plane.

Hierarchy

  • Vector

Index

Constructors

constructor

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

    • x: number

      X component of the Vector

    • y: number

      Y component of the Vector

    Returns Vector

Properties

x

x: number

X component of the Vector

y

y: number

Y component of the Vector

Static Down

Down: Vector = new Vector(0, 1)

A unit vector pointing down (0, 1)

Static Half

Half: Vector = new Vector(0.5, 0.5)

A (0.5, 0.5) vector

Static Left

Left: Vector = new Vector(-1, 0)

A unit vector pointing left (-1, 0)

Static One

One: Vector = new Vector(1, 1)

A (1, 1) vector

Static Right

Right: Vector = new Vector(1, 0)

A unit vector pointing right (1, 0)

Static Up

Up: Vector = new Vector(0, -1)

A unit vector pointing up (0, -1)

Static Zero

Zero: Vector = new Vector(0, 0)

A (0, 0) vector

Methods

add

addEqual

  • Adds one vector to this one modifying the original

    Parameters

    Returns Vector

average

  • Returns the average (midpoint) between the current point and the specified

    Parameters

    Returns Vector

clone

  • Creates new vector that has the same values as the previous.

    Returns Vector

cross

  • Performs a 2D cross product with scalar. 2D cross products with a scalar return a vector.

    Parameters

    • v: number

      The scalar to cross

    Returns Vector

  • Performs a 2D cross product with another vector. 2D cross products return a scalar value not a vector.

    Parameters

    • v: Vector

      The vector to cross

    Returns number

distance

  • distance(v?: Vector): number
  • The distance to another vector. If no other Vector is specified, this will return the magnitude.

    Parameters

    • Optional v: Vector

      The other vector. Leave blank to use origin vector.

    Returns number

dot

  • Performs a dot product with another vector

    Parameters

    Returns number

equals

  • equals(vector: Vector, tolerance?: number): boolean
  • Compares this point against another and tests for equality

    Parameters

    • vector: Vector
    • Default value tolerance: number = 0.001

    Returns boolean

magnitude

  • magnitude(): number
  • The magnitude (size) of the Vector

    Returns number

negate

normal

  • Returns the normal vector to this one, same as the perpendicular of length 1

    Returns Vector

normalize

perpendicular

rotate

  • Rotates the current vector around a point by a certain number of degrees in radians

    Parameters

    • angle: number
    • Optional anchor: Vector

    Returns Vector

scale

  • Scales a vector's by a factor of size

    Parameters

    • size: number

      The factor to scale the magnitude by

    Returns Vector

scaleEqual

  • scaleEqual(size: number): Vector
  • Scales this vector by a factor of size and modifies the original

    Parameters

    • size: number

    Returns Vector

setTo

  • setTo(x: number, y: number): void
  • Sets the x and y components at once

    Parameters

    • x: number
    • y: number

    Returns void

sub

  • Subtracts a vector from another, if you subract vector B.sub(A) the resulting vector points from A -> B

    Parameters

    • v: Vector

      The vector to subtract

    Returns Vector

subEqual

  • Subtracts a vector from this one modifying the original

    parallel

    v The vector to subtract

    Parameters

    Returns Vector

toAngle

  • toAngle(): number
  • Returns the angle of this vector.

    Returns number

toString

  • toString(): string
  • Returns a string repesentation of the vector.

    Returns string

Static distance

Static fromAngle

  • fromAngle(angle: number): Vector
  • Returns a vector of unit length in the direction of the specified angle in Radians.

    Parameters

    • angle: number

      The angle to generate the vector

    Returns Vector

Static isValid

  • isValid(vec: Vector): boolean
  • Checks if vector is not null, undefined, or if any of its components are NaN or Infinity.

    Parameters

    Returns boolean