想象一下,我在屏幕上绘制了一个带有中心坐标(cx,cy)的圆圈,并在圆圈上选择了一个随机点(A)。
通过得到A点的坐标,我需要找到(a)的角度。
更新
我尝试使用以下公式:
Math.toDegrees(Math.asin(((x - cx) / radius).toDouble()))
实际上是相反的(圆圈是通过向这个角度馈送角度创建的):
x = radius * Math.sin(Math.toRadians(angle.toDouble())) + cx
y = radius * Math.cos(Math.toRadians(angle.toDouble())) + cy
但由于公式中不存在y坐标,因此答案可能是错误的。
答案 0 :(得分:3)
如果您知道点A(x,y)的笛卡尔坐标,那么您可以通过将其转换为极坐标来找到角度theta,如下所示:
double theta = Math.toDegrees(Math.atan2(y - cy, x - cx));
如果你的X轴是0度,这个公式是有效的,否则你需要考虑偏移。
答案 1 :(得分:2)
我认为你正在寻找的方法是Math.atan2,它计算x和y坐标的角度。我现在修改了代码以调整向下0度。我还翻转了y轴,将0,0坐标放在左上角(屏幕坐标),调整度数大于180,报告为负度:
public double theta(int cx, int cy, int x, int y)
{
double angle = Math.toDegrees(Math.atan2(cy - y, x - cx)) + 90;
return angle <= 180? angle: angle - 360;
}
验证某些角度的小测试...
@Test
public void test()
{
assertThat(theta(50, 50, 60, 50), is(90.0));
assertThat(theta(50, 50, 50, 60), is(0.0));
assertThat(theta(50, 50, 40, 50), is(-90.0));
assertThat(theta(50, 50, 50, 40), is(180.0));
}
答案 2 :(得分:-1)
你可以找到切线角度并从270那个角度添加到90或子结构,我相信找到结果。我像你的绘图一样设计代码。我想你可以让它更通用。 你有4个区域:
代码:
public static double findAngle(double x, double y,
double cx, double cy, double radius){
double beta, alfa;
double distanceX = Math.abs(Math.abs(x) - Math.abs(cx));
double distanceY = Math.abs(Math.abs(y) - Math.abs(cy));
// check the point is on the circle or not
// with euchlid
if (radius != Math.sqrt(x * x + y * y)) {
return -1;
}
if (x >= cx && y <= cy) {
// find tangent
beta = Math.atan(distanceY / distanceX);
alfa = 90 - beta;
return alfa;
}
// 90-180 -> second area
else if (x >= cx && y >= cy) {
beta = Math.atan(distanceY / distanceX);
alfa = 90 + beta;
return alfa;
}
// 180 - -90 -> third area
else if (x <= cx && y >= cy) {
beta = Math.atan(distanceY / distanceX);
alfa = 270 - beta;
return alfa;
}
// -90 - 0 -> forth area
else if (x <= cx && y <= cy) {
beta = Math.atan(distanceY / distanceX);
alfa = 270 + beta;
if (alfa == 360) {
alfa = 0;
}
return alfa;
}
else {
return -1;
}
}