Skip to content

Instantly share code, notes, and snippets.

@r-i-c-h
Created August 19, 2023 05:09
Show Gist options
  • Save r-i-c-h/fae07fe234397a336b7f11ad0fc04278 to your computer and use it in GitHub Desktop.
Save r-i-c-h/fae07fe234397a336b7f11ad0fc04278 to your computer and use it in GitHub Desktop.
HTML Canvas Calculate Distance Between 2 Points
function calcDistBetweenPoints(pt1, pt2) {
const dx = pt1.x - pt2.x;
const dy = pt1.y - pt2.y;
// Distance = Pythagorean Theorem to get Hypoteneuse of triangle represented by the two points
// doing it with Math.sqrt() is apparently faster than going directly to Math.hypot() 🤷👌
return Math.sqrt((dx * dx) + (dy * dy));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment