简体   繁体   English

求圆上点的角度

[英]Find angle of point on circle

Imagine I have drawn a circle with center coordinates (cx,cy) on the screen and a random point (A) is selected on the circle.想象一下,我在屏幕上绘制了一个中心坐标为 (cx,cy) 的圆,并在圆上选择了一个随机点 (A)。

三角圆

By having the the coordinates of point A, I need to find the angle of (a).通过获得 A 点的坐标,我需要找到 (a) 的角度。

Update:更新:

I have tried using the following formula:我尝试使用以下公式:

Math.toDegrees(Math.asin(((x - cx) / radius).toDouble()))

which is actually the reverse of (the circle is created by feeding angles to this one):这实际上是相反的(圆形是通过向这个角度馈送角度创建的):

x = radius * Math.sin(Math.toRadians(angle.toDouble())) + cx
y = radius * Math.cos(Math.toRadians(angle.toDouble())) + cy

But since the y coordinate is not present in the formula the answer could be wrong.但由于公式中不存在 y 坐标,因此答案可能是错误的。

If you know the cartesian coordinates of the point A(x,y), then you can find the angle theta by converting it into polar coordinates as below:如果您知道点 A(x,y) 的笛卡尔坐标,那么您可以通过将其转换为极坐标来找到角度 theta,如下所示:

double theta = Math.toDegrees(Math.atan2(y - cy, x - cx));

This formula works if your X axis is in 0 degrees, otherwise you need to consider the offset.如果您的 X 轴在 0 度,则此公式有效,否则您需要考虑偏移。

I think the method you are looking for i Math.atan2 which computes the angle to an x and y cordinate.我认为您正在寻找的方法 i Math.atan2 计算 x 和 y 坐标的角度。 I have now modified the code to adjust for putting 0 degrees downwards.我现在修改了代码以调整向下 0 度。 I have also flipped the y-axis to put 0, 0 cordinate in the upper left corner (screen coordinates), and adjusted degrees above 180 to be reported as negative degrees:我还翻转了 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;
}

A small test to verify some angles...一个小测试来验证一些角度......

@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));
}

You can find the tangent angles and add to 90 or substruct from 270 that angle and find the result, I believe.我相信,您可以找到切线角度并将该角度添加到 90 或从 270 减去该角度并找到结果。 I design the code like your drawing.我设计的代码就像你的画一样。 You can make it more generic, I guess.我想你可以让它更通用。 You have 4 area:您有 4 个区域:

  1. 0 degree to 90 degree 0度到90度
  2. 90 degree to 180 degree 90度到180度
  3. 90 degree to -90(270) degree 90 度至 -90(270) 度
  4. -90(270) degree to 0(360) degree -90(270) 度到 0(360) 度

Code:代码:

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;
    }
}

The main catch with Atan2 is the center point. Atan2的主要问题是中心点。 If your center is offset (Cx, Cy) and not at the origin and you want to find the arc angle between (X1, Y1) and (X2, Y2) then I would suggest this approach:如果您的中心偏移(Cx, Cy)而不是在原点,并且您想找到(X1, Y1)(X2, Y2)之间的弧角(X2, Y2)那么我建议使用这种方法:

double startAngle = Math.Atan2(Y1-Cy, X1-Cx);
double endAngle = Math.Atan2(Y2-Cy, X2-Cx);
double ArcAngle = endangle - startAngle;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM