Vector, plus and minus - Node.js Data Structure

Node.js examples for Data Structure:Vector

Description

Vector, plus and minus

Demo Code



function Vector(x,y) {
  this.x = x;/*ww w  .  ja v a2s  . c o  m*/
  this.y = y;
}

Vector.prototype.plus = function(vector) {
  xSum = this.x + vector.x;
  ySum = this.y + vector.y;
  return new Vector(xSum,ySum);
}

Vector.prototype.minus = function(vector) {
  xDiff = this.x - vector.x;
  yDiff = this.y - vector.y;
  return new Vector(xDiff,yDiff);
}

Object.defineProperty(Vector.prototype, "length", {
  get: function() { 
    return Math.sqrt(Math.pow(this.x,2) + Math.pow(this.y,2)) 
  }
})

console.log(new Vector(1, 2).plus(new Vector(2, 3)));

console.log(new Vector(1, 2).minus(new Vector(2, 3)));

console.log(new Vector(3, 4).length);

Related Tutorials