[如何在JavaScript中计算三点之间的夹角? [关闭]

问题描述 投票:14回答:1

我想在JavaScript中获得3点之间的夹角。如果我有点A(x1,y1),B(x2,y2),C(x3,y3),我想获得由AB和BC线形成的角度。

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

javascript coordinates point angle
1个回答
26
投票

尝试此功能:

 /*
 * 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));
}
© www.soinside.com 2019 - 2024. All rights reserved.