Class Vector2d

a generic 2D Vector Object

Constructors

Properties

x: number
y: number

Methods

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

    Parameters

    • v: Vector2d

      The vector to which the distance is calculated.

    Returns number

    The Euclidean distance between this vector and the given vector.

    let v1 = new Vector2d(3, 4);
    let v2 = new Vector2d(6, 8);
    v1.distance(v2); // returns 5
  • Checks if this vector is equal to another vector or a pair of coordinates.

    Parameters

    • Rest...args: [number, number] | [XYPoint | Vector2d]

      Either two numbers representing x and y coordinates or a single Vector2d object.

    Returns boolean

    True if the coordinates are equal, false otherwise.

    let v1 = new Vector2d(3, 4);
    let v2 = new Vector2d(3, 4);
    v1.equals(v2); // returns true
    let v = new Vector2d(3, 4);
    v.equals(3, 4); // returns true
  • Linearly interpolate between this vector and the given one.

    Parameters

    • v: Vector2d

      other vector

    • alpha: number

      distance along the line (alpha = 0 will be this vector, and alpha = 1 will be the given one).

    Returns Vector2d

    Reference to this object for method chaining

  • interpolate the position of this vector towards the given one by the given maximum step.

    Parameters

    • target: Vector2d

      vector to rotate towards

    • step: number

      the maximum step per iteration (Negative values will push the vector away from the target)

    Returns Vector2d

    Reference to this object for method chaining

  • Rotate this vector (counter-clockwise) by the specified angle (in radians).

    Parameters

    • angle: number

      The angle to rotate (in radians)

    • Optionalv: XYPoint

      an optional point to rotate around

    Returns Vector2d

    Reference to this object for method chaining

  • Multiply this vector values by the given scalar

    Parameters

    • x: number

      x scale value

    • Optionaly: number = x

      y scale value, if not passed, it uses the x value

    Returns Vector2d

    Reference to this object for method chaining

  • convert the object to a string representation

    Returns `x:${number},y:${number}`

    stringified representation of this vector