Vector2

Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y).

Core Class

Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y).

Implements

  • Vector

Constructors

  • Parameters

    • _x: number
    • _y: number

    Returns Vector2

Properties

x: number

the x value of this vector.

y: number

the y value of this vector.

isVector2: boolean

Check the type whether it belongs to Vector2. This value should not be changed by user.

Accessors

  • get width(): number

    Same to x.

    Returns number

  • set width(w: number): void

    Parameters

    • w: number

    Returns void

  • get height(): number

    Same to y.

    Returns number

  • set height(h: number): void

    Parameters

    • h: number

    Returns void

Methods

  • Sets the x and y components of this vector.

    Parameters

    • x: number
    • y: number

    Returns Vector2

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

    Parameters

    • scalar: number

    Returns Vector2

  • Replaces this vector's x value with Float| x.

    Parameters

    • x: number

    Returns Vector2

  • Replaces this vector's y value with Float| y.

    Parameters

    • y: number

    Returns Vector2

  • If index equals 0 set x to Float| value. If index equals 1 set y to Float| value.

    Parameters

    • index: number
    • value: number

    Returns Vector2

  • If index equals 0 returns the x value. If index equals 1 returns the y value.

    Parameters

    • index: number

      0 or 1.

    Returns number

  • Returns a new Vector2 with the same x and y values as this one.

    Returns Vector2

  • Returns Readonly<
    Pick<
    ReadonlyMarked,
    | "_readonly_mark"
    | "cloneReadonly"
    | "clone"
    | "equals"
    | "getSerializeData"
    | "x"
    | "y"
    | "getNumberCount"
    | "toArray"
    | "width"
    | "height"
    | "isVector2"
    | "dot"
    | "cross"
    | "lengthSq"
    | "length"
    | "angle"
    | "manhattanLength"
    | "distanceTo"
    | "distanceToSquared"
    | "manhattanDistanceTo"
    | "intoSize",
    >,
    >

  • Copies the values of the passed Vector2's x and y properties to this Vector2.

    Parameters

    Returns Vector2

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

    Parameters

    • s: number

    Returns Vector2

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

    Parameters

    • s: number

    Returns Vector2

  • Multiplies this vector by scalar s.

    Parameters

    • scalar: number

    Returns Vector2

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

    Parameters

    • scalar: number

    Returns Vector2

  • 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 Vector2

  • 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 Vector2

  • 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

    • min: Vector2

      the minimum x and y values.

    • max: Vector2

      the maximum x and y values in the desired range.

    Returns Vector2

  • 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

    • minVal: number
    • maxVal: number

    Returns Vector2

  • 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 Vector2

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

    Returns Vector2

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

    Returns Vector2

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

    Returns Vector2

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

    Returns Vector2

  • Inverts this vector - i.e. sets x = -x and y = -y.

    Returns Vector2

  • Calculates the dot product of this vector and v.

    Parameters

    Returns number

  • Calculates the cross product of this vector and v.

    Parameters

    Returns number

    that a 'cross-product' in 2D is not well-defined. This function computes a geometric cross-product often used in 2D graphics

  • Computes the square of the Euclidean length (straight-line length) from (0, 0) to (x, y). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.

    Returns number

  • Computes the Euclidean length (straight-line length) from (0, 0) to (x, y).

    Returns number

  • Computes the Manhattan length of this vector.

    Returns number

  • Converts this vector to a unit vector. that is, sets it equal to a vector with the same direction as this one, but length| length 1.

    Returns Vector2

  • Computes the angle in radians of this vector with respect to the positive x-axis.

    Returns number

  • Computes the distance from this vector to v.

    Parameters

    Returns number

  • Computes the squared distance from this vector to v. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly more efficient to calculate.

    Parameters

    Returns number

  • Sets this vector to a vector with given length the same direction as this one.

    Parameters

    • length: number

    Returns Vector2

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

    Parameters

    • v: Vector2

      Vector2 to interpolate towards.

    • alpha: number

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

    Returns Vector2

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

    Parameters

    • v1: Vector2

      the starting Vector2.

    • v2: Vector2

      Vector2 to interpolate towards.

    • alpha: number

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

    Returns Vector2

  • Checks for strict equality of this vector and v.

    Parameters

    Returns boolean

  • Sets this vector's x value to be array[ offset ] and y value to be array[ offset + 1 ].

    Parameters

    • array: ArrayLike<number>

      the source array.

    • offset: number

      (optional) offset into the array.

    Returns Vector2

  • There are 2 elements in this vector.

    Returns number

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

    Parameters

    • array: number[]

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

    • offset: number

      (optional) optional offset into the array.

    Returns number[]

  • Parameters

    • attribute: { getX: (number: number) => number; getY: (number: number) => number }

      the source attribute.

    • index: number

      index in the attribute.

    • offset: number

    Returns Vector2

    please use BufferAttribute.getVector2 Sets this vector's .x| x and .y| y values from the BufferAttribute| attribute.

  • Rotates this vector around center by Float| angle radians.

    Parameters

    • center: Vector2

      the point around which to rotate.

    • angle: number

      the angle to rotate, in radians.

    Returns Vector2

  • Returns Size