Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimobrian/3a5853fda426aea2d85d2d3e6909ba13 to your computer and use it in GitHub Desktop.
Save kimobrian/3a5853fda426aea2d85d2d3e6909ba13 to your computer and use it in GitHub Desktop.
JavaScript: Find the angle between three points. Inspired by conorbuck :https://gist.github.com/conorbuck/2606166 and Lance Roberts : http://stackoverflow.com/questions/1211212/how-to-calculate-an-angle-from-three-points
var p1={
x:0,
y:0
};
var p2={
x:0,
y:1
};
var p3={
x:-1,
y:1
};
var p12 = Math.sqrt(Math.pow((p1.x - p2.x),2) + Math.pow((p1.y - p2.y),2));
var p13 = Math.sqrt(Math.pow((p1.x - p3.x),2) + Math.pow((p1.y - p3.y),2));
var p23 = Math.sqrt(Math.pow((p2.x - p3.x),2) + Math.pow((p2.y - p3.y),2));
//angle in radians
var resultRadian = Math.acos(((Math.pow(p12, 2)) + (Math.pow(p13, 2)) - (Math.pow(p23, 2))) / (2 * p12 * p13));
//angle in degrees
var resultDegree = Math.acos(((Math.pow(p12, 2)) + (Math.pow(p13, 2)) - (Math.pow(p23, 2))) / (2 * p12 * p13)) * 180 / Math.PI;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment