src/math/Vector.js
/*
    A quick JavaScript 2D vector implementation.
    Based on simplifications of the vector functions in P5.js
    Useful for quick animations in JS using acceleration & velocity.
    MIT License
    Ivaylo Getov, 2015
    www.ivaylogetov.com
    Quick reference:
    let v = new Vector(some_x_val, some_y_val);
        v.set(new_x_val, new_y_val);
        v.add(x_val, y_val) OR v.add(v2);
        v.div(divisor);
        v.mult(scalar);
        v.normalize();
        v.setMag(new_length);
        v.dot(v2) OR v.dot(x_val,y_val);
        v.dist(v2);
        v.limit(max_length);
        v.rotateRads(angle_in_radians);
        v.rotateDegs(angle_in_degrees);
        v.headingDegs();
        v.headingDeg();
        v.angleBetweenRads(v2) OR v.angleBetweenRads(some_x,some_y);
        v.angleBetweenDegs(v2) OR v.angleBetweenDegs(some_x,some_y)
        v.lerp(v2, lerp_amount) OR v.lerp(some_x, some_y, lerp_amount);
        v.equals(v2) OR v.equals(some_x,some_y);
        let v2 = v.copy();
*/
A quick JavaScript 2D vector implementation.
Based on simplifications of the vector functions in P5.js
Useful for quick animations in JS using acceleration & velocity.
A quick JavaScript 2D vector implementation . Based on simplifications of the vector functions in P5 . js Useful for quick animations in JS using acceleration & velocity .
function Vector (x, y) {
  this.x = x; 
  this.y = y;
  this.set = function (x, y) {
    this.x = x; 
    this.y = y;
  };
  this.magSq = function () {
    let x = this.x, y = this.y;
    return x * x + y * y;
  };
  this.mag = function () {
    return Math.sqrt (this.magSq ());
  };
  this.add = function (x, y) {
    if (x instanceof Vector) {
      this.x += x.x; 
      this.y += x.y;
      return this;
    }
    this.x += x;
    this.y += y;
    return this;
  };
  this.sub = function (x, y) {
    if (x instanceof Vector) {
      this.x -= x.x;
      this.y -= x.y;
      return this;
    }
    this.x -= x;
    this.y -= y;
    return this;
  };
  this.div = function (n) {
    this.x /= n; 
    this.y /= n;
    return this;
  };
  this.mult = function (n) {
    this.x *= n; 
    this.y *= n;
    return this;
  };
  this.normalize = function () {
    return this.div (this.mag ()); 
  };
  this.setMag = function (n) {
    return this.normalize ().mult (n); 
  };
  this.dot = function (x, y) {
    if (x instanceof Vector) {
      return this.dot (x.x, x.y);
    }
    return this.x * (x || 0) + this.y * (y || 0);
  };
  this.dist = function (v) {
    let d = v.copy ().sub (this); 
    return d.mag ();
  };
  this.limit = function (l) {
    let mSq = this.magSq (); 
    if (mSq > l * l) {
      this.div (Math.sqrt (mSq));
      this.mult (l);
    }
    return this;
  };
  this.headingDegs = function () {
    let h = Math.atan2 (this.y, this.x);
    return h;
  };
  this.headingDeg = function () {
    let r = Math.atan2 (this.y, this.x);
    let h = r * 180.0 / Math.PI;
    return h;
  };
  this.rotateRads = function (a) {
    let newHead = this.headingDegs () + a; 
    let mag = this.mag ();
    this.x = Math.cos (newHead) * mag;
    this.y = Math.sin (newHead) * mag;
    return this;
  };
  this.rotateDegs = function (a) {
    a = a * Math.PI / 180.0; 
    let newHead = this.headingDegs () + a;
    let mag = this.mag ();
    this.x = Math.cos (newHead) * mag;
    this.y = Math.sin (newHead) * mag;
    return this;
  };
  this.angleBetweenRads = function (x, y) {
    let v1 = this.copy (), v2; 
    if (x instanceof Vector) {
      v2 = x.copy ();
    } else {
      v2 = new Vector (x, y);
    }
    let angle = Math.acos (v1.dot (v2) / (v1.mag () * v2.mag ()));
    return angle;
  };
  this.angleBetweenDegs = function (x, y) {
    let r = this.angleBetweenRads (x, y);
    let d = r * 180 / Math.PI;
    return d;
  };
  this.lerp = function (x, y, amt) {
    if (x instanceof Vector) {
      return this.lerp (x.x, x.y, y); 
    } 
    if (amt > 1.0) {
      amt = 1.0;
    }
    this.x += (x - this.x) * amt;
    this.y += (y - this.y) * amt;
    return this;
  };
  this.equals = function (x, y) {
    let a, b; 
    if (x instanceof Vector) {
      a = x.x || 0;
      b = x.y || 0;
    } else {
      a = x || 0;
      b = y || 0;
    }
    return this.x === a && this.y === b;
  };
  this.copy = function () {
    return new Vector (this.x, this.y); 
  }; 
}
function Vector ( x , y ) { this . x = x ; this . y = y ; this . set = function ( x , y ) { this . x = x ; this . y = y ; } ; this . magSq = function ( ) { let x = this . x , y = this . y ; return x * x + y * y ; } ; this . mag = function ( ) { return Math . sqrt ( this . magSq ( ) ) ; } ; this . add = function ( x , y ) { if ( x instanceof Vector ) { this . x += x . x ; this . y += x . y ; return this ; } this . x += x ; this . y += y ; return this ; } ; this . sub = function ( x , y ) { if ( x instanceof Vector ) { this . x -= x . x ; this . y -= x . y ; return this ; } this . x -= x ; this . y -= y ; return this ; } ; this . div = function ( n ) { this . x /= n ; this . y /= n ; return this ; } ; this . mult = function ( n ) { this . x *= n ; this . y *= n ; return this ; } ; this . normalize = function ( ) { return this . div ( this . mag ( ) ) ; } ; this . setMag = function ( n ) { return this . normalize ( ) . mult ( n ) ; } ; this . dot = function ( x , y ) { if ( x instanceof Vector ) { return this . dot ( x . x , x . y ) ; } return this . x * ( x || 0 ) + this . y * ( y || 0 ) ; } ; this . dist = function ( v ) { let d = v . copy ( ) . sub ( this ) ; return d . mag ( ) ; } ; this . limit = function ( l ) { let mSq = this . magSq ( ) ; if ( mSq > l * l ) { this . div ( Math . sqrt ( mSq ) ) ; this . mult ( l ) ; } return this ; } ; this . headingDegs = function ( ) { let h = Math . atan2 ( this . y , this . x ) ; return h ; } ; this . headingDeg = function ( ) { let r = Math . atan2 ( this . y , this . x ) ; let h = r * 180.0 / Math . PI ; return h ; } ; this . rotateRads = function ( a ) { let newHead = this . headingDegs ( ) + a ; let mag = this . mag ( ) ; this . x = Math . cos ( newHead ) * mag ; this . y = Math . sin ( newHead ) * mag ; return this ; } ; this . rotateDegs = function ( a ) { a = a * Math . PI / 180.0 ; let newHead = this . headingDegs ( ) + a ; let mag = this . mag ( ) ; this . x = Math . cos ( newHead ) * mag ; this . y = Math . sin ( newHead ) * mag ; return this ; } ; this . angleBetweenRads = function ( x , y ) { let v1 = this . copy ( ) , v2 ; if ( x instanceof Vector ) { v2 = x . copy ( ) ; } else { v2 = new Vector ( x , y ) ; } let angle = Math . acos ( v1 . dot ( v2 ) / ( v1 . mag ( ) * v2 . mag ( ) ) ) ; return angle ; } ; this . angleBetweenDegs = function ( x , y ) { let r = this . angleBetweenRads ( x , y ) ; let d = r * 180 / Math . PI ; return d ; } ; this . lerp = function ( x , y , amt ) { if ( x instanceof Vector ) { return this . lerp ( x . x , x . y , y ) ; } if ( amt > 1.0 ) { amt = 1.0 ; } this . x += ( x - this . x ) * amt ; this . y += ( y - this . y ) * amt ; return this ; } ; this . equals = function ( x , y ) { let a , b ; if ( x instanceof Vector ) { a = x . x || 0 ; b = x . y || 0 ; } else { a = x || 0 ; b = y || 0 ; } return this . x === a && this . y === b ; } ; this . copy = function ( ) { return new Vector ( this . x , this . y ) ; } ; }
A quick JavaScript 2D vector implementation.
A quick JavaScript 2D vector implementation .
// create a new instance using "new" // let v = new Vector(some_x_val, some_y_val); // reset the x,y values of an existing vector. // v.set(new_x_val, new_y_val); // returns the length of the vector, squared. // returns the length of the vector. // add two vectors together, or add x and y values // to an existing vector. // v.add(x_val, y_val) OR v.add(v2) // same as above, with subtraction // divide vector length (ie magnitude) by a constant // v.div(divisor) // multiply vector length (ie magnitude) by a constant // v.mult(scalar) // set magnitude equal to 1 // v.normalize() // set magnitude to a given value // v.setMag(new_length) // returns dot product of two vectors // v1.dot(v2) OR v.dot(x_val,y_val) // returns the distance between two points defined as vectors // v1.dist(v2) // constrain the magnitude (length) of a vector to the value // passed to this function. // v.limit(max_length) // returns heading in radians // returns heading in Degrees // rotates the vector by given angle in radians // v.rotateRads(angle_in_radians) // rotates the vector by given angle in radians // v.rotateDegs(angle_in_degrees) // find the angle between two vectors in radians // v1.angleBetweenRads(v2) OR v.angleBetweenRads(some_x,some_y) // same as above, except in degrees // linear interpolate the vector to another vector // amt is a value between 0.0 (close to the old vector) // and 1.0 (close to the new vector) // v1.lerp(v2, lerp_amount) OR v.lerp(some_x, some_y, lerp_amount) // checks if two vectors are identical. // returns true or false // v1.equals(v2) OR v.equals(some_x,some_y) // returns a COPY of the vector (ie pass by value, not by reference) // let v2 = v1.copy()