C# C语言中的轴承计算#

C# C语言中的轴承计算#,c#,bearing,gis,C#,Bearing,Gis,我想计算两个GPS位置之间的方位角,我愚弄了我的算法建议: public static double Bearing(IPointGps pt1, IPointGps pt2) { double x = Math.Cos(pt1.Latitude) * Math.Sin(pt2.Latitude) - Math.Sin(pt1.Latitude) * Math.Cos(pt2.Latitude) * Math.Cos(pt2.Longitude - pt1.Lon

我想计算两个GPS位置之间的方位角,我愚弄了我的算法建议:

    public static double Bearing(IPointGps pt1, IPointGps pt2)
    {
        double x = Math.Cos(pt1.Latitude) * Math.Sin(pt2.Latitude) - Math.Sin(pt1.Latitude) * Math.Cos(pt2.Latitude) * Math.Cos(pt2.Longitude - pt1.Longitude);
        double y = Math.Sin(pt2.Longitude - pt1.Longitude) * Math.Cos(pt2.Latitude);

        // Math.Atan2 can return negative value, 0 <= output value < 2*PI expected 
        return (Math.Atan2(y, x) + Math.PI * 2)%(Math.PI * 2);
    }
我有以下测试样本:

  • 点1(横向、纵向)=43.6373638888888888888888889,1.357622222222
  • 点2(横向、纵向)=43.61564444,1.380225
  • 预期轴承=323°
但是,我获得了315.5°(5.50623558910762 rad)的方位角。如果我计算出期望的弧度值,我得到了5.637413,这无疑是我的问题在于我的方位方法

我已经使用.Net数学包实现了其他计算方法(包括Cos、Sin、Tan和ATan方法),并且我的单元测试以1e-12精度通过。我错过了什么

PS:我还尝试重新实现Atan2方法,以防精度不高。我得到了同样的结果

编辑:根据下面的界面,我的纬度和经度是双倍的

public interface IPointGps
{
    double Latitude { get; }
    double Longitude { get; }
}

看起来纬度和经度变量是浮动的(单精度)。如果是这种情况,那么您将面临精度错误。

看起来您的纬度和经度变量是浮点(单精度)。如果是这种情况,那么您将面临一个精度错误。

Math.Sin()
和所有类似的方法都希望参数以弧度为单位,但纬度和经度以度为单位。在计算方向角或修改方向角计算之前,必须将IPointGps转换为弧度,例如:

public static double Bearing(IPointGps pt1, IPointGps pt2)
{
    double x = Math.Cos(DegreesToRadians(pt1.Latitude)) * Math.Sin(DegreesToRadians(pt2.Latitude)) - Math.Sin(DegreesToRadians(pt1.Latitude)) * Math.Cos(DegreesToRadians(pt2.Latitude)) * Math.Cos(DegreesToRadians(pt2.Longitude - pt1.Longitude));
    double y = Math.Sin(DegreesToRadians(pt2.Longitude - pt1.Longitude)) * Math.Cos(DegreesToRadians(pt2.Latitude));

    // Math.Atan2 can return negative value, 0 <= output value < 2*PI expected 
    return (Math.Atan2(y, x) + Math.PI * 2) % (Math.PI * 2);
}

public static double DegreesToRadians(double angle)
{
    return angle * Math.PI / 180.0d;
}
公共静态双轴承(IPointGps pt1、IPointGps pt2)
{
double x=Math.Cos(度数弧度(pt1.纬度))*Math.Sin(度数弧度(pt2.纬度))-Math.Sin(度数弧度(pt1.纬度))*Math.Cos(度数弧度(pt2.纬度))*Math.Cos(度数弧度(pt2.经度-pt1.经度));
double y=Math.Sin(度数弧度(pt2.经度-pt1.经度))*Math.Cos(度数弧度(pt2.纬度));
//Math.Atan2可以返回负值,0
Math.Sin()
和所有类似的方法都希望参数以弧度为单位,但纬度和经度以度为单位。在计算方向角之前,必须将IPointGps转换为弧度,或修改方向角计算,例如:

public static double Bearing(IPointGps pt1, IPointGps pt2)
{
    double x = Math.Cos(DegreesToRadians(pt1.Latitude)) * Math.Sin(DegreesToRadians(pt2.Latitude)) - Math.Sin(DegreesToRadians(pt1.Latitude)) * Math.Cos(DegreesToRadians(pt2.Latitude)) * Math.Cos(DegreesToRadians(pt2.Longitude - pt1.Longitude));
    double y = Math.Sin(DegreesToRadians(pt2.Longitude - pt1.Longitude)) * Math.Cos(DegreesToRadians(pt2.Latitude));

    // Math.Atan2 can return negative value, 0 <= output value < 2*PI expected 
    return (Math.Atan2(y, x) + Math.PI * 2) % (Math.PI * 2);
}

public static double DegreesToRadians(double angle)
{
    return angle * Math.PI / 180.0d;
}
公共静态双轴承(IPointGps pt1、IPointGps pt2)
{
double x=Math.Cos(度数弧度(pt1.纬度))*Math.Sin(度数弧度(pt2.纬度))-Math.Sin(度数弧度(pt1.纬度))*Math.Cos(度数弧度(pt2.纬度))*Math.Cos(度数弧度(pt2.经度-pt1.经度));
double y=Math.Sin(度数弧度(pt2.经度-pt1.经度))*Math.Cos(度数弧度(pt2.纬度));

//Math.Atan2可以返回负值,0不,我从文件中读取了这些值,但它们存储在双精度中。不,我从文件中读取了这些值,但它们存储在双精度中。Daaaaaaaaaaaaaaaaaaaaaaaaaaamn!非常感谢!结果转换为真方向角还是相对方向角?Daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaamn!非常感谢!真的吗sult转换为真方位还是相对方位?