Created
April 14, 2016 12:34
-
-
Save amodig/d93a3219b7a8d7f7cc7b627a1a3b4208 to your computer and use it in GitHub Desktop.
Javascript Vector constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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