计算圆周上的点角

时间:2014-05-29 00:49:05

标签: java geometry trigonometry cartesian-coordinates

我已经知道如何根据角度在圆周上找到一个点。我用来执行此操作的代码如下所示。

x = Math.sin(Math.toRadians(angle)) * radius;
y = Math.cos(Math.toRadians(angle)) * radius;

我试图撤消这个过程。

到目前为止,我有这个代码,它仅适用于小于或等于90度的角度。

DecimalFormat df = new DecimalFormat("###.####");

angleFromX = normalize(
    Double.parseDouble(
        df.format(
            Math.toDegrees(
                Math.asin(
                    (x / radius)
                )
            )
        )
    )
);
angleFromY = normalize(
    Double.parseDouble(
        df.format(
            Math.toDegrees(
                Math.acos(
                    (y / radius)
                )
            )
        )
    )
);

以上使用的normalize方法。

public static double normalize(double angle) {
    angle %= 360;

    if (angle < 0) {
        angle = angle + 360;
    }

    return angle;
}

1 个答案:

答案 0 :(得分:3)

你混淆了罪和cos。

double x = Math.cos(Math.toRadians(angle)) * radius;
double y = Math.sin(Math.toRadians(angle)) * radius;

要转换回来,请使用以下公式:

double newRadius = Math.hypot(x, y);
double theta = Math.atan2(y,x);
double newAngle = Math.toDegrees(theta);

根据实施情况,可能需要调整你的θ(角度)值。

  • 如果它在象限2或3中,则添加180度。
  • 如果它在象限4中,请添加360度。

此外,您可能需要添加:

newAngle = (newAngle+360)%360

保持角度为正,介于0和360之间。