Created
January 23, 2012 12:22
-
-
Save mbernson/1662820 to your computer and use it in GitHub Desktop.
Punt class in Javascript
This file contains hidden or 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
/** | |
* Punt | |
*/ | |
var Punt = function( x, y ) | |
{ | |
this.x = x || 0; | |
this.y = y || 0; | |
this.distance = function( punt ) | |
{ | |
if( ! punt instanceof Punt ) | |
throw 'Invalid argument.'; | |
return Math.sqrt( | |
( punt.x - this.x ) * ( punt.x - this.x ) + | |
( punt.y - this.y ) * ( punt.y - this.y ) | |
); | |
} | |
} | |
// Example | |
var p = new Punt( 23, 48 ), | |
q = new Punt( 2, 4 ); | |
console.log( | |
q.distance( p ) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment