求两点之间连线的角度

function getAngle(px1, py1, px2, py2) {
//两点的x、y值
x = px2-px1;
y = py2-py1;
hypotenuse = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
//斜边长度
cos = x/hypotenuse;
radian = Math.acos(cos);
//求出弧度
angle = 180/(Math.PI/radian);
//用弧度算出角度
if (y<0) {
angle = -angle;
} else if ((y == 0) && (x<0)) {
angle = 180;
}
return angle;
}

猜你喜欢

转载自blog.csdn.net/strivero/article/details/53078578