Skip to content

Instantly share code, notes, and snippets.

@Samueleroux
Last active May 4, 2023 05:24
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Samueleroux/f6854e8e443a210ff6958b23f2237097 to your computer and use it in GitHub Desktop.
Save Samueleroux/f6854e8e443a210ff6958b23f2237097 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;
@Seh83
Copy link

Seh83 commented Jan 14, 2021

This is exactly was I was looking for. Thanks

@distroinfinity
Copy link

Which out of p1,p2 and p3 is the vertex point in this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment