【问题标题】:Calculate angle of point on circumference计算圆周上点的角度
【发布时间】:2014-05-29 00:49:05
【问题描述】:

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

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

【问题讨论】:

    标签: java geometry trigonometry cartesian-coordinates


    【解决方案1】:

    你把 sin 和 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);
    

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

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

    您可能还需要添加:

    newAngle = (newAngle+360)%360
    

    保持角度为正且在 0 到 360 度之间。

    【讨论】:

    • 你为什么要改变半径?
    • 改变?你的意思是radius = Math.sqrt(x*x + y*y)
    • 这两个例子是相互独立的,不清楚吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2014-12-11
    相关资源
    最近更新 更多