How to calculate in JavaScript angle between 3 points? [closed]

安稳与你 提交于 2020-01-01 02:26:28

问题


I want to get angle between 3 points in JavaScript. If I have points A(x1,y1), B(x2, y2), C(x3, y3) I want to get angle that is formed with lines AB and BC.

let A = {x:x1, y:y1}, B = {x:x2, y:y2}, C = {x:x3, y:y3}


回答1:


Try this function :

 /*
 * Calculates the angle ABC (in radians) 
 *
 * A first point, ex: {x: 0, y: 0}
 * C second point
 * B center point
 */
function find_angle(A,B,C) {
    var AB = Math.sqrt(Math.pow(B.x-A.x,2)+ Math.pow(B.y-A.y,2));    
    var BC = Math.sqrt(Math.pow(B.x-C.x,2)+ Math.pow(B.y-C.y,2)); 
    var AC = Math.sqrt(Math.pow(C.x-A.x,2)+ Math.pow(C.y-A.y,2));
    return Math.acos((BC*BC+AB*AB-AC*AC)/(2*BC*AB));
}


来源:https://stackoverflow.com/questions/17763392/how-to-calculate-in-javascript-angle-between-3-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!