求圆上点的角度
Find angle of point on circle
假设我在屏幕上画了一个中心坐标为 (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 坐标,所以答案可能是错误的。
如果你知道点A(x,y)的笛卡尔坐标,那么你可以通过将其转换为极坐标来求出角度theta,如下所示:
double theta = Math.toDegrees(Math.atan2(y - cy, x - cx));
如果您的 X 轴为 0 度,则此公式有效,否则您需要考虑偏移量。
我认为您正在寻找的方法是 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));
}
我相信,您可以求出切角并加上 90 度或从 270 度减去该角度并求得结果。我设计的代码就像你的图一样。我想你可以让它更通用。
您有 4 个区域:
- 0 度到 90 度
- 90 度到 180 度
- 90度到-90(270)度
- -90(270)度到0(360)度
代码:
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;
}
}
Atan2
的主要收获是中心点。如果您的中心偏移 (Cx, Cy)
而不是原点,并且您想找到 (X1, Y1)
和 (X2, Y2)
之间的弧角,那么我建议采用这种方法:
double startAngle = Math.Atan2(Y1-Cy, X1-Cx);
double endAngle = Math.Atan2(Y2-Cy, X2-Cx);
double ArcAngle = endangle - startAngle;
假设我在屏幕上画了一个中心坐标为 (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 坐标,所以答案可能是错误的。
如果你知道点A(x,y)的笛卡尔坐标,那么你可以通过将其转换为极坐标来求出角度theta,如下所示:
double theta = Math.toDegrees(Math.atan2(y - cy, x - cx));
如果您的 X 轴为 0 度,则此公式有效,否则您需要考虑偏移量。
我认为您正在寻找的方法是 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));
}
我相信,您可以求出切角并加上 90 度或从 270 度减去该角度并求得结果。我设计的代码就像你的图一样。我想你可以让它更通用。 您有 4 个区域:
- 0 度到 90 度
- 90 度到 180 度
- 90度到-90(270)度
- -90(270)度到0(360)度
代码:
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;
}
}
Atan2
的主要收获是中心点。如果您的中心偏移 (Cx, Cy)
而不是原点,并且您想找到 (X1, Y1)
和 (X2, Y2)
之间的弧角,那么我建议采用这种方法:
double startAngle = Math.Atan2(Y1-Cy, X1-Cx);
double endAngle = Math.Atan2(Y2-Cy, X2-Cx);
double ArcAngle = endangle - startAngle;