double anglex = Math.atan2((y2-y1), (x2-x1)); //与X轴的夹角
double angley = Math.atan2((x2-x1), (y2-y1)); //与Y轴的夹角
System.out.println(angle*(180/Math.PI));
以下代码仅供参考(需要知道0°的基准)
public static double CalulateXYAnagle(double startx, double starty, double endx, double endy) {
//除数不能为0
double tan = Math.atan(Math.abs((endy - starty) / (endx - startx))) * 180 / Math.PI;
if (endx > startx && endy > starty){
//第一象限
return -tan;
} else if (endx < startx && endy > starty){
//第二象限
return tan;
} else if (endx < startx && endy < starty){
//第三象限
return tan - 180;
} else {
return 180 - tan;
}
}