简体   繁体   English

缩短此代码,确定C#中两个纬度/长度之间的距离?

[英]Shorten this code that determines distance between two lat/longs in C#?

public static double Distance(LatLong from, LatLong to)
{
    double lat1 = from.Latitude * (Math.PI / 180.0);
    double lat2 = to.Latitude * (Math.PI / 180.0);

    return
        Math.Acos((Math.Sin(lat1) * Math.Sin(lat2)) +
        (Math.Cos(lat1) * Math.Cos(lat2) *
        Math.Cos((Math.PI / 180.0) * (to.Longitude - from.Longitude)))) * 3958.760;
}

Can you shorten this code any stretch? 你可以缩短这段代码吗? I'm just wondering ... 我是在想 ...

That's the standard spherical law of cosines formula. 这是余弦公式的标准球面定律。 You won't get it any simpler than that. 你不会比这更简单。 At best, you could clean up the code a little: 充其量,您可以稍微清理一下代码:

public static double Distance(LatLong from, LatLong to)
{
    double deg = Math.PI / 180.0;       // One degree in radians
    double lat1 = from.Latitude * deg;
    double lat2 = to.Latitude * deg;
    double dLng = (to.Longitude - from.Longitude) * deg;
    double R = 3958.760;

    return Math.Acos(Math.Sin(lat1) * Math.Sin(lat2) +
                     Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(dLng)) * R;
}

No, but I can offer a shorter, faster way, but far less accurate way to obtain relative distances: 不,但我可以提供更短,更快的方式,但获得相对距离的准确方法要差得多:

public static double RelativeDistance(LatLong from, LatLong to)
{
  return (from.Latitude - to.Latitude) * (from.Latitude - to.Latitude) + (from.Longitude - to.Longitude) * (from.Longitude - to.Longitude);
}

This returns a value relative to the square of the distance in terms of the projection of coordinates unto a square 2D grid (as if the world were a 2:1 rectangle). 这将返回一个相对于距离平方的值,就坐标投影到方形2D网格而言(就好像世界是2:1的矩形)。 It's so useless for real distances that I wouldn't even bother to take a square root to bring it back to being proportional to the projection (since the projection is silly), but what it can serve for is rapidly sorting by relative distances within such a small area (and far enough from the poles) that the gross inaccuracy doesn't matter much. 它对于真正的距离是如此无用,我甚至懒得拿一个平方根使其恢复到与投影成比例(因为投影是愚蠢的),但它可以服务的是通过这样的相对距离快速排序一个小区域(远离两极),严重不准确无关紧要。

Hence, it won't help you calculate your fuel costs, but it will help you work out which pub is (probably) nearest. 因此,它不会帮助您计算您的燃料成本,但它将帮助您确定哪个酒吧(可能)最近。 If you wanted to sort by relative distance to a given point, it could serve well and its speed be a boon. 如果你想按给定点的相对距离排序,它可以很好地服务,它的速度是一个福音。 Outside of that use, it's pointless. 除此之外,它毫无意义。

The formula looks like it is computing the distance along the surface of a sphere, and would thus be reasonably accurate even for points that were practically on opposite sides of the world. 该公式看起来像计算沿着球体表面的距离,因此即使对于实际上位于世界相对侧的点也是合理准确的。 If the distances will be very close together, you could approximate it by projecting the points onto the surface of a cylinder (coaxial with the Earth) passing through one of the points; 如果距离非常接近,你可以通过将点投射到通过其中一个点的圆柱体(与地球同轴)的表面上来近似它; scale the cylinder so north/south and east/west distances on the cylinder match those on the Earth. 缩放圆柱体,使圆柱体的北/南和东/西距离与地球上的距离相匹配。 This will simply require taking the cosine of one of the latitudes. 这只需要取其中一个纬度的余弦。 Note that if the points are far enough apart that it matters which point's latitude you use, they are too far apart for this to be a good approximation, but for small distances this approach is quick and easy. 请注意,如果这些点相距足够远以至于您使用哪个点的纬度很重要,那么它们距离太近以至于不能很好地近似,但对于小距离,这种方法快速而简单。

Note, btw, that something like a conical projection will be accurate over wider distances, but will also require more calculation; 请注意,顺便说一下,像锥形投影这样的东西在更远的距离上是准确的,但也需要更多的计算; if one is going to that much trouble, one may as well use the 'right' calculations. 如果一个人遇到那么多麻烦,也可以使用“正确”的计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM