两个坐标计算之间的轴承输出奇怪

发布于 2024-09-28 09:26:28 字数 2380 浏览 3 评论 0原文

我有一个方法,应该计算两个地理坐标之间的方位(以 40.7486,-73.9864 示例格式)。

然而,我遇到一个问题,它计算的标题与我使用经过尝试和测试的应用程序计算的标题不同。

例如,以下点之间的初始方位角为 074 度,但我的方法返回 047 度。
40.7486,-73.9864
40.9486, -72.9866

这是相关的代码片段。

        /// <summary>
    /// Calculate the inital bearing between two Locations
    /// </summary>
    /// <param name="pointA"></param>
    /// <param name="pointB"></param>
    /// <param name="headingType"></param>
    /// <returns></returns>
    public static double BearingToLocation(Location pointA, Location pointB)
    {
        // Convert both locations from degrees to radians
        pointA = LocationToRad(pointA);
        pointB = LocationToRad(pointB);

        double partOne = exMath.Sin(pointB.Longitude - pointA.Longitude) * exMath.Cos(pointB.Latitude);
        double partTwo = exMath.Cos(pointA.Latitude) * exMath.Sin(pointB.Latitude) - exMath.Sin(pointA.Latitude) * exMath.Cos(pointB.Latitude) * exMath.Cos(pointB.Longitude - pointA.Longitude);
        double heading = AdditionalMath.ToDeg(exMath.Atan2(partOne, partTwo) % 2 * exMath.PI);

        // Solve for compass wrap around
        if (heading < 0)
            heading += 360;

        return heading;
    }

    /// <summary>
    /// Return a new location in radians
    /// </summary>
    /// <param name="pointA"></param>
    /// <returns></returns>
    public static Location LocationToRad(Location pointA)
    {
        return new Location(AdditionalMath.ToRad(pointA.Latitude), AdditionalMath.ToRad(pointA.Longitude), pointA.Altitude);
    }

}

/// <summary>
/// Misc. math functions not available in Elze Kool's lib
/// </summary>
public static class AdditionalMath
{
    /// <summary>
    /// Degrees to radians
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    public static double ToRad(double x)
    {
        return exMath.PI * x / 180.00F;
    }

    /// <summary>
    /// Radians to degrees
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    public static double ToDeg(double x)
    {
        return x * 180.00F / exMath.PI;
    }
}

有人能看到这个问题吗?我查看了一下,没有发现问题所在。

I have a method that that is suppose to calculate the bearing between two geographic coordinates (in 40.7486, -73.9864 example format).

I am however, having an issue where the heading that it calculates is different from the heading I calculate using a tried and tested application.

For example, the initial bearing between of the following points is 074 degrees, however my method returns 047.

40.7486, -73.9864

40.9486, -72.9866

Here is the relevant snippit of code.

        /// <summary>
    /// Calculate the inital bearing between two Locations
    /// </summary>
    /// <param name="pointA"></param>
    /// <param name="pointB"></param>
    /// <param name="headingType"></param>
    /// <returns></returns>
    public static double BearingToLocation(Location pointA, Location pointB)
    {
        // Convert both locations from degrees to radians
        pointA = LocationToRad(pointA);
        pointB = LocationToRad(pointB);

        double partOne = exMath.Sin(pointB.Longitude - pointA.Longitude) * exMath.Cos(pointB.Latitude);
        double partTwo = exMath.Cos(pointA.Latitude) * exMath.Sin(pointB.Latitude) - exMath.Sin(pointA.Latitude) * exMath.Cos(pointB.Latitude) * exMath.Cos(pointB.Longitude - pointA.Longitude);
        double heading = AdditionalMath.ToDeg(exMath.Atan2(partOne, partTwo) % 2 * exMath.PI);

        // Solve for compass wrap around
        if (heading < 0)
            heading += 360;

        return heading;
    }

    /// <summary>
    /// Return a new location in radians
    /// </summary>
    /// <param name="pointA"></param>
    /// <returns></returns>
    public static Location LocationToRad(Location pointA)
    {
        return new Location(AdditionalMath.ToRad(pointA.Latitude), AdditionalMath.ToRad(pointA.Longitude), pointA.Altitude);
    }

}

/// <summary>
/// Misc. math functions not available in Elze Kool's lib
/// </summary>
public static class AdditionalMath
{
    /// <summary>
    /// Degrees to radians
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    public static double ToRad(double x)
    {
        return exMath.PI * x / 180.00F;
    }

    /// <summary>
    /// Radians to degrees
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    public static double ToDeg(double x)
    {
        return x * 180.00F / exMath.PI;
    }
}

Can anyone see the issue? I looked it over and could not find the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2024-10-05 09:26:28

此行中的 % 看起来很可疑:

    double heading = AdditionalMath.ToDeg(exMath.Atan2(partOne, partTwo) % 2 * exMath.PI);

The % looks fishy in this line:

    double heading = AdditionalMath.ToDeg(exMath.Atan2(partOne, partTwo) % 2 * exMath.PI);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文