# js 已知A,B,C三个点的坐标,求B的角度？

``````let A = {X:0,Y:0}
let B = {X:4,Y:0}
let C = {X:4,Y:4}
getAngle(A,B,C, 'B')``````

``````function getAngle(A,B,C,angle) {
// coding
}``````

3 个回答

https://zhuanlan.zhihu.com/p/...

https://zhidao.baidu.com/ques...

https://jingyan.baidu.com/art...

``````function getAngle(A,B,C,angle) {
// 通过坐标算边长
let a = Math.sqrt(Math.pow(C.x-B.x,2) + Math.pow(C.y-B.y,2));
let b = Math.sqrt(Math.pow(A.x-C.x,2) + Math.pow(A.y-C.y,2));
let c = Math.sqrt(Math.pow(B.x-A.x,2) + Math.pow(B.y-A.y,2));
// 通过边长算余弦
let cosA = Math.acos((b*b+c*c-a*a)/(b*c*2));
let cosB = Math.acos((a*a+c*c-b*b)/(a*c*2));
let cosC = Math.acos((a*a+b*b-c*c)/(a*b*2));
// 通过余弦算角度
return ({
A : cosA * 180 / Math.PI,
B : cosB * 180 / Math.PI,
C : cosC * 180 / Math.PI,
})[angle];
}``````

`` a * c= |a| * |c| * cos<a,c>=a.x * c.x + a.y* c.y``

``角B = acos(（a * c）/ ( |a| * |c|) );``

``````function calculateAngle(lonlat1, lonlat2) {

if(lon1==lon2){
if (lat2 >= lat1){
return 0
}else{
return 180
}
}
if (lat1 == lat2){
if(lon2 > lon1 ){
return 90
}else if(lon2 < lon1){
return 270
}
}

var a = THREE.Math.radToDeg(Math.atan((lon2 - lon1) * Math.cos(lat2) / (lat2 - lat1)))
if(lat2 > lat1){
if(lon2 > lon1){
return a
}else{
return a + 360
}
}else{
return a + 180
}
}``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进