C# C语言中的轴承计算#
我想计算两个GPS位置之间的方位角,我愚弄了我的算法建议: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
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°
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可以返回负值,0Math.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转换为真方位还是相对方位?