Represents a 2D vector with x and y components.

Constructors

  • Creates an instance of Vec2.

    Parameters

    • Optionalx: number

      The x coordinate (default is 0).

    • Optionaly: number

      The y coordinate (default is 0).

    Returns Vec2

Properties

x: number
y: number

Methods

  • Calculates the angle of the vector relative to the x-axis.

    Returns number

    The angle of the vector.

  • Inverts the direction of the vector.

    Returns this

    The current instance with updated values.

  • Calculates the length of the vector.

    Returns number

    The length of the vector.

  • Normalizes the vector to have a length of 1.

    Returns this

    The current instance with updated values.

  • Rotates the vector 90 degrees counterclockwise.

    Returns this

    The current instance with updated values.

  • Multiplies the vector by a scalar.

    Parameters

    • f: number

      The scalar value to multiply by.

    Returns this

    The current instance with updated values.

  • Adds two vectors together.

    Parameters

    • p0: Vec2

      The first vector.

    • p1: Vec2

      The second vector.

    Returns Vec2

    A new vector representing the sum of p0 and p1.

  • Computes the angle between two vectors.

    Parameters

    • p0: Vec2

      The starting vector.

    • p1: Vec2

      The ending vector.

    Returns number

    The angle between the two vectors in radians.

  • Converts an array of coordinate pairs into an array of Vec2 instances.

    Parameters

    • array: number[][]

      An array of [x, y] coordinate pairs.

    Returns Vec2[]

    An array of Vec2 instances.

  • Finds the midpoint between two vectors.

    Parameters

    • p0: Vec2

      The first vector.

    • p1: Vec2

      The second vector.

    Returns Vec2

    A new vector representing the midpoint between p0 and p1.

  • Subtracts one vector from another.

    Parameters

    • p1: Vec2

      The vector to subtract from.

    • p0: Vec2

      The vector to subtract.

    Returns Vec2

    A new vector representing the difference between p1 and p0.