Class Vector2

The Vector2 implements a fixed-size two-dimensional vector. The class follows the 'copy-on-write' pattern, which means that every operation such as Vector2.normalized or Vector2.add returns a copy of the vector with updates values. It also implies that the values of the vector cannot be directly modified.

The concept of immutability improves sharing of objects as all types in TypeScript are assigned by identity, instead of by-value (e.g. a copy) as in other programming languages like C.

Constructors

  • Parameters

    • Optionalx: number
    • Optionaly: number

    Returns Vector2

Properties

_x: number
_y: number
one: Vector2

A Vector with all components set to one.

xAxis: Vector2

A Vector with the X component set to one and the Y set to zero.

yAxis: Vector2

A Vector with the Y component set to one and the X set to zero.

zero: Vector2

A Vector with all components set to zero.

Accessors

  • get isZero(): boolean
  • Determines all elements of the vector are zero.

    Returns boolean

  • get x(): number
  • Returns number

  • set x(value): void
  • Parameters

    • value: number

    Returns void

  • get y(): number
  • Returns number

  • set y(value): void
  • Parameters

    • value: number

    Returns void

Methods

  • Copies all properties defined by the prototype of Vector2 from other to this instance.

    Parameters

    Returns Vector2

  • Clones this instance and returns a new Vector2 with identical values.

    Returns Vector2

  • Returns a normalized direction vector from this instance to position.

    Parameters

    Returns Vector2

  • Returns the distance to position.

    Parameters

    Returns number

  • Returns a copy of the current vector divided by a factor.

    Parameters

    • factor: number

    Returns Vector2

  • Adds a scalar from this instance and returns it.

    Parameters

    • value: number

    Returns Vector2

  • Adds a scalar from this instance and returns it.

    Parameters

    • x: number
    • y: number

    Returns Vector2

  • Clears all elements of the vector to zero.

    Returns Vector2

  • Divides this vector by a factor.

    Parameters

    • factor: number

    Returns Vector2

  • Selects the maximum element of both vectors and assigns it, performed element-wise.

    Parameters

    Returns Vector2

  • Selects the minimum element of both vectors and assigns it, performed element-wise.

    Parameters

    Returns Vector2

  • Performs scalar multiplication of this vector and returns it.

    Parameters

    • factor: number

    Returns Vector2

  • Normalizes the vector.

    Returns number

  • Rounds the vector to the nearest multiple.

    Parameters

    • multiple: number

      Multiple to round to.

    Returns Vector2

    This instance.

    This is a helpful utility to quickly snap an position to a specic grid size.

  • Subtracts a scalar from this instance and returns it.

    Parameters

    • value: number

    Returns Vector2

  • Evaluates the expression and assigns it to the vector.

    Parameters

    • fnExpression: (() => number)

      The expression to evaluate.

        • (): number
        • Returns number

    Returns Vector2

    A reference to this vector

    Make sure to read up on how Vector expressions work in Typescript.

    MathEx.eval for detailed documentation, limitations and considerations.

  • Gets the maximum absolute element.

    Returns number

  • Accesses a specific component of the vector.

    Parameters

    • index: number

      Index of the component.

    Returns number

    Value of the specific component.

  • Gets the maximum element (non absolute).

    Returns number

  • Returns whether the provided vector, given a error margin, has the same vector components values as this one.

    Parameters

    • rhs: Immutable<Vector2>

      The other Vector2 to compare with.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Returns whether the given vector contains the same vector components values as this one.

    Parameters

    Returns boolean

  • Returns the length of the vector.

    Returns number

  • Returns the squared length of the vector.

    Returns number

  • Returns a copy of this vector multiplied by the factor.

    Parameters

    • factor: number

    Returns Vector2

  • Returns a normalized copy of the vector.

    Returns Vector2

  • Returns an perpendicual vector rotated clockwise or counterclockwise.

    Parameters

    • clockwise: boolean

    Returns Vector2

  • Returns a copy of the vector rounded to the nearest multiple.

    Parameters

    • multiple: number

      Multiple to round to.

    Returns Vector2

    This instance.

    This is a helpful utility to quickly snap an position to a specic grid size.

  • Sets the value of the vector component at the given index.

    Parameters

    • index: number
    • value: number

    Returns void

  • Assigns the specified values.

    Parameters

    • x: number
    • y: number

    Returns void

  • Converts this instance to an array with 2 elements.

    Returns [number, number]

  • Returns a string representation of an object.

    Returns string

  • Calculates the cross product of the two vectors.

    Parameters

    Returns number

    Cross product between the two vectors.

  • Calculates the distance between two points.

    Parameters

    Returns number

    Distance between the two points.

  • Calculates the inner product of the two vectors.

    Parameters

    Returns number

    Inner product between the two vectors.

  • Compares two 2D vectors with an error margin.

    Parameters

    • lhs: Immutable<Vector2>

      First vector to compare.

    • rhs: Immutable<Vector2>

      Second vector to compare.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Determines if both vectors are element-wise equal.

    Parameters

    Returns boolean

  • Linearly interpolates between two values.

    Parameters

    • a: Immutable<Vector2>

      Starting value to interpolate from.

    • b: Immutable<Vector2>

      Ending value to interpolate towards.

    • t: number

      Interpolation factor in specified range.

    Returns Vector2

    Interpolated vector.

  • Calculates the manhattan distance between two points.

    Parameters

    Returns number

    Manhattan distance between the two points.

  • Returns the maximum of all the vector components as a new vector.

    Parameters

    Returns Vector2

    Vector consisting of maximum components of the first and second vector.

  • Returns the minimum of all the vector components as a new vector.

    Parameters

    Returns Vector2

    Vector consisting of minimum components of the first and second vector.