我已经知道如何根据角度找到圆圆周上的点。我用来执行此操作的代码如下。
x = Math.sin(Math.toRadians(angle)) * radius;
y = Math.cos(Math.toRadians(angle)) * radius;
我正在尝试撤消此过程。
到目前为止,我有这段代码,它仅适用于小于或等于 90 度的角度。
DecimalFormat df = new DecimalFormat("###.####");
angleFromX = normalize(
Double.parseDouble(
df.format(
Math.toDegrees(
Math.asin(
(x / radius)
)
)
)
)
);
angleFromY = normalize(
Double.parseDouble(
df.format(
Math.toDegrees(
Math.acos(
(y / radius)
)
)
)
)
);
这是上面使用的标准化
方法。
public static double normalize(double angle) {
angle %= 360;
if (angle < 0) {
angle = angle + 360;
}
return angle;
}
最佳答案
你混淆了正弦和余弦。
double x = Math.cos(Math.toRadians(angle)) * radius;
double y = Math.sin(Math.toRadians(angle)) * radius;
要转换回来,请使用以下公式:
double newRadius = Math.hypot(x, y);
double theta = Math.atan2(y,x);
double newAngle = Math.toDegrees(theta);
根据实现情况,您可能需要调整 theta(角度)值。
- 如果位于象限 2 或 3,请添加 180 度。
- 如果位于象限 4,请添加 360 度。
您可能还需要添加:
newAngle = (newAngle+360)%360
保持角度为正且介于 0 到 360 之间。
关于java - 计算圆周上一点的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23924105/