Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Created July 18, 2012 08:40
Show Gist options
  • Save davidaurelio/3135083 to your computer and use it in GitHub Desktop.
Save davidaurelio/3135083 to your computer and use it in GitHub Desktop.
A toString()’able point that allows addition
function Point(x, y) {
if (arguments.length === 1 && typeof x === 'string') {
return x.slice(1, -1)
.split('}{')
.map(function(tuple) {
return tuple.split(',', 2).map(parseFloat)
})
.reduce(function(point, coords) {
point.x += coords[0];
point.y += coords[1];
return point;
}, new Point());
}
this.x = x || 0;
this.y = y || 0;
}
Point.prototype.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