public static double calculateDirection(double x, double y) {
if (x == 0) {
if (y > 0) {
return Math.PI / 2;
}
return Math.PI * 3 / 2;
}
if (x > 0 && y >= 0) {
return Math.atan(y / x);
} else if (x > 0 && y < 0) {
// tan < 0
return 2 * Math.PI + Math.atan(y / x);
} else if (x < 0 && y > 0) {
// tan < 0
return Math.PI + Math.atan(y / x);
} else {
// tan > 0
return Math.PI + Math.atan(y / x);
}
}
看象限没啥好说的。