JS计算三个坐标点的夹角

先计算两个线段的弧度 再乘以180/π 得到两个点到中间点的夹角角度

function getAngle(pointA, pointB, pointC) {
        const ABX = pointA.longitude - pointB.longitude;
        const ABY = pointA.latitude - pointB.latitude;
        const CBX = pointC.longitude - pointB.longitude;
        const CBY = pointC.latitude - pointB.latitude;
        const AB_MUL_CB = ABX * CBX + ABY * CBY;
        const DIST_AB = Math.sqrt(ABX*ABX+ABY*ABY)
        const DIST_CB = Math.sqrt(CBX*CBX+CBY*CBY)
        const cosValue = AB_MUL_CB/(DIST_AB*DIST_CB);
        return Math.acos(cosValue)*180/Math.PI;
    }
    //90度
   console.log(getAngle({longitude:116.379817,latitude:39.930138},{longitude:116.3805,latitude:39.91333},{longitude:116.398035,latitude:39.913856}))
posted @ 2022-01-25 11:04  零贰  阅读(1340)  评论(0编辑  收藏  举报