Skip to content

Instantly share code, notes, and snippets.

@ashblue
Created October 7, 2012 02:14
Show Gist options
  • Save ashblue/3846858 to your computer and use it in GitHub Desktop.
Save ashblue/3846858 to your computer and use it in GitHub Desktop.
Angle between points
/**
* 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