license:mit region:us
---
license: mit
---

# Haversine Distance

Consider the Earth as a perfect sphere. If we wanted to measure the distance between two points on this sphere, we would simply draw a straight line between the coordinates φ1λ1 and φ2λ2, right?

Actually, not exactly. When trying to calculate this distance, we should keep in mind the angle formed by the sphere's radius, which is represented by the unit θ. By ignoring this angle, we would consider the Earth as a flat surface. Recognizing this complexity, some mathematicians developed the Haversine Formula over the years based on astronomical observations. This formula calculates the shortest distance between two points on a sphere using its radius. Nowadays, it is widely used in navigation and geolocation applications, as it allows for precise calculations of distances on the Earth's surface.

![Image 1: Graphical representation of the trigonometric functions.](img/1697580080732.png)

> φ1λ1 represents the latitude and longitude of the first coordinate, and φ2λ2 represents the latitude and longitude of the second coordinate.

To understand the concept of Haversine, visualize a sphere and a triangle inscribed in it, whose vertex is located at the center of the sphere (O) and whose base connects the points φ1λ1 and φ2λ2 on the surface of the sphere. From O, draw a segment that extends to touch the surface of the sphere at a specific angle, known as the central angle (θ). The distance between the center of the sphere (O) and the point where this segment touches the surface is related to the Versine function.

The Haversine is an essential tool for calculating the orthodromic distance, which is the shortest distance between two points on the surface of a sphere. This distance is determined between the points φ1λ1 and φ2λ2 and is expressed using the trigonometric function of the sine. Essentially, the haversine(θ) is half of a versine(θ). The reason for using the Haversine instead of the Versine is that it provides more accurate calculations for short distances. When the distance between φ1λ1 and φ2λ2 is very small, the value of versine(θ) approaches 1, becoming less precise. The Haversine, on the other hand, remains effective in representing that small distance accurately.

> Essentially, the Haversine is half of a Versine.

Given this explanation, the central angle (θ) is given by the relationship of the distance between the two points on the sphere divided by the radius of the sphere:

```python
def central_angle(d, r):
    """
    Calculates the central angle of a circle given the distance and
    radius.

    Args:
        d (float): Distance
        r (float): Radius

    Returns:
        float: Central angle
    """
    return d / r
```

Where:
- d is the distance between the two points on the sphere;
- r is the radius of the sphere.

You can see in Image 1 that the value of versine(θ) is 1-cos(θ) on the trigonometric circle, and this equates to 2sin^2(θ/2) using the trigonometric identities. As previously mentioned, the haversine(θ) will be exactly half of this (sin^2(θ/2)):

```python
import math

def hav(θ):
    """
    Calculates the haversine of an angle.

    Args:
        θ (float): Angle in radians

    Returns:
        float: Haversine of angle.
    """
    return pow(math.sin(θ / 2), 2)
```

As we need the haversine(θ) in a spherical system, it will be necessary to bring in the coordinates of the points φ1λ1 and φ2λ2 and calculate the haversine of the central difference between the two geographic points:

```python
def haversine_difference(φ1, φ2, λ1, λ2):
    """
    Calculates the haversine of the central difference between
    two geographic points.

    Args:
        φ1 (float): Latitude of first point in radians.
        φ2 (float): Latitude of second point in radians.
        λ1 (float): Longitude of first point in radians.
        λ2 (float): Longitude of second point in radians.

    Returns:
        float: Haversine of the central difference.
    """
    return hav(φ2 - φ1) + (math.cos(φ1) * math.cos(φ2) * hav(λ2 - λ1))
```

Where:
- φ1 and φ2 are the latitudes of each point in radians;
- λ1 and λ2 are the longitudes of each point in radians.

Now that we have the main tools to calculate the central angle and the distance of the geographic points, we can compute the distance between the two points on the coordinate using the inverse sine function (arcsin):

```python
import math

def haversine(φ1, φ2, λ1, λ2, rad=6371):
    """
    Calculates the distance between two points on the Earth's
    surface given their latitude and longitude in degrees.

    Args:
        φ1 (float): Latitude of first point in degrees.
        φ2 (float): Latitude of second point in degrees.
        λ1 (float): Longitude of first point in degrees.
        λ2 (float): Longitude of second point in degrees.
        rad (int): Radius of the Earth in the desired units (default is 6371 km).

    Returns:
        float: Distance between the two points in the units corresponding to the provided Earth's radius.
    """

    φ1, φ2 = math.radians(φ1), math.radians(φ2)
    λ1, λ2 = math.radians(λ1), math.radians(λ2)

    central_angle_hav = haversine_difference(φ1, φ2, λ1, λ2)

    return 2 * rad * math.asin(math.sqrt(central_angle_hav))
```

Where:
- φ1 and φ2 are the latitudes of each point in radians;
- λ1 and λ2 are the longitudes of each point in radians;
- rad is the radius of the perfect sphere.

Note:

Notice that we are converting the angles of the coordinates into radians, as we are computing trigonometric functions in the radian system.

## Real-world Applications

The ability to accurately calculate the distance between two points on the Earth's surface has various uses, such as: GPS navigation, logistics and transport, aviation, geological studies, environmental research, tourism, and more.

## Some Considerations

As many of us know, Earth does not have the exact shape of a perfect sphere; it more closely resembles an oblate spheroid, with significant variations due to terrain, gravity, among other factors. The "Haversine distance" method provides us with an approximation of the real distance between two points on the Earth's surface and is sufficiently accurate for many applications that don't require extreme precision. If we need more accurate calculations, we can turn to other methods and formulas, like the Vincenty formula, and in even more specific situations, the Earth Gravitational Model (EGM).
