Skip to content

Instantly share code, notes, and snippets.

@ebonneville
Created October 22, 2012 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebonneville/3929297 to your computer and use it in GitHub Desktop.
Save ebonneville/3929297 to your computer and use it in GitHub Desktop.
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