Created
August 19, 2023 05:09
-
-
Save r-i-c-h/fae07fe234397a336b7f11ad0fc04278 to your computer and use it in GitHub Desktop.
HTML Canvas Calculate Distance Between 2 Points
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 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