Forked from Samueleroux/angle-between-three-points.js
Created
April 15, 2020 07:28
-
-
Save miketahani/6bf61c42fd73fbf34aae5e0eba8ae117 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 hidden or 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment