Class Vector

Represents a 2D vector with x and y components.

Constructors

Properties

x: number = 0

The x-coordinate of the vector.

y: number = 0

The y-coordinate of the vector.

Methods

  • Calculates the angle of the vector in radians. The angle is measured counterclockwise from the positive x-axis.

    Returns number

    The angle of the vector in radians.

  • Clamps the length of the vector to a specified value. If the length of the vector is greater than the specified value, the vector is scaled down. If the length of the vector is less than or equal to the specified value, the vector remains unchanged.

    Parameters

    • length: number = 1

      The maximum length to clamp the vector to. Default value is 1.

    Returns Vector

    A new vector with the clamped length.

  • Calculates the cross product of this vector and the given vector.

    Parameters

    • v: Vector

      The vector to calculate the cross product with.

    Returns number

    The cross product of the two vectors.

  • Returns the direction of the vector.

    Returns
        | 0
        | 1
        | 2
        | 3

    The direction of the vector. - 0: Up - 1: Right - 2: Down - 3: Left

  • Calculates the Euclidean distance between this vector and the given vector.

    Parameters

    • v: Vector

      The vector to calculate the distance to.

    Returns number

    The Euclidean distance between the two vectors.

  • Calculates the squared distance between this vector and the given vector.

    Parameters

    • v: Vector

      The vector to calculate the distance to.

    Returns number

    The squared distance between this vector and the given vector.

  • Divides the vector by either another vector or a number. If the argument is a number, it divides each component of the vector by that number. If the argument is a vector, it divides the corresponding components of the vector by the components of the argument vector.

    Parameters

    • d: number | Vector

      The vector or number to divide by.

    Returns Vector

    A new vector resulting from the division.

  • Calculates the dot product between this vector and the given vector.

    Parameters

    • v: Vector

      The vector to calculate the dot product with.

    Returns number

    The dot product of the two vectors.

  • Checks if the current vector is within the range of the specified size.

    Parameters

    • size: Vector

      The size vector to compare against.

    Returns boolean

    true if the current vector is within the range, false otherwise.

  • Performs a linear interpolation between two vectors.

    Parameters

    • v: Vector

      The vector to interpolate with.

    • percent: number

      The interpolation factor, ranging from 0 to 1.

    Returns Vector

    The interpolated vector.

  • Normalizes the vector to a specified length. If the vector's length is zero, a new vector with the specified length is returned.

    Parameters

    • length: number = 1

      The desired length of the normalized vector. Default is 1.

    Returns Vector

    A new Vector instance representing the normalized vector.

  • Rotates the vector by the specified angle.

    Parameters

    • angle: number

      The angle (in radians) by which to rotate the vector.

    Returns Vector

    A new Vector representing the rotated vector.

  • Sets the angle and length of the vector.

    Parameters

    • angle: number = 0

      The angle in radians.

    • length: number = 1

      The length of the vector.

    Returns Vector

    The modified vector.