[英]Weird output in bearing between two coordinate calculation
我有一种方法可以用来计算两个地理坐标之间的方位(示例格式为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;
}
}
谁能看到这个问题? 我查看了一下,却找不到问题。
%
在此行中看起来像鱼:
double heading = AdditionalMath.ToDeg(exMath.Atan2(partOne, partTwo) % 2 * exMath.PI);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.