Last active
May 4, 2023 05:24
-
-
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
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
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; |
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
This is exactly was I was looking for. Thanks