Skip to main content

Vector

A 2D vector on a plane.

Hierarchy

Implements

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

publicstaticEQUALS_EPSILON

EQUALS_EPSILON: number = 0.001

Get or set the vector equals epsilon, by default 0.001 meaning vectors within that tolerance on x or y will be considered equal.

Accessors

publicsize

  • get size(): number
  • set size(newLength: number): void
  • The size (magnitude) of the Vector


    Returns number

  • Setting the size mutates the current vector

    @warning

    Can be used to set the size of the vector, be very careful using this, mutating vectors can cause hard to find bugs


    Parameters

    • newLength: number

    Returns void

publicx

  • get x(): number
  • set x(val: number): void
  • Get the x component of the vector


    Returns number

  • Set the x component, THIS MUTATES the current vector. It is usually better to create a new vector.

    @warning

    Be very careful setting components on shared vectors, mutating shared vectors can cause hard to find bugs


    Parameters

    • val: number

    Returns void

publicy

  • get y(): number
  • set y(val: number): void
  • Get the y component of the vector


    Returns number

  • Set the y component, THIS MUTATES the current vector. It is usually better to create a new vector.

    @warning

    Be very careful setting components on shared vectors, mutating shared vectors can cause hard to find bugs


    Parameters

    • val: number

    Returns void

publicstaticDown

  • A unit vector pointing down (0, 1)


    Returns Vector

publicstaticHalf

  • A (0.5, 0.5) vector


    Returns Vector

publicstaticLeft

  • A unit vector pointing left (-1, 0)


    Returns Vector

publicstaticOne

publicstaticRight

  • A unit vector pointing right (1, 0)


    Returns Vector

publicstaticUp

  • A unit vector pointing up (0, -1)


    Returns Vector

publicstaticZero

Methods

publicadd

  • Adds one vector to another


    Parameters

    • v: Vector

      The vector to add

    • optionaldest: Vector

      Optionally copy the result into a provided vector

    Returns Vector

publicaddEqual

  • Adds one vector to this one modifying the original

    @warning

    Be very careful using this, mutating vectors can cause hard to find bugs


    Parameters

    Returns Vector

publicaverage

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


    Parameters

    Returns Vector

publicclampMagnitude

  • clampMagnitude(magnitude: number): Vector
  • Clamps the current vector's magnitude mutating it


    Parameters

    • magnitude: number

    Returns Vector

publicclone

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


    Parameters

    Returns Vector

publiccross

  • 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

publicdistance

  • The distance to another vector. If no other Vector is specified, this will return the [[magnitude]].


    Parameters

    • optionalv: Vector

      The other vector. Leave blank to use origin vector.

    Returns number

publicdot

  • Performs a dot product with another vector


    Parameters

    Returns number

publicequals

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


    Parameters

    • vector: Vector

      The other point to compare to

    • tolerance: number = Vector.EQUALS_EPSILON

      Amount of euclidean distance off we are willing to tolerate

    Returns boolean

publicnegate

  • Negate the current vector


    Returns Vector

publicnormal

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


    Returns Vector

publicnormalize

  • Normalizes a vector to have a magnitude of 1.


    Returns Vector

publicperpendicular

  • Returns the perpendicular vector to this one


    Returns Vector

publicrotate

  • Rotates the current vector around a point by a certain angle in radians.


    Parameters

    • angle: number
    • optionalanchor: Vector

    Returns Vector

publicscale

  • Scales a vector's by a factor of size


    Parameters

    • scale: Vector
    • optionaldest: Vector

      Optionally provide a destination vector for the result

    Returns Vector

publicscaleEqual

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

    @warning

    Be very careful using this, mutating vectors can cause hard to find bugs


    Parameters

    • size: number

    Returns Vector

setTo

  • setTo(x: number, y: number): void
  • Sets the x and y components at once, THIS MUTATES the current vector. It is usually better to create a new vector.

    @warning

    Be very careful using this, mutating vectors can cause hard to find bugs


    Parameters

    • x: number
    • y: number

    Returns void

publicsquareDistance

  • squareDistance(v?: Vector): number
  • Parameters

    Returns number

publicsub

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


    Parameters

    • v: Vector

      The vector to subtract

    Returns Vector

publicsubEqual

  • Subtracts a vector from this one modifying the original

    @warning

    Be very careful using this, mutating vectors can cause hard to find bugs


    Parameters

    • v: Vector

      The vector to subtract

    Returns Vector

publictoAngle

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


    Returns number

publictoString

  • toString(fixed?: number): string
  • Returns a string representation of the vector.


    Parameters

    • optionalfixed: number

    Returns string

staticcross

publicstaticdistance

  • Calculates distance between two Vectors


    Parameters

    Returns number

publicstaticfromAngle

  • 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

publicstaticisValid

  • Checks if vector is not null, undefined, or if any of its components are NaN or Infinity.


    Parameters

    Returns boolean

publicstaticmax

publicstaticmin