Find angle of point on circle

拜拜、爱过 提交于 2019-12-06 10:37:54

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:

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.

I think the method you are looking for i Math.atan2 which computes the angle to an x and y cordinate. I have now modified the code to adjust for putting 0 degrees downwards. 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:

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

You can find the tangent angles and add to 90 or substruct from 270 that angle and find the result, I believe. I design the code like your drawing. You can make it more generic, I guess. You have 4 area:

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

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

The main catch with Atan2 is the center point. 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:

double startAngle = Math.Atan2(Y1-Cy, X1-Cx);
double endAngle = Math.Atan2(Y2-Cy, X2-Cx);
double ArcAngle = endangle - startAngle;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!