Skip to content

Instantly share code, notes, and snippets.

@PaulMaynard
Created June 9, 2014 17:13
Show Gist options
  • Save PaulMaynard/be3402b412fe6d6f1fd7 to your computer and use it in GitHub Desktop.
Save PaulMaynard/be3402b412fe6d6f1fd7 to your computer and use it in GitHub Desktop.
Gist from mistakes.io
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.dist = function dist(p) {
return Math.sqrt(Math.pow(Math.abs(this.x - p.x), 2)
+ Math.pow(Math.abs(this.y - p.y), 2));
}
Point.prototype.toString = function() {
return '(' + this.x + ',' + this.y + ')'
}
function Point3d(x, y, z) {
Point.call(this, x, y)
this.z = z
}
Point3d.prototype = Object.create(Point.prototype)
Point3d.prototype.dist = function dist(p) {
return Math.sqrt(Math.pow(Math.abs(this.x - p.x), 2)
+ Math.pow(Math.abs(this.y - p.y), 2)
+ Math.pow(Math.abs(this.z - (p.z || 0)), 2));
}
Point3d.prototype.toString = function() {
return '(' + this.x + ',' + this.y + ',' + this.z + ')'
}
function Line(p1, p2) {
this.start = p1;
this.end = p2;
}
Line.prototype.toString = function() {
return '[' + this.start + '-' + this.end + ' <' + this.length + '>' + ']'
}
Object.defineProperty(Line.prototype, 'length', {
configurable: true,
enumerable: true,
get: function() {
return this.start.dist(this.end);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment