@statox/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.angleWith(new Vector(1, 0));
      assert.equal(a, 0)
      a = vec1.angleWith(new Vector(1, 1));
      assert.equal(a, 45)
      a = vec1.angleWith(new Vector(1, -1));
      assert.equal(a, 45)
      a = vec1.angleWith(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 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, 45)
      a = vec1.orientedAngleWith(new Vector(1, -1));
      assert.equal(a, -45)
      a = vec1.orientedAngleWith(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)
    • 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 degrees

      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

      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)

      DivisionByZeroError If any axis of the argument vector is 0

    • Divides both vector axes by the given scalar

      Parameters

      • scalar: number

        The scalar to divide by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.divideScalar(2);
      assert.equal(vec.x, 50)
      assert.equal(vec.y, 25)

      DivisionByZeroError If the argument scalar is 0

    • Divides the X axis by the given scalar

      Parameters

      • scalar: number

        The scalar to divide by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.divideScalarX(2);
      assert.equal(vec.x, 50)
      assert.equal(vec.y, 50)

      DivisionByZeroError If x axis of argument vector is 0

    • Divides the Y axis by the given scalar

      Parameters

      • scalar: number

        The scalar to divide by

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

      vec.divideScalarY(2);
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 25)

      DivisionByZeroError If x axis of argument vector is 0

    • 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

      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)

      DivisionByZeroError If the X axis of the argument vector is 0

    • 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

      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)

      DivisionByZeroError If the Y axis of the argument vector is 0

    • 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)

    Comparison

    • Returns true if this vector axes values are the same as another

      Parameters

      Returns boolean

      true if the vector magnitude is 0, false otherwise

      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);
    • Returns true if this vector is parallel to another one.

      This method has a small tolerance so that vector 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 vector which seems almost perpendicular are considered perpendicular. This is to avoid rounding errors inherent to floating point programming. (For example v.isParallelTo(v.rotateBy(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 (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

      const vec = new Vector(100, 50);
      
    • Creates a clone of this vector

      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)
    • 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 lenght 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

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

      vec.toString();
      // 'x:42, y:21'
    • Creates a new instance from an object ressembling a vector (Object must have a x: number and a y: number property)

      Parameters

      Returns Vector

      A new Vector instance

      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(vec1.x, 0)
      assert.equal(vec1.y, 100)

    Copy

    • Copies vector axes 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.distanceX(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.distanceY(vec2);
      assert.equal(d, 10)
    • Calculates the euclidean distance between this vector and another

      Parameters

      Returns number

      The euclidian 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 squared euclidean distance between this vector and another

      Parameters

      Returns number

      The squared euclidian 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 between the X axis of this vector the X axis of another

      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 between the X axis of this vector the X axis of another

      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

      Parameters

      • vec: Vector

        The other vector

      • amount: number = 0.5

        The blend amount (optional, default: 0.5)

      Returns Vector

      this for chaining capabilities

      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)

      RangeError if amount is not between 0 and 1

    • Performs a linear blend / interpolation of the X axis towards another vector.

      Parameters

      • vec: Vector

        The other vector

      • amount: number = 0.5

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

      Returns Vector

      this for chaining capabilities

      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)

      RangeError if amount is not between 0 and 1

    • Performs a linear blend / interpolation of the Y axis towards another vector

      Parameters

      • vec: Vector

        The other vector

      • amount: number = 0.5

        The blend amount (optional, default: 0.5)

      Returns Vector

      this for chaining capabilities

      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)

      RangeError if amount is not between 0 and 1

    Inversion

    • Inverts both axes

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

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

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

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

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 50);

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

    Magnitude

    magnitude: () => number = ...

    Alias for .length()

    Type declaration

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

        Returns number

        The magnitude of the vector

        const vec = new Vector(100, 50);

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

    The magnitude of the vector

    norm: () => Vector = ...

    Alias for Vector.normalize

    Type declaration

      • (): Vector
      • Normalize the vector. (Keep direction but reduce length to 1)

        Returns Vector

        this for chaining capabilities

        const vec = new Vector(10, 0);

        vec.normalize();
        assert.equal(vec.x, 1)
        assert.equal(vec.y, 0)

    this for chaining capabilities

    • Resize the vector to clamp the magnitude to the max value. Also clamp to a minimum value if a second argument is specified.

      This preserves the angle of the vector.

      Parameters

      • max: number

        The maximum value for the Y axis

      • Optionalmin: number

        (optional) The maximum value for the Y axis

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 100);

      vec.clampY(50)
      assert.equal(vec.y, 50)
      assert.equal(vec.y, 100)

      RangeError RangeError if the min value is defined and larger than the max value

    • Clamp the value of the x axis to a maximum value. Also clamp to a minimum value if a second argument is specified

      Parameters

      • max: number

        The maximum value for the X axis

      • Optionalmin: number

        (optional) The maximum value for the X axis

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 100);

      vec.clampX(50)
      assert.equal(vec.x, 50)
      assert.equal(vec.y, 100)

      RangeError RangeError if the min value is defined and larger than the max value

    • Clamp the value of the y axis to a maximum value. Also clamp to a minimum value if a second argument is specified

      Parameters

      • max: number

        The maximum value for the Y axis

      • Optionalmin: number

        (optional) The maximum value for the Y axis

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(100, 100);

      vec.clampY(50)
      assert.equal(vec.y, 50)
      assert.equal(vec.y, 100)

      RangeError RangeError if the min value is defined and larger than the max value

    • Calculates the length or magnitude of the vector

      Returns number

      The magnitude of the vector

      const vec = new Vector(100, 50);

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

      Returns number

      The squared magnitude of the vector

      const vec = new Vector(100, 50);

      const m = vec.lengthSq()
      assert.equal(m, 12500)
    • 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.limit(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.limit(80, 0.9);
      assert.equal(vec.x, 100)
      assert.equal(vec.y, 90)
    • Normalize the vector. (Keep direction but reduce length to 1)

      Returns Vector

      this for chaining capabilities

      const vec = new Vector(10, 0);

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

      Parameters

      • magnitude: number

        The new value of the vector's magnitude

      Returns Vector

      this for chaining capabilities

      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);
    • 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 & Projection

    • Calculates the cross product of this vector and another.

      Note that the resulting scalar value is due to considering the z axes as 0 https://stackoverflow.com/a/243977

      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)
    • Projects a vector onto another vector, setting itself to the result.

      Parameters

      Returns Vector

      this for chaining capabilities

      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)

    Randomization

    • Randomizes both vector axes with a value between 2 vectors

      Parameters

      • topLeft: Vector

        First bounding vector

      • bottomRight: Vector

        Second bouding 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 axes of 2 other vectors

      Parameters

      • topLeft: Vector

        First bounding vector

      • bottomRight: Vector

        Second bouding 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 bouding 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 bouding 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)

    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.rotate(Math.PI)
      assert.equal(180, vec.horizontalAngleDeg())

      vec.rotate(Math.PI / 2)
      // π + π / 2 => -π/2
      assert.equal(-90, vec.horizontalAngleDeg())
    • 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.rotateDeg(180)
      assert.equal(180, vec.horizontalAngleDeg())

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

      Parameters

      • rotation: number

      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 positif X axis as origin, move counter-clockwise

      Parameters

      • rotation: number

      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

      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)

      RangeError if maxAngle equal or less than zero

    • 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

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

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

      RangeError if maxAngle equal or less than zero