Created
October 7, 2012 02:14
-
-
Save ashblue/3846858 to your computer and use it in GitHub Desktop.
Angle between 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
/** | |
* Calculates the angle between two separate points. 0 degrees points up and 180 degrees points down | |
* @link https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/atan2 | |
* @link http://stackoverflow.com/questions/3309617/calculating-degrees-between-2-points-with-inverse-y-axis | |
* @param {object} start Beginning vertex formatted as {x, y} | |
* @param {object} end End vertex formatted as {x, y} | |
* @returns {number} Returns a counterclockwise angle, measured in radians | |
*/ | |
function angleBetweenPoints (start, end) { | |
// find angle | |
var theta = Math.atan2(end[0] - start[0], -(end[1] - start[1])); | |
// if negative flip it positive | |
if (theta < 0) { | |
theta += 2 * Math.PI; | |
} | |
return theta; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment