Fórmula Haversine para encontrar la distancia entre dos puntos en una esfera

La fórmula de Haversine calcula la distancia más corta entre dos puntos en una esfera utilizando sus latitudes y longitudes medidas a lo largo de la superficie. Es importante para su uso en la navegación. El haversine se puede expresar en función trigonométrica como: 
haversine(\theta)=sin^2\Big(\frac{\theta}{2}\Big)
El haversine del ángulo central (que es d/r) se calcula mediante la siguiente fórmula:
\largehaversine\Big(\frac{d}{r}\Big)=haversine(\Phi_2-\Phi_1)+ cos(\Phi_1)cos(\Phi_2)haversine(\lambda_2-\lambda_1)
donde r es el radio de la tierra (6371 km), d es la distancia entre dos puntos ,  \phi_1, \phi_2    es la latitud de los dos puntos, y  \lambda_1, \lambda_2    es la longitud de los dos puntos respectivamente.
Resolviendo d aplicando el haversine inverso o usando la función seno inversa, obtenemos: 
 d = r hav^{-1}(h) = 2r sin^{-1}(\sqrt{h})

o

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)
The distance between Big Ben in London (51.5007° N, 0.1246° W) and The Statue of Liberty in 
New York (40.6892° N, 74.0445° W) is 5574.8 km. This is not the exact measurement because the 
formula assumes that the Earth is a perfect sphere when in fact it is an oblate spheroid.
Below is the implementation of the above formulae:  

C++

// C++ program for the haversine formula
// C++ program for the
// haversine formula
#include <iostream>
#include <cmath>
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

<?php
// PHP program for the
// haversine formula
 
function haversine($lat1, $lon1,
                   $lat2, $lon2)
{
    // distance between latitudes
    // and longitudes
    $dLat = ($lat2 - $lat1) *
                M_PI / 180.0;
    $dLon = ($lon2 - $lon1) *
                M_PI / 180.0;
 
    // convert to radians
    $lat1 = ($lat1) * M_PI / 180.0;
    $lat2 = ($lat2) * M_PI / 180.0;
 
    // apply formulae
    $a = pow(sin($dLat / 2), 2) +
         pow(sin($dLon / 2), 2) *
             cos($lat1) * cos($lat2);
    $rad = 6371;
    $c = 2 * asin(sqrt($a));
    return $rad * $c;
}
 
// Driver code
$lat1 = 51.5007;
$lon1 = 0.1246;
$lat2 = 40.6892;
$lon2 = 74.0445;
 
echo haversine($lat1, $lon1,
               $lat2, $lon2) .
                      " K.M.";
                 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>

Javascript

<script>
 
// Javascript program for the haversine formula
     
    function haversine(lat1, lon1, lat2, lon2)
    {
        // distance between latitudes
        // and longitudes
        let dLat = (lat2 - lat1) * Math.PI / 180.0;
        let dLon = (lon2 - lon1) * Math.PI / 180.0;
           
        // convert to radiansa
        lat1 = (lat1) * Math.PI / 180.0;
        lat2 = (lat2) * Math.PI / 180.0;
         
        // apply formulae
        let a = Math.pow(Math.sin(dLat / 2), 2) +
                   Math.pow(Math.sin(dLon / 2), 2) *
                   Math.cos(lat1) *
                   Math.cos(lat2);
        let rad = 6371;
        let c = 2 * Math.asin(Math.sqrt(a));
        return rad * c;
         
    }
     // Driver Code
     let lat1 = 51.5007;
     let lon1 = 0.1246;
     let lat2 = 40.6892;
     let lon2 = 74.0445;
     document.write(haversine(lat1, lon1, lat2, lon2) + " K.M.");
 
 
// This code is contributed by avanitrachhadiya2155
</script>
Producción: 

5574.840456848555 K.M.

 

Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)

Publicación traducida automáticamente

Artículo escrito por prakhar7 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *