📜  地球上两点间距离的程序

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

给定纬度和经度(以度为单位),找出地球上两点之间的距离。

图片来源:维基百科
例子:

Input : Latitude 1: 53.32055555555556
        Latitude 2: 53.31861111111111
        Longitude 1: -1.7297222222222221
        Longitude 2: -1.6997222222222223
Output: Distance is: 2.0043678382716137 Kilometers

问题可以使用Haversine公式解决:

大圆距离或正交距离是球体(或地球表面)上两点之间的最短距离。为了使用这种方法,我们需要有A点和B点的坐标。选择大圆法而不是其他方法。
首先,将纬度和经度值从十进制度数转换为弧度。为此,将两个点的经度和纬度值除以 180/pi。 pi 的值为 22/7。 180/pi 的值约为 57.29577951。如果我们要计算两地之间的距离(以英里为单位),请使用值 3,即 963,即地球的半径。如果我们想以千米为单位计算两个地方之间的距离,请使用值 6,即 378.8,即地球的半径。

获取A点的经纬度坐标。使用上述转换方法将纬度和经度的值转换为弧度。我将其称为 lat1 和 long1。对 B 点的坐标做同样的事情,得到 lat2 和 long2。
现在,要获得 A 点和 B 点之间的距离,请使用以下公式:

获得的距离 d 以英里为单位。如果您希望您的值以公里为单位,则将 d 乘以 1.609344。
d 公里 = 1.609344 * d 英里
因此,您可以使用大圆距离方法获得地球上两个地方之间的最短距离。

C++
// C++ program to calculate Distance
// Between Two Points on Earth
#include 
using namespace std;
 
// Utility function for
// converting degrees to radians
long double toRadians(const long double °ree)
{
    // cmath library in C++
    // defines the constant
    // M_PI as the value of
    // pi accurate to 1e-30
    long double one_deg = (M_PI) / 180;
    return (one_deg * degree);
}
 
long double distance(long double lat1, long double long1,
                     long double lat2, long double long2)
{
    // Convert the latitudes
    // and longitudes
    // from degree to radians.
    lat1 = toRadians(lat1);
    long1 = toRadians(long1);
    lat2 = toRadians(lat2);
    long2 = toRadians(long2);
     
    // Haversine Formula
    long double dlong = long2 - long1;
    long double dlat = lat2 - lat1;
 
    long double ans = pow(sin(dlat / 2), 2) +
                          cos(lat1) * cos(lat2) *
                          pow(sin(dlong / 2), 2);
 
    ans = 2 * asin(sqrt(ans));
 
    // Radius of Earth in
    // Kilometers, R = 6371
    // Use R = 3956 for miles
    long double R = 6371;
     
    // Calculate the result
    ans = ans * R;
 
    return ans;
}
 
// Driver Code
int main()
{
    long double lat1 = 53.32055555555556;
    long double long1 = -1.7297222222222221;
    long double lat2 = 53.31861111111111;
    long double long2 = -1.6997222222222223;
     
    // call the distance function
    cout << setprecision(15) << fixed;
    cout << distance(lat1, long1,
                     lat2, long2) << " K.M";
 
    return 0;
}
 
// This code is contributed
// by Aayush Chaturvedi


Java
// Java program to calculate Distance Between
// Two Points on Earth
import java.util.*;
import java.lang.*;
 
class GFG {
 
    public static double distance(double lat1,
                     double lat2, double lon1,
                                  double lon2)
    {
 
        // The math module contains a function
        // named toRadians which converts from
        // degrees to radians.
        lon1 = Math.toRadians(lon1);
        lon2 = Math.toRadians(lon2);
        lat1 = Math.toRadians(lat1);
        lat2 = Math.toRadians(lat2);
 
        // Haversine formula
        double dlon = lon2 - lon1;
        double dlat = lat2 - lat1;
        double a = Math.pow(Math.sin(dlat / 2), 2)
                 + Math.cos(lat1) * Math.cos(lat2)
                 * Math.pow(Math.sin(dlon / 2),2);
             
        double c = 2 * Math.asin(Math.sqrt(a));
 
        // Radius of earth in kilometers. Use 3956
        // for miles
        double r = 6371;
 
        // calculate the result
        return(c * r);
    }
 
    // driver code
    public static void main(String[] args)
    {
        double lat1 = 53.32055555555556;
        double lat2 = 53.31861111111111;
        double lon1 = -1.7297222222222221;
        double lon2 = -1.6997222222222223;
        System.out.println(distance(lat1, lat2,
                           lon1, lon2) + " K.M");
    }
}
 
// This code is contributed by Prasad Kshirsagar


Python3
# Python 3 program to calculate Distance Between Two Points on Earth
from math import radians, cos, sin, asin, sqrt
def distance(lat1, lat2, lon1, lon2):
     
    # The math module contains a function named
    # radians which converts from degrees to radians.
    lon1 = radians(lon1)
    lon2 = radians(lon2)
    lat1 = radians(lat1)
    lat2 = radians(lat2)
      
    # Haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
 
    c = 2 * asin(sqrt(a))
    
    # Radius of earth in kilometers. Use 3956 for miles
    r = 6371
      
    # calculate the result
    return(c * r)
     
     
# driver code
lat1 = 53.32055555555556
lat2 = 53.31861111111111
lon1 = -1.7297222222222221
lon2 =  -1.6997222222222223
print(distance(lat1, lat2, lon1, lon2), "K.M")


C#
// C# program to calculate
// Distance Between Two
// Points on Earth
using System;
 
class GFG
{
    static double toRadians(
           double angleIn10thofaDegree)
    {
        // Angle in 10th
        // of a degree
        return (angleIn10thofaDegree * 
                       Math.PI) / 180;
    }
    static double distance(double lat1,
                           double lat2,
                           double lon1,
                           double lon2)
    {
 
        // The math module contains
        // a function named toRadians
        // which converts from degrees
        // to radians.
        lon1 = toRadians(lon1);
        lon2 = toRadians(lon2);
        lat1 = toRadians(lat1);
        lat2 = toRadians(lat2);
 
        // Haversine formula
        double dlon = lon2 - lon1;
        double dlat = lat2 - lat1;
        double a = Math.Pow(Math.Sin(dlat / 2), 2) +
                   Math.Cos(lat1) * Math.Cos(lat2) *
                   Math.Pow(Math.Sin(dlon / 2),2);
             
        double c = 2 * Math.Asin(Math.Sqrt(a));
 
        // Radius of earth in
        // kilometers. Use 3956
        // for miles
        double r = 6371;
 
        // calculate the result
        return (c * r);
    }
 
    // Driver code
    static void Main()
    {
        double lat1 = 53.32055555555556;
        double lat2 = 53.31861111111111;
        double lon1 = -1.7297222222222221;
        double lon2 = -1.6997222222222223;
        Console.WriteLine(distance(lat1, lat2,
                          lon1, lon2) + " K.M");
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP


Javascript


输出:

2.0043678382716137 K.M

参考:维基百科

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程