Skip to content

Instantly share code, notes, and snippets.

@amodig
Created April 14, 2016 12:34
Show Gist options
  • Save amodig/d93a3219b7a8d7f7cc7b627a1a3b4208 to your computer and use it in GitHub Desktop.
Save amodig/d93a3219b7a8d7f7cc7b627a1a3b4208 to your computer and use it in GitHub Desktop.
Javascript Vector constructor
function Vector(x, y) {
this.x = x;
this.y = y;
}
Vector.prototype.plus = function(vector) {
var x_new = this.x + vector.x;
var y_new = this.y + vector.y;
return new Vector(x_new, y_new);
}
Vector.prototype.minus = function(vector) {
var x_new = this.x - vector.x;
var y_new = this.y - vector.y;
return new Vector(x_new, y_new);
}
Object.defineProperty(Vector.prototype, "length", {
get: function() { return Math.sqrt(this.x*this.x + this.y*this.y); }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment