📜  计算球体上两点之间距离的Haversine公式

📅  最后修改于: 2021-10-23 08:21:08             🧑  作者: Mango

Haversine公式使用沿表面测量的纬度和经度计算球体上两点之间的最短距离。在导航中使用很重要。半正矢可以在函数的表达:
haversine(\theta)=sin^2\Big(\frac{\theta}{2}\Big)
中心角的半正弦值(即 d/r)由以下公式计算:
\largehaversine\Big(\frac{d}{r}\Big)=haversine(\Phi_2-\Phi_1)+ cos(\Phi_1)cos(\Phi_2)haversine(\lambda_2-\lambda_1)
其中 r 是地球的半径(6371 公里),d 是两点之间的距离, \phi_1, \phi_2  是两点的纬度,并且\lambda_1, \lambda_2  分别是两点的经度。
通过应用反半正弦或使用反正弦函数求解 d,我们得到:
d = r hav^{-1}(h) = 2r sin^{-1}(\sqrt{h})

要么

d = 2r sin^{-1}\bigg(\sqrt{sin^2\Big(\frac{\Phi_2-\Phi_1}{2}\Big)+cos(\Phi_1)cos(\Phi_2)sin^2\Big(\frac{\lambda_2-\lambda_1}{2}\Big)}\ \bigg)
伦敦大本钟 (51.5007° N, 0.1246° W) 和自由女神像之间的距离
纽约(北纬 40.6892°,西经 74.0445°)是 5574.8 公里。这不是准确的测量,因为
公式假定地球是一个完美的球体,而实际上它是一个扁球体。
下面是上述公式的实现:

C++
// C++ program for the haversine formula
// C++ program for the
// haversine formula
#include 
#include 
using namespace std;
 
static double haversine(double lat1, double lon1,
                        double lat2, double lon2)
    {
        // distance between latitudes
        // and longitudes
        double dLat = (lat2 - lat1) *
                      M_PI / 180.0;
        double dLon = (lon2 - lon1) *
                      M_PI / 180.0;
 
        // convert to radians
        lat1 = (lat1) * M_PI / 180.0;
        lat2 = (lat2) * M_PI / 180.0;
 
        // apply formulae
        double a = pow(sin(dLat / 2), 2) +
                   pow(sin(dLon / 2), 2) *
                   cos(lat1) * cos(lat2);
        double rad = 6371;
        double c = 2 * asin(sqrt(a));
        return rad * c;
    }
 
// Driver code
int main()
{
    double lat1 = 51.5007;
    double lon1 = 0.1246;
    double lat2 = 40.6892;
    double lon2 = 74.0445;
     
    cout << haversine(lat1, lon1,
                      lat2, lon2) << " K.M.";
    return 0;
}
 
// This code is contributed
// by Mahadev.


Java
// Java program for the haversine formula
public class Haversine {
 
    static double haversine(double lat1, double lon1,
                            double lat2, double lon2)
    {
        // distance between latitudes and longitudes
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
 
        // convert to radians
        lat1 = Math.toRadians(lat1);
        lat2 = Math.toRadians(lat2);
 
        // apply formulae
        double a = Math.pow(Math.sin(dLat / 2), 2) +
                   Math.pow(Math.sin(dLon / 2), 2) *
                   Math.cos(lat1) *
                   Math.cos(lat2);
        double rad = 6371;
        double c = 2 * Math.asin(Math.sqrt(a));
        return rad * c;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        double lat1 = 51.5007;
        double lon1 = 0.1246;
        double lat2 = 40.6892;
        double lon2 = 74.0445;
        System.out.println(haversine(lat1, lon1, lat2, lon2) + " K.M.");
    }
}


Python 3
# Python 3 program for the
# haversine formula
import math
 
# Python 3 program for the
# haversine formula
def haversine(lat1, lon1, lat2, lon2):
     
    # distance between latitudes
    # and longitudes
    dLat = (lat2 - lat1) * math.pi / 180.0
    dLon = (lon2 - lon1) * math.pi / 180.0
 
    # convert to radians
    lat1 = (lat1) * math.pi / 180.0
    lat2 = (lat2) * math.pi / 180.0
 
    # apply formulae
    a = (pow(math.sin(dLat / 2), 2) +
         pow(math.sin(dLon / 2), 2) *
             math.cos(lat1) * math.cos(lat2));
    rad = 6371
    c = 2 * math.asin(math.sqrt(a))
    return rad * c
 
# Driver code
if __name__ == "__main__":
    lat1 = 51.5007
    lon1 = 0.1246
    lat2 = 40.6892
    lon2 = 74.0445
     
    print(haversine(lat1, lon1,lat2, lon2), "K.M.")
 
# This code is contributed
# by ChitraNayal


C#
// C# program for the haversine formula
using System;
class GFG
{
 
static double haversine(double lat1, double lon1,
                        double lat2, double lon2)
{
    // distance between latitudes and longitudes
    double dLat = (Math.PI / 180) * (lat2 - lat1);
    double dLon = (Math.PI / 180) * (lon2 - lon1);
 
    // convert to radians
    lat1 = (Math.PI / 180) * (lat1);
    lat2 = (Math.PI / 180) * (lat2);
 
    // apply formulae
    double a = Math.Pow(Math.Sin(dLat / 2), 2) +
               Math.Pow(Math.Sin(dLon / 2), 2) *
               Math.Cos(lat1) * Math.Cos(lat2);
    double rad = 6371;
    double c = 2 * Math.Asin(Math.Sqrt(a));
    return rad * c;
}
 
// Driver Code
public static void Main()
{
    double lat1 = 51.5007;
    double lon1 = 0.1246;
    double lat2 = 40.6892;
    double lon2 = 74.0445;
    Console.WriteLine(haversine(lat1, lon1,
                                lat2, lon2) + " K.M.");
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
5574.840456848555 K.M.