Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dead23angel/7b3b4e2d1bb33015159efd02a2aeb1c6 to your computer and use it in GitHub Desktop.
Save dead23angel/7b3b4e2d1bb33015159efd02a2aeb1c6 to your computer and use it in GitHub Desktop.
Javascript Math: get the distance between two points.
/**
* return the distance between two points.
*
* @param {number} x1 x position of first point
* @param {number} y1 y position of first point
* @param {number} x2 x position of second point
* @param {number} y2 y position of second point
* @return {number} distance between given points
*/
Math.getDistance = function( x1, y1, x2, y2 ) {
var xs = x2 - x1,
ys = y2 - y1;
xs *= xs;
ys *= ys;
return Math.sqrt( xs + ys );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment