Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Vecta

2D vector class.

This class is designed to be immutable. The x and y coordinates are private and so should not be changed. None of the class methods change these values - they instead all return a new Vecta with equal values.

All methods are designed with chaining in mind, unless it returns a scalar value.

Hierarchy

  • Vecta

Index

Constructors

constructor

  • new Vecta(x: number, y: number): Vecta
  • Constructor

    Parameters

    • x: number

      The vector's x coordinate

    • y: number

      The vector's y coordinate

    Returns Vecta

Properties

Private x

x: number

The vector's x coordinate

Private y

y: number

The vector's y coordinate

Methods

add

  • Adds the x dimension of the vectors together and the y dimension of the vector together and returns a new Vecta with the result.

    Parameters

    Returns Vecta

addScalar

  • addScalar(scalarX: number, scalarY?: undefined | number): Vecta
  • Adds the given scalar to both axes of the vecta and returns a new Vecta with the result

    Parameters

    • scalarX: number
    • Optional scalarY: undefined | number

    Returns Vecta

addX

  • Adds the x coordinates of the two vectors together and returns a new Vecta with the result

    Parameters

    Returns Vecta

addY

  • Adds the y coordinates of the two vectors together and returns a new Vecta with the result

    Parameters

    Returns Vecta

angle

  • angle(): number
  • The angle between the y axis, (0, 0) and this vector in degrees.

    Returns number

angleRad

  • angleRad(): number
  • The angle between the x axis, (0, 0) and this vector in radians.

    Returns number

angleTo

  • angleTo(vector: Vecta): number
  • Finds the angle between this vector and another vector in degrees.

    Parameters

    Returns number

angleToRad

  • angleToRad(vector: Vecta): number
  • Finds the angle between this vector and another vector in radians.

    Parameters

    Returns number

clone

  • Returns a new Vecta with the same x and y values.

    Returns Vecta

crossProduct

  • crossProduct(vector: Vecta): number
  • Returns the cross product of this vector with the other vector. Cross product = a.x * b.y - a.x * b.y Results in a 3D vector with only a z component. Returns the magnitude of that z component.

    Parameters

    Returns number

distanceTo

  • distanceTo(vector: Vecta): number
  • Calculates the Euclidean distance between this vector and the given vector.

    Parameters

    Returns number

div

divScalar

  • divScalar(scalarX: number, scalarY?: undefined | number): Vecta
  • Parameters

    • scalarX: number
    • Optional scalarY: undefined | number

    Returns Vecta

divX

divY

dotProduct

  • dotProduct(vector: Vecta): number
  • Returns the dot product of this vector with the other vector. Dot product = a.x * b.x + a.y * b.y

    Parameters

    Returns number

getX

  • getX(): number
  • Returns the vector's x value

    Returns number

getY

  • getY(): number
  • Returns the vector's y value

    Returns number

interpolate

  • interpolate(vector: Vecta, factorX: number, factorY: number): Vecta
  • Interpolates between two vectors. Factor indicates how progressed (between 0 and 1) the interpolation is towards the given vector. The interpolation method allows for nonlinear interpolation

    Parameters

    • vector: Vecta

      The vector to interpolate to

    • factorX: number

      Progression towards vector (0 to 1) on x axis

    • factorY: number

      Progression towards vector (0 to 1) on y axis

    Returns Vecta

invert

  • Inverts the vector (multiplies by -1) and returns the result in a new Vecta.

    Returns Vecta

invertX

invertY

limit

  • limit(max: number, factor: number): Vecta
  • Limits a vector by multiplying values outside of the range by the factor.

    Parameters

    • max: number
    • factor: number

    Returns Vecta

magnitude

  • magnitude(): number

mul

mulScalar

  • mulScalar(scalarX: number, scalarY?: undefined | number): Vecta
  • Multiplies the vector by the given scalar. If only the X scalar is given, then uses that same value for the Y scalar.

    Parameters

    • scalarX: number
    • Optional scalarY: undefined | number

    Returns Vecta

mulX

mulY

normalize

rotateBy

  • rotateBy(degrees: number): Vecta

rotateByRad

  • rotateByRad(radians: number): Vecta

round

sub

  • Subtracts the x coordinate of the given vector from this Vecta's x coordinate and does the same for the y value. Returns the result in a new Vecta.

    Parameters

    Returns Vecta

subScalar

  • subScalar(scalarX: number, scalarY?: undefined | number): Vecta
  • Parameters

    • scalarX: number
    • Optional scalarY: undefined | number

    Returns Vecta

subX

  • Subtracts the x coordinate of the given vector from this vecta's x coordinate and returns a new Vecta with the result

    Parameters

    Returns Vecta

subY

  • Subtracts the y coordinate of the given vector from this vecta's y coordinate and returns a new Vecta with the result

    Parameters

    Returns Vecta

toString

  • toString(): string

zero

Static fromArray

  • fromArray(array: [number, number]): Vecta
  • Converts an array containing two numbers in the format [x, y] into a Vecta

    Parameters

    • array: [number, number]

    Returns Vecta

Static fromObject

  • fromObject(ob: object): Vecta
  • Converts an object containing the properties x and y of type number into a Vecta

    Parameters

    • ob: object
      • x: number
      • y: number

    Returns Vecta

Static random

  • Creates a new vector at a pseudorandom point in a rectangle defined by the * given top-left and bottom-right corners. topLeft v--------------- | | | | | | | r | <-- R might be the random location | | ---------------v bottomRight

    Parameters

    • topLeft: Vecta

      The top-left corner of the square

    • bottomRight: Vecta

      The bottom-right corner of the square

    Returns Vecta

Static zero

Generated using TypeDoc