-
-
Save JoshuaCaputo/c1c3901ff9795d5146fa23a3809c9634 to your computer and use it in GitHub Desktop.
JavaScript: Find the angle between two 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
var angleBetweenPoints = function(p1, p2, rad){ // vector2, vector2, bool | |
if (!p1 || !p2){ | |
return false; | |
} | |
// format angle in radians | |
var angleRadians = Math.atan2(p2.y - p1.y, p2.x - p1.x); | |
// format angle in degrees | |
var angleDegrees = angleRadians * 180 / Math.PI; | |
// return formatted angle | |
if (rad) return angleRadians; | |
return angleDegrees; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment