simple-vector
    Preparing search index...

    Class Vector

    A simple 2D vector class

    Index

    Angle

    angle: () => number = ...

    Alias for horizontalAngle

    Kept for compatibility with Victor.js. Might change later

    Type Declaration

      • (): number
      • Gets the angle θ in the plane (in radians, -π < θ < π ) between the positive X axis and the ray from (0, 0) to the point (x, y).

        This is also the phase of the complex number x + iy.

        Caution: The direction is not the same as verticalAngle()

        Returns number

        The angle in radians

        assert.equal(0,          (new Vector(10, 0)).horizontalAngle());
        assert.equal(Math.PI/2, (new Vector(0, 10)).horizontalAngle());
        assert.equal(Math.PI, (new Vector(-10, 0)).horizontalAngle());
        assert.equal(-Math.PI/2, (new Vector(0, -10)).horizontalAngle());
    angleDeg: () => number = ...

    Alias for .horizontalAngleDeg()

    Kept for compatibility with Victor.js. Might change later

    Type Declaration

      • (): number
      • Gets the angle θ in the plane (in degrees, -180 < θ < 180 ) between the positive X axis and the ray from (0, 0) to the point (x, y)

        This is also the phase of the complex number x + iy.

        Caution: The direction is not the same as verticalAngleDeg()

        Returns number

        The angle in degrees

        assert.equal(0,    (new Vector(10, 0)).horizontalAngleDeg());
        assert.equal(90, (new Vector(0, 10)).horizontalAngleDeg());
        assert.equal(180, (new Vector(-10, 0)).horizontalAngleDeg());
        assert.equal(-90, (new Vector(0, -10)).horizontalAngleDeg());
    direction: () => number = ...

    Alias for horizontalAngle()

    Kept for compatibility with Victor.js. Might change later

    Type Declaration

      • (): number
      • Gets the angle θ in the plane (in radians, -π < θ < π ) between the positive X axis and the ray from (0, 0) to the point (x, y).

        This is also the phase of the complex number x + iy.

        Caution: The direction is not the same as verticalAngle()

        Returns number

        The angle in radians

        assert.equal(0,          (new Vector(10, 0)).horizontalAngle());
        assert.equal(Math.PI/2, (new Vector(0, 10)).horizontalAngle());
        assert.equal(Math.PI, (new Vector(-10, 0)).horizontalAngle());
        assert.equal(-Math.PI/2, (new Vector(0, -10)).horizontalAngle());
    • Gets the angle in degrees (0 < θ <= 180 between this vector and another one

      If both vectors are null the method returns NaN

      Parameters

      Returns number

      The angle between both vectors in degrees

      const vec1 = new Vector(1, 0);

      a = vec1.angleDegWith(new Vector(1, 0));
      assert.equal(a, 0)
      a = vec1.angleDegWith(new Vector(1, 1));
      assert.equal(a, 45)
      a = vec1.angleDegWith(new Vector(1, -1));
      assert.equal(a, 45)
      a = vec1.angleDegWith(new Vector(-1, 0));
      assert.equal(a, 180)
    • Gets the angle in radian (0 < θ <= π) between this vector and another one

      Parameters

      Returns number

      The angle between both vectors in radians

      const vec1 = new Vector(1, 0);

      a = vec1.angleWith(new Vector(1, 0));
      assert.equal(a, 0)
      a = vec1.angleWith(new Vector(1, 1));
      assert.equal(a, Math.PI / 4)
      a = vec1.angleWith(new Vector(1, -1));
      assert.equal(a, Math.PI / 4)
      a = vec1.angleWith(new Vector(-1, 0));
      assert.equal(a, Math.PI)
    • Gets the angle θ in the plane (in radians, -π < θ < π ) between the positive X axis and the ray from (0, 0) to the point (x, y).

      This is also the phase of the complex number x + iy.

      Caution: The direction is not the same as verticalAngle()

      Returns number

      The angle in radians

      assert.equal(0,          (new Vector(10, 0)).horizontalAngle());
      assert.equal(Math.PI/2, (new Vector(0, 10)).horizontalAngle());
      assert.equal(Math.PI, (new Vector(-10, 0)).horizontalAngle());
      assert.equal(-Math.PI/2, (new Vector(0, -10)).horizontalAngle());
    • Gets the angle θ in the plane (in degrees, -180 < θ < 180 ) between the positive X axis and the ray from (0, 0) to the point (x, y)

      This is also the phase of the complex number x + iy.

      Caution: The direction is not the same as verticalAngleDeg()

      Returns number

      The angle in degrees

      assert.equal(0,    (new Vector(10, 0)).horizontalAngleDeg());
      assert.equal(90, (new Vector(0, 10)).horizontalAngleDeg());
      assert.equal(180, (new Vector(-10, 0)).horizontalAngleDeg());
      assert.equal(-90, (new Vector(0, -10)).horizontalAngleDeg());
    • Gets the angle in degrees ( -180 < θ <= 180) between this vector and another one measured in a counterclockwise direction from this to the other one.

      This method is roughly 20% slower than this.angleDegWith()

      Parameters

      Returns number

      The angle between both vectors in degrees

      const vec1 = new Vector(1, 0);

      a = vec1.orientedAngleDegWith(new Vector(1, 0));
      assert.equal(a, 0)
      a = vec1.orientedAngleDegWith(new Vector(1, 1));
      assert.equal(a, 45)
      a = vec1.orientedAngleDegWith(new Vector(1, -1));
      assert.equal(a, -45)
      a = vec1.orientedAngleDegWith(new Vector(-1, 0));
      assert.equal(a, 180)
    • Gets the angle in radian ( -π < θ <= π) between this vector and another one measured in a counterclockwise direction from this to the other one.

      This method is roughly 20% slower than this.angleWith()

      Parameters

      Returns number

      The angle between both vectors in radians

      const vec1 = new Vector(1, 0);

      a = vec1.orientedAngleWith(new Vector(1, 0));
      assert.equal(a, 0)
      a = vec1.orientedAngleWith(new Vector(1, 1));
      assert.equal(a, Math.PI / 4)
      a = vec1.orientedAngleWith(new Vector(1, -1));
      assert.equal(a, -Math.PI / 4)
      a = vec1.orientedAngleWith(new Vector(-1, 0));
      assert.equal(a, Math.PI)
    • Computes the slope (or gradient) of the line passing by the vector.

      Note that the slope is positive for vectors in quadrants I and III, negative for vectors in quadrants II and IV, 0 for horizontal vectors and Infinity for vertical vectors

      Returns number

      The slope of the line passing by the vector

      assert.equal(0,   (new Vector(1, 0)).slope());
      assert.equal(0, (new Vector(-1, 0)).slope());
      assert.equal(1, (new Vector(1, 1)).slope());
      assert.equal(1, (new Vector(-1, -1)).slope());
      assert.equal(-1, (new Vector(-1, 1)).slope());
      assert.equal(-1, (new Vector(1, -1)).slope());
      assert.equalInfinity, (new Vector(0, 1)).slope());
      assert.equalInfinity, (new Vector(0, -1)).slope());
    • Gets the angle θ in the plane (in rads -π < θ < π) between the positive Y axis and the ray from (0, 0) to the point (x, y)

      Caution: The direction is not the same as horizontalAngle()

      Returns number

      The angle in radians

      assert.equal(0,          (new Vector(0, 10)).verticalAngle());
      assert.equal(-Math.PI/2, (new Vector(-10, 0)).verticalAngle());
      assert.equal(Math.PI/, (new Vector(0, 10)).verticalAngle());
      assert.equal(Math.PI/2, (new Vector(10, 0)).verticalAngle());
    • Gets the angle θ in the plane (in degrees -180 < θ < 180) between the positive Y axis and the ray from (0, 0) to the point (x, y)

      Caution: The direction is not the same as horizontalAngleDeg()

      Returns number

      The angle in degrees

      assert.equal(0,   (new Vector(0, 10)).verticalAngleDeg());
      assert.equal(-90, (new Vector(-10, 0)).verticalAngleDeg());
      assert.equal(180, (new Vector(0, 10)).verticalAngleDeg());
      assert.equal(90, (new Vector(10, 0)).verticalAngleDeg());

    Arithmetic operations

    • Adds another vector to this one

      Parameters

      • vec: Vector

        The other vector you want to add to this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(10, 10);
      const vec2 = new Vector(20, 30);

      vec1.add(vec2);
      assert.equal(vec1.x, 30)
      assert.equal(vec1.y, 40)
    • Adds the given scalar to both vector axes

      Parameters

      • scalar: number

        The scalar to add

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 20);

      vec.addScalar(2);
      assert.equal(vec.x, 12)
      assert.equal(vec.y, 22)
    • Adds the given scalar to the X axis

      Parameters

      • scalar: number

        The scalar to add

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 20);

      vec.addScalarX(2);
      assert.equal(vec.x, 12)
      assert.equal(vec.y, 20)
    • Adds the given scalar to the Y axis

      Parameters

      • scalar: number

        The scalar to add

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 20);

      vec.addScalarY(2);
      assert.equal(vec.x, 10)
      assert.equal(vec.y, 22)
    • Adds the X axis of another vector to this one

      Parameters

      • vec: Vector

        The other vector you want to add to this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(10, 10);
      const vec2 = new Vector(20, 30);

      vec1.addX(vec2);
      assert.equal(vec1.x, 30)
      assert.equal(vec1.y, 10)
    • Adds the Y axis of another vector to this one

      Parameters

      • vec: Vector

        The other vector you want to add to this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(10, 10);
      const vec2 = new Vector(20, 30);

      vec1.addY(vec2);
      assert.equal(vec1.x, 10)
      assert.equal(vec1.y, 40)
    • Divides both axes of this vector by those of another one

      Parameters

      • vec: Vector

        The vector to divide by

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If any axis of the argument vector is 0

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(2, 2);

      vec1.divide(vec2);
      assert.equal(vec1.x, 50)
      assert.equal(vec1.y, 25)
    • Divides both vector axes by the given scalar

      Parameters

      • scalar: number

        The scalar to divide by

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If the argument scalar is 0

      const vec = new Vector(100, 50);

      vec.divideScalar(2);
      assert.equal(vec.x, 50)
      assert.equal(vec.y, 25)
    • Divides the X axis by the given scalar

      Parameters

      • scalar: number

        The scalar to divide by

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If x axis of argument vector is 0

      const vec = new Vector(100, 50);

      vec.divideScalarX(2);
      assert.equal(vec.x, 50)
      assert.equal(vec.y, 50)
    • Divides the Y axis by the given scalar

      Parameters

      • scalar: number

        The scalar to divide by

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If x axis of argument vector is 0

      const vec = new Vector(100, 50);

      vec.divideScalarY(2);
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 25)
    • Divides the X axis of this vector by the X axis of another one

      Parameters

      • vec: Vector

        The other vector you want divide by

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If the X axis of the argument vector is 0

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(2, 0);

      vec1.divideX(vec2);
      assert.equal(vec1.x, 50)
      assert.equal(vec1.y, 50)
    • Divides the Y axis of this vector by the Y axis of another one

      Parameters

      • vec: Vector

        The other vector you want divide by

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If the Y axis of the argument vector is 0

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(0, 2);

      vec1.divideY(vec2);
      assert.equal(vec1.x, 100)
      assert.equal(vec1.y, 25)
    • Multiplies both axes of this vector by those of another one

      Parameters

      • vec: Vector

        The vector to multiply by

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(2, 2);

      vec1.multiply(vec2);
      assert.equal(vec1.x, 200)
      assert.equal(vec1.y, 100)
    • Multiplies both vector axes by the given scalar

      Parameters

      • scalar: number

        The scalar to multiply by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.multiplyScalar(2);
      assert.equal(vec.x, 200)
      assert.equal(vec.y, 100)
    • Multiplies the X axis by the given scalar

      Parameters

      • scalar: number

        The scalar to multiply by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.multiplyScalarX(2);
      assert.equal(vec.x, 200)
      assert.equal(vec.y, 50)
    • Multiplies the Y axis by the given scalar

      Parameters

      • scalar: number

        The scalar to multiply by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.multiplyScalarY(2);
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 100)
    • Multiplies the X axis of this vector by the X axis of another one

      Parameters

      • vec: Vector

        The other vector you want multiply by

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(2, 0);

      vec1.multiplyX(vec2);
      assert.equal(vec1.x, 200)
      assert.equal(vec1.y, 50)
    • Multiplies the Y axis of this vector by the Y axis of another one

      Parameters

      • vec: Vector

        The other vector you want multiply by

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(0, 2);

      vec1.multiplyY(vec2);
      assert.equal(vec1.x, 100)
      assert.equal(vec1.y, 100)
    • Subtracts another vector from this one

      Parameters

      • vec: Vector

        The other vector you want to subtract from this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(30, 30);
      const vec2 = new Vector(10, 20);

      vec1.subtract(vec2);
      assert.equal(vec1.x, 20)
      assert.equal(vec1.y, 10)
    • Subtracts the given scalar from both axes

      Parameters

      • scalar: number

        The scalar to subtract

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 20);

      vec.subtractScalar(2);
      assert.equal(vec.x, 8)
      assert.equal(vec.y, 18)
    • Subtracts the given scalar from the X axis

      Parameters

      • scalar: number

        The scalar to subtract

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 20);

      vec.subtractScalarX(2);
      assert.equal(vec.x, 8)
      assert.equal(vec.y, 20)
    • Subtracts the given scalar from the Y axis

      Parameters

      • scalar: number

        The scalar to subtract

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 20);

      vec.subtractScalarY(2);
      assert.equal(vec.x, 10)
      assert.equal(vec.y, 18)
    • Subtracts the X axis of another vector from this one

      Parameters

      • vec: Vector

        The other vector you want to subtract from this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(30, 30);
      const vec2 = new Vector(10, 20);

      vec1.subtractX(vec2);
      assert.equal(vec1.x, 20)
      assert.equal(vec1.y, 30)
    • Subtracts the Y axis of another vector from this one

      Parameters

      • vec: Vector

        The other vector you want to subtract from this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(30, 30);
      const vec2 = new Vector(10, 20);

      vec1.subtractY(vec2);
      assert.equal(vec1.x, 30)
      assert.equal(vec1.y, 10)

    Clamp

    • Enforce the value of both axes to be between a max and min value. See clampX and clampY for details.

      This is equivalent to calling .clampX(min, max).clampY(min, max)

      Parameters

      • min: number
      • max: number

      Returns Vector

      this for chaining capabilities

      TypeError If the min value is defined and not of the same type as the max value

      InvalidNumberError If one of the number parameter is invalid

      const vec = new Vector(100, 100);

      vec.clampAxes(50)
      assert.equal(vec.x, 50)
      assert.equal(vec.y, 50)
    • Use the axes of two vectors to enforce the bounds of the axes of this vector.

      Parameters

      • minVector: Vector

        The vector defining the minimum value for the axes

      • maxVector: Vector

        The vector defining the maximum value for the axes

      Returns Vector

    • Use a number to enforce the max value of the axes of this vector.

      Parameters

      • max: number

        The maximum value for the axes

      Returns Vector

    • Use the axes of a vector to enforce the max value of the axes of this vector.

      Parameters

      • maxVector: Vector

        The vector defining the maximum value for the axes

      Returns Vector

    • Resize the vector to enforce its magnitude to be between and max and a min value.

      If only one value is provided, it is used as the max bound and no min bound is enforced.

      If two values are provided the method will sort them to use the largest one as the max bound and the smallest one as the min bound. These values must be of the same type (either two Vector or two number)

      This preserves the angle of the vector.

      Parameters

      • min: number

        The minimum value for the magnitude

      • max: number

        The maximum value for the magnitude

      Returns Vector

      this for chaining capabilities

      RangeError if one of the parameter is a negative number

      TypeError If the second parameter is defined and not of the same type as the first parameter

      InvalidNumberError If one of the number parameter is invalid

      const vec = new Vector(100, 100);
      const angle = vec.horizontalAngle();

      vec.clampMag(50)
      assert.equal(vec.magnitude(), 50)
      assert.equal(vec.horizontalAngle(), angle)
    • Use the magnitudes of two vectors to enforce the bounds of the magnitude of this vector.

      Parameters

      • minVector: Vector

        The vector defining the minimum value for magnitude

      • maxVector: Vector

        The vector defining the maximum value for magnitude

      Returns Vector

    • Use a number to enforce the max value of the magnitude of this vector.

      Parameters

      • max: number

        The maximum value for the magnitude

      Returns Vector

    • Use the magnitude of a vector to enforce the max value of the magnitude of this vector.

      Parameters

      • maxVector: Vector

        The vector defining the maximum value for the magnitude of this vector

      Returns Vector

    • Enforce the value of the X axis to be between a max and min value.

      If only one value is provided, it is used as the max bound and no min bound is enforced.

      If two values are provided the method will sort them to use the largest one as the max bound and the smallest one as the min bound. These values must be of the same type (either two Vector or two number)

      Parameters

      • min: number

        The minimum value for the X axis

      • max: number

        The maximum value for the X axis

      Returns Vector

      this for chaining capabilities

      TypeError If the second parameter is defined and not of the same type as the first parameter

      InvalidNumberError If one of the number parameter is invalid

      const vec = new Vector(100, 100);

      vec.clampX(0, 50)
      assert.equal(vec.x, 50)
      assert.equal(vec.y, 100)
    • Use the X axes of two vectors to enforce the bounds of the X axis of this vector.

      Parameters

      • minVector: Vector

        The vector defining the minimum value for the X axis

      • maxVector: Vector

        The vector defining the maximum value for the X axis

      Returns Vector

    • Use a number to enforce the max value of the X axis of this vector.

      Parameters

      • max: number

        The maximum value for the X axis

      Returns Vector

    • Use the X axis of a vector to enforce the max value of the X axis of this vector.

      Parameters

      • maxVector: Vector

        The vector defining the maximum value for the X axis

      Returns Vector

    • Enforce the value of the Y axis to be between a max and min value.

      If only one value is provided, it is used as the max bound and no min bound is enforced.

      If two values are provided the method will sort them to use the largest one as the max bound and the smallest one as the min bound. These values must be of the same type (either two Vector or two number)

      Parameters

      • min: number

        The minimum value for the Y axis

      • max: number

        The maximum value for the Y axis

      Returns Vector

      this for chaining capabilities

      TypeError If the second parameter is defined and not of the same type as the first parameter

      InvalidNumberError If one of the number parameter is invalid

      const vec = new Vector(100, 100);

      vec.clampY(0, 50)
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 50)
    • Use the Y axes of two vectors to enforce the bounds of the Y axis of this vector.

      Parameters

      • minVector: Vector

        The vector defining the minimum value for the Y axis

      • maxVector: Vector

        The vector defining the maximum value for the Y axis

      Returns Vector

    • Use a number to enforce the max value of the Y axis of this vector.

      Parameters

      • max: number

        The maximum value for the Y axis

      Returns Vector

    • Use the Y axis of a vector to enforce the max value of the Y axis of this vector.

      Parameters

      • maxVector: Vector

        The vector defining the maximum value for the Y axis

      Returns Vector

    Comparison

    • Returns true if this vector axes values are close to the axes of the other vector, within the margin of the epsilon parameter.

      This is useful when dealing with floating point errors. You probably want to use this method rather than isEqualTo.

      Parameters

      • vec: Vector

        The second vector

      • epsilon: number = 1e-6

        (default 1e-6) The size of the margin, you want to keep this value small

      Returns boolean

      true if the vector axes are close to the other one's within the provided margin

      const vec1 = new Vector(100, 50);

      const vec2 = new Vector(100.000000001, 49.99999999998);
      assert.false(vec1.isEqualTo(vec2);
      assert.true(vec1.isCloseTo(vec2);

      // Useful when manipulating vectors
      const vec1Rot = vec1.clone().rotateByDeg(360)
      // => {x: 100.00000000000001, y: 49.99999999999998}
      assert.true(vec1.isCloseTo(vec1Rot));
      assert.false(vec1.isEqualTo(vec1Rot));
    • Returns true if this vector axes values are exactly the same as another.

      Be cautious with floating point errors. If vectors are close but not exactly the same this method returns false, in most cases you probably want to use isCloseTo instead.

      Parameters

      Returns boolean

      true if the vector axes are equal to the other one's

      const vec1 = new Vector(100, 50);

      const vec2 = new Vector(100, 50);
      assert.true(vec1.isEqualTo(vec2);

      const vec3 = new Vector(0, 0);
      assert.false(vec1.isEqualTo(vec3);

      // Beware when manipulating vectors
      const vec1Rot = vec1.clone().rotateByDeg(360)
      // => {x: 100.00000000000001, y: 49.99999999999998}
      assert.false(vec1.isEqualTo(vec1Rot));
    • Returns true if this vector is parallel to another one.

      This method has a small tolerance so that vectors which seems almost parallel are considered parallel. This is to avoid rounding errors inherent to floating point programming. (For example v.isParallelTo(v.rotateBy(2 * Math.PI)) would be likely to be false without this small tolerance).

      Parameters

      Returns boolean

      true if the vector is parallel to the other one

      const vec1 = new Vector(1, 1);
      const vec2 = new Vector(-2, -2);
      assert.true(vec1.isParallelTo(vec2))
    • Returns true if this vector is perpendicular to another one.

      This method has a small tolerance so that vectors which seems almost perpendicular are considered perpendicular. This is to avoid rounding errors inherent to floating point programming. (For example v.isParallelTo(v.isPerpendicularTo(Math.PI / 2)) would be likely to be false without this small tolerance).

      Parameters

      Returns boolean

      true if the vector is perpendicular to the other one

      const vec1 = new Vector(1, 0);
      const vec2 = new Vector(0, -2);
      assert.true(vec1.isPerpendicularTo(vec2))
    • Returns true if vector is exactly (0, 0)

      Returns boolean

      true if the vector magnitude is 0, false otherwise

      const vec = new Vector(100, 50);
      assert.false(vec.isZero())

      vec.zero();
      assert.true(vec.isZero())

    Constructor

    • A simple 2D vector class

      Parameters

      • x: number = 0

        Value of the X axis

      • y: number = 0

        Value of the Y axis

      Returns Vector

      TypeError if either of the arguments are not valid numbers

      const vec = new Vector(100, 50);
      
    • Creates a clone of this vector with the same properties. This is particularly useful when you need to apply the method which modifies the vector but keep the original vector untouched

      Returns Vector

      The instance of the newly created vector

      const vec1 = new Vector(10, 10);
      const vec2 = vec1.clone();

      assert.equal(vec2.x, vec1.x)
      assert.equal(vec2.y, vec1.y)
      assert.notEqual(vec1, vec2)
    • Returns an array representation of the vector

      Returns number[]

      An array representation of the vector

      const vec = new Vector(10, 20);

      vec.toArray();
      // [10, 20]
    • Returns an object representation of the vector

      Returns VectorLike

      An object representation of the vector

      const vec = new Vector(10, 20);

      vec.toObject();
      // { x: 10, y: 20 }
    • Returns a polar representation of the vector.

      Returns Polar

      A polar representation of the vector

      const vec = new Vector(1, 1);

      vec.toPolar();
      // { theta: Math.PI/ 4, r: Math.sqrt(2) }
    • Returns a string representation of the vector

      Returns string

      A string representing the vector's axes

      const vec = new Vector(10, 20);
      const s = vec.toString();
      assert.equal(s, "x:10, y:20")
    • Creates a new instance from an array using first two items as x and y. (The array length must be at least 2)

      Parameters

      • arr: number[]

        Array with the x and y values at index 0 and 1 respectively

      Returns Vector

      A new Vector instance

      TypeError if either of the array length is less than 2

      TypeError if either of the first 2 array members are not valid numbers

      const vec = Vector.fromArray([42, 21]);

      assert.equal(vec.x, 42)
      assert.equal(vec.y, 21)
    • Creates a new instance from an object resembling a vector (Object must have a x: number and a y: number property)

      Parameters

      Returns Vector

      A new Vector instance

      TypeError if either of the x or y objects property are not valid numbers

      const vec1 = Vector.fromObject({ x: 42, y: 21 });
      const vec2 = new Vector(42, 21);

      assert.true(vec1.isEqualTo(vec2))
    • Creates a new instance from an angle in radians and a magnitude (The angle is from the positive x axis)

      Parameters

      • radians: number

        Object with properties x and/or y

      • magnitude: number

        Object with properties x and/or y

      Returns Vector

      A new Vector instance

      const vec = Vector.fromPolar(Math.PI / 2, 100);

      assert.equal(vec.x, 0)
      assert.equal(vec.y, 100)
    • Creates a new instance of magnitude 1 with a random angle

      Returns Vector

      A new Vector instance

      const vec = Vector.randomUnitVector();

      assert.equal(vec.magnitude(), 1)

    Copy

    • Copies the X and Y axes of another vector to this one

      Parameters

      • vec: Vector

        The other vector you want to copy to this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(10, 10);
      const vec2 = new Vector(20, 20);

      vec1.copy(vec2);
      assert.equal(vec1.x, 20)
      assert.equal(vec1.y, 20)
    • Copies the X axis of another vector to this one

      Parameters

      • vec: Vector

        The other vector you want to copy to this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(10, 10);
      const vec2 = new Vector(20, 20);

      vec1.copyX(vec2);
      assert.equal(vec1.x, 20)
      assert.equal(vec1.y, 10)
    • Copies the Y axis of another vector to this one

      Parameters

      • vec: Vector

        The other vector you want to copy to this one

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(10, 10);
      const vec2 = new Vector(20, 20);

      vec1.copyY(vec2);
      assert.equal(vec1.x, 10)
      assert.equal(vec1.y, 20)

    Distance

    • Same as distanceX but always returns an absolute number

      Parameters

      Returns number

      The absolute distance between the X axes

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(200, 60);

      const d = vec1.absDistanceX(vec2);
      assert.equal(d, 100)
    • Same as distanceY but always returns an absolute number

      Parameters

      Returns number

      The absolute distance between the Y axes

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(200, 60);

      const d = vec1.absDistanceY(vec2);
      assert.equal(d, 10)
    • Calculates the euclidean distance between this vector and another one

      Parameters

      Returns number

      The euclidean distance between the vectors

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(200, 60);

      const d = vec1.distance(vec2);
      assert.equal(d, 100.4987562112089)
    • Calculates the Chebyshev distance between this vector and another. This is the greatest absolute difference between the respective coordinates of the vectors.

      Parameters

      Returns number

      The Chebyshev distance between the vectors

      const vec1 = new Vector(1, 1);
      const vec2 = new Vector(3, 4);

      const d = vec1.distanceChebyshev(vec2);
      assert.equal(d, 3)
    • Calculates the Manhattan distance between this vector and another. This is the sum of the absolute difference between the respective coordinates of the vectors.

      Parameters

      Returns number

      The Manhattan distance between the vectors

      const vec1 = new Vector(1, 1);
      const vec2 = new Vector(3, 4);

      const d = vec1.distanceManhattan(vec2);
      assert.equal(d, 5)
    • Calculates the squared euclidean distance between this vector and another.

      This is faster than distance because we don't use Math.sqrt.

      Parameters

      Returns number

      The squared euclidean distance between the vectors

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(200, 60);

      const d = vec1.distanceSq(vec2);
      assert.equal(d, 10100)
    • Calculates the distance from the X axis of another vector to the X axis of this one

      Parameters

      Returns number

      The distance between the X axes

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(200, 60);

      const d = vec1.distanceX(vec2);
      assert.equal(d, -100)
    • Calculates the distance from the Y axis of another vector to the Y axis of this one

      Parameters

      Returns number

      The distance between the Y axes

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(200, 60);

      const d = vec1.distanceY(vec2);
      assert.equal(d, -10)

    Interpolation

    • Performs a linear blend / interpolation towards another vector

      The mixFactor parameter is the amount to interpolate between this vector and the other vector. 0.0 keeps the axes equal to this vector's, 0.5 is halfway between, and 1.0 sets the axes equal to the other vector's.

      Parameters

      • vec: Vector

        The other vector

      • mixFactor: number = 0.5

        The blend amount (optional, default: 0.5)

      Returns Vector

      this for chaining capabilities

      RangeError if mixFactor is not between 0 and 1

      const vec1 = new Vector(100, 100);
      const vec2 = new Vector(200, 200);

      vec1.mix(vec2, 0.5);
      assert.equal(vec1.x, 150)
      assert.equal(vec1.y, 150)
    • Performs a linear blend / interpolation of the X axis towards another vector.

      The mixFactor parameter is the amount to interpolate between this vector and the other vector. 0.0 keeps the X axis equal to this vector's, 0.5 is halfway between, and 1.0 sets the X axis equal to the other vector's.

      Parameters

      • vec: Vector

        The other vector

      • mixFactor: number = 0.5

        The blend amount [0, 1] (optional, default: 0.5)

      Returns Vector

      this for chaining capabilities

      RangeError if mixFactor is not between 0 and 1

      const vec1 = new Vector(100, 100);
      const vec2 = new Vector(200, 200);

      vec1.mixX(vec2, 0.5);
      assert.equal(vec1.x, 150)
      assert.equal(vec1.y, 100)
    • Performs a linear blend / interpolation of the Y axis towards another vector

      The mixFactor parameter is the amount to interpolate between this vector and the other vector. 0.0 keeps the Y axis equal to this vector's, 0.5 is halfway between, and 1.0 sets the Y axis equal to the other vector's.

      Parameters

      • vec: Vector

        The other vector

      • mixFactor: number = 0.5

        The blend amount (optional, default: 0.5)

      Returns Vector

      this for chaining capabilities

      RangeError if mixFactor is not between 0 and 1

      const vec1 = new Vector(100, 100);
      const vec2 = new Vector(200, 200);

      vec1.mixY(vec2, 0.5);
      assert.equal(vec1.x, 100)
      assert.equal(vec1.y, 150)
    • Projects a vector onto the direction of another vector

      Parameters

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If the vector to project onto is zero. This prevents NaN results.

      const vec1 = new Vector(100, 0);
      const vec2 = new Vector(100, 100);

      vec1.projectOnto(vec2);
      assert.equal(vec1.x, 50)
      assert.equal(vec1.y, 50)

    Inversion

    • Multiplies both axes by -1

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.invert();
      assert.equal(vec.x, -100)
      assert.equal(vec.y, -50)
    • Multiplies the X axis by -1

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.invertX();
      assert.equal(vec.x, -100)
      assert.equal(vec.y, 50)
    • Multiplies the Y axis by -1

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.invertY();
      assert.equal(vec.x, 100)
      assert.equal(vec.y, -50)

    Limit

    • If the absolute value of the axes is greater than max, multiplies the axis by factor

      Parameters

      • max: number

        The maximum value for both X and Y axes

      • factor: number

        Factor by which the axes are to be multiplied by

      Returns Vector

      this for chaining capabilities

      const vec1 = new Vector(100, 50);

      vec1.limit(80, 0.9);
      assert.equal(vec1.x, 90)
      assert.equal(vec1.y, 50)

      const vec2 = new Vector(100, 100);

      vec2.limit(80, 0.9);
      assert.equal(vec2.x, 90)
      assert.equal(vec2.y, 90)
    • If the absolute value of the X axis is greater than max, multiplies its value by factor

      Parameters

      • max: number

        The maximum value for the X axis

      • factor: number

        Factor by which the axis is to be multiplied by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 100);

      vec.limitX(80, 0.9);
      assert.equal(vec.x, 90)
      assert.equal(vec.y, 100)
    • If the absolute value of the Y axis is greater than max, multiplies its value by factor

      Parameters

      • max: number

        The maximum value for the Y axes

      • factor: number

        Factor by which the axis is to be multiplied by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 100);

      vec.limitY(80, 0.9);
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 90)

    Magnitude

    magnitude: () => number = ...

    Alias for mag

    Type Declaration

      • (): number
      • Calculates the magnitude (or length) of the vector

        Returns number

        The magnitude of the vector

        const vec = new Vector(100, 50);

        const m = vec.mag()
        assert.equal(m, 111.80339887498948)

    The magnitude of the vector

    norm: () => Vector = ...

    Alias for Vector.normalize

    Type Declaration

      • (): Vector
      • Normalize the vector (Keep the direction but change the magnitude to 1)

        Returns Vector

        this for chaining capabilities

        DivisionByZeroError If the vector is zero

        const vec = new Vector(10, 0);

        vec.normalize();
        assert.equal(vec.x, 1)
        assert.equal(vec.y, 0)
    • Calculates the magnitude (or length) of the vector

      Returns number

      The magnitude of the vector

      const vec = new Vector(100, 50);

      const m = vec.mag()
      assert.equal(m, 111.80339887498948)
    • Calculates the squared length (or squared magnitude) of the vector

      This is faster than mag because we don't use Math.sqrt.

      Returns number

      The squared magnitude of the vector

      const vec = new Vector(100, 50);

      const m = vec.magSq()
      assert.equal(m, 12500)
    • Sets the vector axes to zero (0,0)

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 10);

      vec.zero();
      assert.equal(vec1.x, 0)
      assert.equal(vec1.y, 0)

    Other

    x: number = 0

    The X axis value

    y: number = 0

    The Y axis value

    Precision

    • Fix both axes to a certain precision using Number.toFixed() on each axis

      Parameters

      • precision: number = 8

        (default: 8)

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100.2345, 50.9876);

      vec.fixPrecision(2);
      assert.equal(vec.x, 100.23)
      assert.equal(vec.y, 50.99)
    • Rounds both axes to an integer value using Math.round()

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100.2, 50.9);

      vec.unfloat();
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 51)

    Product

    • Calculates the cross product of this vector and another.

      Note that the resulting scalar value is due to considering the Z axes as 0. See stackoverflow.com

      Parameters

      Returns number

      The cross product of this vector and the other one

      const vec1 = new Vector(100, 100);
      const vec2 = new Vector(500, 200);

      const cp = vec1.cross(vec2);
      assert.equal(dp, -30000)
    • Calculates the dot product of this vector and another

      Parameters

      Returns number

      The dot product of this vector and the other one

      const vec1 = new Vector(100, 50);
      const vec2 = new Vector(200, 60);

      const dp = vec1.dot(vec2);
      assert.equal(dp, 23000)

    Randomization

    • Randomizes both axes of the vector with a value between 2 vectors

      Parameters

      • topLeft: Vector

        First bounding vector

      • bottomRight: Vector

        Second bounding vector

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      const topLeft = new Vector(50, 60)
      const bottomRight = new Vector(70, 80)

      vec.randomize(topLeft, bottomRight);
      assert.equal(vec.x, 67.17186656753522)
      assert.equal(vec.y, 73.933542831865296)
    • Randomly choses one axis and randomizes it with a value between the corresponding axis of 2 other vectors

      Parameters

      • topLeft: Vector

        First bounding vector

      • bottomRight: Vector

        Second bounding vector

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      const topLeft = new Vector(50, 60)
      const bottomRight = new Vector(70, 80)

      vec.randomizeAny(topLeft, bottomRight);
      assert.equal(vec.x, 67.17186656753522)
      assert.equal(vec.y, 50)
    • Randomizes the X axis with a value between the X axes of 2 others vectors

      Parameters

      • topLeft: Vector

        First bounding vector

      • bottomRight: Vector

        Second bounding vector

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      const topLeft = new Vector(50, 60)
      const bottomRight = new Vector(70, 80)

      vec.randomizeX(topLeft, bottomRight);
      assert.equal(vec.x, 67.17186656753522)
      assert.equal(vec.y, 50)
    • Randomizes the Y axis with a value between the Y axes of 2 others vectors

      Parameters

      • topLeft: Vector

        First bounding vector

      • bottomRight: Vector

        Second bounding vector

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      const topLeft = new Vector(50, 60)
      const bottomRight = new Vector(70, 80)

      vec.randomizeY(topLeft, bottomRight);
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 73.933542831865296)

    Reflection

    • Reflects the vector about a line. The orientation of the line is described by the parameter surfaceNormal a normal vector that points away from the shape.

      Parameters

      • surfaceNormal: Vector

        The normal vector of the line to reflect about

      Returns Vector

      this for chaining capabilities

      // normal vector
      const n = new Vector(0, 1);
      // vector to reflect
      const v = new Vector(4, 6);

      // reflect v about n
      v.reflect(n)

      assert.equal(vec.x, 4)
      assert.equal(vec.y, -6)

    Resize

    norm: () => Vector = ...

    Alias for Vector.normalize

    Type Declaration

      • (): Vector
      • Normalize the vector (Keep the direction but change the magnitude to 1)

        Returns Vector

        this for chaining capabilities

        DivisionByZeroError If the vector is zero

        const vec = new Vector(10, 0);

        vec.normalize();
        assert.equal(vec.x, 1)
        assert.equal(vec.y, 0)
    • Normalize the vector (Keep the direction but change the magnitude to 1)

      Returns Vector

      this for chaining capabilities

      DivisionByZeroError If the vector is zero

      const vec = new Vector(10, 0);

      vec.normalize();
      assert.equal(vec.x, 1)
      assert.equal(vec.y, 0)
    • Resizes the vector so that its direction is not changed but its magnitude is set to the new value. If the magnitude argument is negative, the angle of the resulting vector is rotated by 180 degrees.

      Parameters

      • magnitude: number

        The new value of the vector's magnitude

      Returns Vector

      this for chaining capabilities

      TypeError if the magnitude argument is null or undefined

      DivisionByZeroError If the vector is zero

      const vec1 = new Vector(0, 1);
      vec1.resize(10);
      assert.equal(vec1.horizontalAngle(), 90);
      assert.equal(vec1.magnitude(), 10);

      vec1.resize(-2);
      assert.equal(vec1.horizontalAngle(), -90);
      assert.equal(vec1.magnitude(), 2);

    Rotation

    • Rotate the vector counter-clockwise by an angle in radians

      Parameters

      • angle: number

        The angle in radians to rotate the vector by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 0);
      assert.equal(0, vec.horizontalAngleDeg())

      vec.rotateBy(Math.PI)
      assert.equal(180, vec.horizontalAngleDeg())

      vec.rotateBy(Math.PI / 2)
      assert.equal(-90, vec.horizontalAngleDeg()) // π + π/2 => -π/2
    • Rotate the vector counter-clockwise by an angle in degrees

      Parameters

      • angle: number

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 0);
      assert.equal(0, vec.horizontalAngleDeg())

      vec.rotateByDeg(180)
      assert.equal(180, vec.horizontalAngleDeg())

      vec.rotateByDeg(90)
      assert.equal(-90, vec.horizontalAngleDeg()) // 180 + 90 => -90
    • Rotate the vector to an angle in radians using the positive X axis as origin, move counter-clockwise

      Parameters

      • rotation: number

        The angle in radians to rotate the vector to

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 0);

      vec.rotateTo(Math.PI);
      assert.equal(vec1.x, -10)
      assert.equal(vec1.y, 0)

      v.rotateTo(-Math.PI/2);
      assert.equal(v.x, 0);
      assert.equal(v.y, -10);
    • Rotate the vector to an angle in degrees using the positive X axis as origin, move counter-clockwise

      Parameters

      • rotation: number

        The angle in degrees to rotate the vector to

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 0);

      vec.rotateToDeg(180);
      assert.equal(vec1.x, -10)
      assert.equal(vec1.y, 0)

      v.rotateTo(-90);
      assert.equal(v.x, 0);
      assert.equal(v.y, -10);
    • Rotate towards another vector limiting the rotation to a max angle θ in radians (θ > 0)

      Parameters

      • vec: Vector

        The vector steering the current vector

      • maxAngle: number

        The max angle in radians to rotate the vector by

      Returns Vector

      this for chaining capabilities

      RangeError if maxAngle equal or less than zero

      const vec1 = new Vector(10, 0);
      const vec2 = new Vector(0, 10);

      vec1.rotateTowards(vec2, Math.PI / 4);
      assert.equal(vec1.horizontalAngle(), Math.PI / 4)
    • Rotate towards another vector limiting the rotation to a max angle θ in degrees (θ > 0)

      Parameters

      • vec: Vector

        The vector steering the current vector

      • maxAngle: number

        The max angle in degrees to rotate the vector by

      Returns Vector

      this for chaining capabilities

      RangeError if maxAngle equal or less than zero

      const vec1 = new Vector(10, 0);
      const vec2 = new Vector(0, 10);

      vec1.rotateTowardsDeg(vec2, 2);
      assert.equal(vec1.horizontalAngleDeg(), 2)