Class Vector2

2D vector.

Constructors

  • Parameters

    • Optionalx: number
    • Optionaly: number

    Returns Vector2

Properties

x: number
0
y: number
0
width: number
height: number
isVector2: true

Methods

  • Sets value of this vector.

    Parameters

    • x: number
    • y: number

    Returns this

  • Sets the x and y values of this vector both equal to scalar.

    Parameters

    • scalar: number

    Returns this

  • Sets X component of this vector.

    Parameters

    • x: number

    Returns this

  • Sets Y component of this vector.

    Parameters

    • y: number

    Returns this

  • Sets a component of this vector.

    Parameters

    • index: number
    • value: number

    Returns this

  • Gets a component of this vector.

    Parameters

    • index: number

    Returns number

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

    Returns this

  • Copies value of v to this vector.

    Parameters

    Returns this

  • Adds v to this vector.

    Parameters

    Returns this

  • Adds the scalar value s to this vector's x and y values.

    Parameters

    • s: number

    Returns this

  • Sets this vector to a + b.

    Parameters

    Returns this

  • Adds the multiple of v and s to this vector.

    Parameters

    Returns this

  • Subtracts v from this vector.

    Parameters

    Returns this

  • Subtracts s from this vector's x and y components.

    Parameters

    • s: number

    Returns this

  • Sets this vector to a - b.

    Parameters

    Returns this

  • Multiplies this vector by v.

    Parameters

    Returns this

  • Multiplies this vector by scalar s.

    Parameters

    • scalar: number

    Returns this

  • Divides this vector by v.

    Parameters

    Returns this

  • Divides this vector by scalar s.
    Set vector to ( 0, 0 ) if s == 0.

    Parameters

    • s: number

    Returns this

  • Multiplies this vector (with an implicit 1 as the 3rd component) by m.

    Parameters

    Returns this

  • If this vector's x or y value is greater than v's x or y value, replace that value with the corresponding min value.

    Parameters

    Returns this

  • If this vector's x or y value is less than v's x or y value, replace that value with the corresponding max value.

    Parameters

    Returns this

  • If this vector's x or y value is greater than the max vector's x or y value, it is replaced by the corresponding value.
    If this vector's x or y value is less than the min vector's x or y value, it is replaced by the corresponding value.

    Parameters

    Returns this

  • If this vector's x or y values are greater than the max value, they are replaced by the max value.
    If this vector's x or y values are less than the min value, they are replaced by the min value.

    Parameters

    • min: number

      the minimum value the components will be clamped to.

    • max: number

      the maximum value the components will be clamped to.

    Returns this

  • If this vector's length is greater than the max value, it is replaced by the max value.
    If this vector's length is less than the min value, it is replaced by the min value.

    Parameters

    • min: number

      the minimum value the length will be clamped to.

    • max: number

      the maximum value the length will be clamped to.

    Returns this

  • The components of the vector are rounded down to the nearest integer value.

    Returns this

  • The x and y components of the vector are rounded up to the nearest integer value.

    Returns this

  • The components of the vector are rounded to the nearest integer value.

    Returns this

  • The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value.

    Returns this

  • Inverts this vector.

    Returns this

  • Computes dot product of this vector and v.

    Parameters

    Returns number

  • Computes cross product of this vector and v.

    Parameters

    Returns number

  • Computes squared length of this vector.

    Returns number

  • Computes length of this vector.

    Returns number

  • Computes the Manhattan length of this vector.

    see Taxicab Geometry

    Returns number

  • Normalizes this vector.

    Returns this

  • computes the angle in radians with respect to the positive x-axis

    Returns number

  • Returns the angle between this vector and vector v in radians.

    Parameters

    Returns number

  • Computes distance of this vector to v.

    Parameters

    Returns number

  • Computes squared distance of this vector to v.

    Parameters

    Returns number

  • Computes the Manhattan length (distance) from this vector to the given vector v

    see Taxicab Geometry

    Parameters

    Returns number

  • Normalizes this vector and multiplies it by l.

    Parameters

    • length: number

    Returns this

  • Linearly interpolates between this vector and v, where alpha is the distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v.

    Parameters

    • v: Vector2Like

      vector to interpolate towards.

    • alpha: number

      interpolation factor in the closed interval [0, 1].

    Returns this

  • Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2.

    Parameters

    • v1: Vector2Like

      the starting vector.

    • v2: Vector2Like

      vector to interpolate towards.

    • alpha: number

      interpolation factor in the closed interval [0, 1].

    Returns this

  • Checks for strict equality of this vector and v.

    Parameters

    Returns boolean

  • Sets this vector's x and y value from the provided array or array-like.

    Parameters

    • array: number[] | ArrayLike<number>

      the source array or array-like.

    • Optionaloffset: number

      (optional) offset into the array. Default is 0.

    Returns this

  • Returns an array [x, y], or copies x and y into the provided array.

    Parameters

    • Optionalarray: number[]

      (optional) array to store the vector to. If this is not provided, a new array will be created.

    • Optionaloffset: number

      (optional) optional offset into the array.

    Returns number[]

    The created or provided array.

  • Parameters

    Returns Vector2Tuple

  • Copies x and y into the provided array-like.

    Parameters

    • array: ArrayLike<number>

      array-like to store the vector to.

    • Optionaloffset: number

      (optional) optional offset into the array.

    Returns ArrayLike<number>

    The provided array-like.

  • Sets this vector's x and y values from the attribute.

    Parameters

    • attribute: BufferAttribute

      the source attribute.

    • index: number

      index in the attribute.

    Returns this

  • Rotates the vector around center by angle radians.

    Parameters

    • center: Vector2Like

      the point around which to rotate.

    • angle: number

      the angle to rotate, in radians.

    Returns this

  • Sets this vector's x and y from Math.random

    Returns this

  • Iterating through a Vector2 instance will yield its components (x, y) in the corresponding order.

    Returns Iterator<number, any, undefined>