Skip to content

Instantly share code, notes, and snippets.

@mbernson
Created January 23, 2012 12:22
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 mbernson/1662820 to your computer and use it in GitHub Desktop.
Save mbernson/1662820 to your computer and use it in GitHub Desktop.
Punt class in Javascript
/**
* 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