Created
July 18, 2012 08:40
-
-
Save davidaurelio/3135083 to your computer and use it in GitHub Desktop.
A toString()’able point that allows addition
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 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