Created
May 5, 2012 22:51
-
-
Save conorbuck/2606166 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 p1 = { | |
x: 20, | |
y: 20 | |
}; | |
var p2 = { | |
x: 40, | |
y: 40 | |
}; | |
// angle in radians | |
var angleRadians = Math.atan2(p2.y - p1.y, p2.x - p1.x); | |
// angle in degrees | |
var angleDeg = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @timjuedemann but I actually wrote a function for this eventually that allows you to change where zero is, and even calculates distance from cursor to the element too.
Check it out here: https://github.com/funkhaus/fuxt/blob/master/utils/angleDistanceToCursor.js