Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Basic position holding class.
// basic position class, holds x and y coordinates and utility functions
function Vector2(x, y) {
this.x = x;
this.y = y;
this.add = function(other) {
return new Vector2(this.x + other.x, this.y + other.y);
};
this.distance = function(pos) {
var dx = pos.x - this.x;
var dy = pos.y - this.y;
return Math.abs(Math.sqrt((dx * dx) + (dy * dy)));
};
this.manhattan = function(pos) {
return(Math.abs(this.x - pos.x) + Math.abs(this.y - pos.y));
};
this.clone = function() {
return(new Vector2(this.x, this.y));
};
this.toString = function() {
return("(" + this.x + ", " + this.y + ")");
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment