Options
All
  • Public
  • Public/Protected
  • All
Menu

Simple 2D vector that provides vector math helpers.

Most methods are chainable.

const vector1 = new Vec(4, 8)
const vector2 = Vec.from(vector1)

vector1.add(vector2).multiply(3)

Hierarchy

  • Vec

Implements

Index

Constructors

constructor

  • new Vec(x?: number, y?: number): Vec
  • Initialize Vec object.

    Parameters

    • Default value x: number = 0

      X coordinate value.

    • Default value y: number = 0

      Y coordinate value.

    Returns Vec

Properties

x

x: number

X coordinate value.

y

y: number

Y coordinate value.

Methods

add

  • add(vec: Vec): this
  • Adds the x and y values from the provided Vec.

    Parameters

    • vec: Vec

      The Vec to add values from.

    Returns this

clone

copy

  • copy(vec: Vec): this
  • Copies the x and y values from the provided Vec.

    Parameters

    • vec: Vec

      The Vec to copy values from.

    Returns this

divide

  • divide(s: number): this
  • Divides the x and y values by the provided value.

    Parameters

    • s: number

      The scalar value to divide by.

    Returns this

dot

  • dot(vec: Vec): number
  • Gets the dot product of two Vecs.

    Parameters

    • vec: Vec

      The Vec to get dot product from.

    Returns number

mag

  • mag(): number

multiply

  • multiply(s: number): this
  • Multiplies the x and y values by the provided value.

    Parameters

    • s: number

      The scalar value to multiply.

    Returns this

normalize

  • normalize(): this

set

  • set(x: number, y: number): this
  • Set the coordinates of the Vec.

    Parameters

    • x: number

      X coordinate value.

    • y: number

      Y coordinate value.

    Returns this

subtract

  • subtract(vec: Vec): this
  • Subtracts the x and y values from the provided Vec.

    Parameters

    • vec: Vec

      The Vec to subtract values from.

    Returns this

toString

  • toString(): string
  • Custom string representation.

    Returns string

    String formatted as "(x, y)"

Static from

  • Creates a new Vec from the provided Vec.

    Parameters

    • v: Vec

      Vec to copy into new Vec.

    Returns Vec