Represents a 2D vector and provides various operations for vector arithmetic and transformations.

This class is useful for geometric computations, transformations, and rendering in 2D space.

Constructors

  • Creates a new Vector instance.

    Parameters

    • point: T_Point = ...

      The initial coordinates of the vector as [x, y]. Defaults to [0, 0].

    Returns Vector

Properties

x: number

The x-coordinate of the vector.

y: number

The y-coordinate of the vector.

Accessors

Methods

  • Divides the vector by a scalar.

    Parameters

    • d: number

      The scalar to divide by.

    Returns this

    The updated vector.

  • Draws the vector on a canvas.

    Parameters

    • context: CanvasRenderingContext2D

      The 2D rendering context of the canvas.

    • origin: T_Point | Vector = ...

      The origin point from which the vector starts, defaulting to [0, 0].

    • Optionaloptions: { color?: string }

      Additional options, such as stroke color.

    Returns void

  • Multiplies the vector by a scalar.

    Parameters

    • m: number

      The scalar to multiply by.

    Returns this

    The updated vector.

  • Sets the vector's coordinates.

    Parameters

    • point: T_Point

      The new coordinates [x, y].

    Returns void

  • Sets the vector's angle while preserving its magnitude.

    Parameters

    • angle: number

      The new angle in radians.

    Returns this

    The updated vector.

  • Sets the vector's magnitude while preserving its direction.

    Parameters

    • mag: number

      The new magnitude.

    Returns this

    The updated vector.

  • Converts the vector into a point.

    Returns T_Point

    The vector as a point [x, y].

  • Computes the average of multiple vectors.

    Parameters

    • vectors: Vector[]

      An array of vectors to average.

    Returns Vector

    A new vector representing the average of all input vectors.

  • Creates a vector from a line segment.

    Parameters

    • line: T_Line

      A line segment represented as two points [start, end].

    Returns Vector

    A vector from the start to the end of the line segment.