2017-09-24 418 views
-1

想象一下,我在屏幕上画了一个中心坐标为(cx,cy)的圆,并在圆上选择了一个随机点(A)。查找圆上的点的角度

trigonometric circle

通过具有点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

看到'java.lang.Math'包文档。你尝试过^ F'角度吗? – pskink

+1

在我的几何类中,零度在圆的东侧 –

回答

1

如果你知道点A(X,Y)的直角坐标系,那么你可以将它转换成极坐标如下找到θ角:

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

该公式工作,如果你的X轴在0度,否则你需要考虑偏移量。

+0

谢谢,但问题在于这个公式无法正确区分象限。此公式始终返回从0到180的角度。 –

+0

您可以使用CAST规则计算出角度,例如。如果x和y都是正的,那么角度是相同的,如果x和y都是负的,那么角度就是theta + 180 –

+1

@HamedMomeni所以你检查了'Math'软件包文档? *“这种方法通过计算y/x在-pi到pi范围内的反正切来计算相位theta * – pskink

1

我认为你正在寻找的方法是Math.atan2,它计算出一个x和y坐标的角度。我现在修改了代码以调整向下0度。我还翻转y轴把0,0 cordinate在左上角(屏幕坐标),和调整度以上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)); 
} 
+1

正确的答案,但...方向是相反的,并且起点由pi/2抵消 - 所以我不认为OP接受它,因为他想要鱼,而不是鱼竿 – pskink

-1

你可以找到切线的角度和从270增加90或substruct这个角度,找到的结果,我相信。我设计的代码就像你的绘图。我猜,你可以使它更通用。 你有4区:

  1. 0度到90度
  2. 90度至180度
  3. 90度到-90(270)度
  4. -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; 
    } 
}