getAngle(pointA, pointB, pointC) {
// pointC = {
// x: pointA.x + 100,
// y: pointA.y
// }
//向量
const BA = {
x: pointB.x - pointA.x,
y: pointB.y - pointA.y
}
const CA = {
x: pointC.x - pointA.x,
y: pointC.y - pointA.y
}
// 计算叉积
const crossProduct = BA.x * CA.y - BA.y * CA.x;
let direction;
if(crossProduct > 0){
direction = -1; //逆时针
}else if(crossProduct < 0){
direction = 1; //顺时针
}else{
direction = 0; //同线
}
// 计算向量点积/模长
const dot = BA.x * CA.x + BA.y * CA.y;
const mat1 = Math.sqrt(BA.x * BA.x + BA.y * BA.y);
const mat2 = Math.sqrt(CA.x * CA.x + CA.y * CA.y);
// 计算角度(以弧度为单位)
const angle = Math.acos(dot / (mat1 * mat2));
// 将弧度转换为角度(以度为单位)
const Angle = angle * (180 / Math.PI);
return Angle.toFixed(); //取整字符串
}
direction是存在角度顺逆时针需要考虑的,比如顺时针转90度其实是逆时针270度,具体依据使用情况而定。