Skip to content

Instantly share code, notes, and snippets.

@JoshuaCaputo
Forked from conorbuck/angle-between-points.js
Last active July 11, 2018 06:54
Show Gist options
  • Save JoshuaCaputo/c1c3901ff9795d5146fa23a3809c9634 to your computer and use it in GitHub Desktop.
Save JoshuaCaputo/c1c3901ff9795d5146fa23a3809c9634 to your computer and use it in GitHub Desktop.
JavaScript: Find the angle between two points
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