## 1. Overview

In this tutorial, we study the definition and usage of the haversine formula for calculating the distance between two points on a spherical surface.

We start by discussing the problem of measuring distances between points in Cartesian and polar coordinate systems.

Then, we extend the polar system to a spherical coordinate system. In that context, we’ll define the haversine formula for computing geographical distances as an instantiation of the general problem of great circle distances between points on a sphere.

At the end of this tutorial, we’ll know how to implement the haversine formula for calculating approximate distances between points on the Earth’s surface.

## 2. Why Do We Need Haversine Formula?

### 2.1. An Intuitive Understanding of Great Circles

Calculating great circle distances is a typical function in any geospatial information system. This function lets us determine the shortest distance required to travel along Earth’s surface in order to reach one location when departing from another:

The haversine formula is frequently used for this purpose, as it gives us a way to relate latitudes and longitudes to great circle distances.

### 2.2. What Are Great Circle Distances?

The most important concept for the haversine formula is that of a great circle in a sphere. **We can think of the great circles as the intersections between some particular planes and a spherical surface**. A sphere, whose surface is spherical, can be intersected by a plane in an infinite number of ways:

Of these planes, **only a subset, however, also intersects the center of the sphere**. If a plane intersects the center of the sphere, we call a great circle the collection of points that comprise the intersection with that sphere’s surface:

**When we have two points on a spherical surface, we can measure their distance as the length of the path that joins them in the only great circle that goes through them**. This path is also called the geodesic that connects the two points, and it’s the shortest path that’s bound to the spherical surface itself.

### 2.3. But Isn’t the Earth Flat?

If we’re moving in a three-dimensional flat space, we don’t need to consider great circles when calculating distances. We can, in fact, simply measure the length of the direct line connecting the two points and consider that as the shortest distance required to reach one from the other:

This type of distance is called Euclidean distance, and it’s appropriate for flat surfaces. The fact that Euclidean distances well approximate real-world distances on Earth for short scales is, of course, a good argument in favor of the idea that Earth is mostly flat. If, however, we consider larger scales, our job might become more complex and our Euclidean approximation less good as the distance between points increases.

### 2.4. Not Only for Earth’s Surface

There are, however, other situations, in addition to the travel across Earth’s surface, when we may have to use great circle distances.

**We may, for example, be restricted to travel along paths with constant gravitational potential**, as is the case when orbiting a body. This is the case if we’re in a geostationary orbit around Earth, and therefore we’re bound to a surface of uniform gravitational potential:

Another similar situation may arise if we conduct flight planning for airline routes. The route for an aircraft includes the altitude that an airplane has to maintain during a section of the travel path. **When the altitude is constant, we can say that the movement takes place on a great circle around Earth**:

That circle isn’t located on the surface of the Earth but rather on a spherical surface that has the same center and larger radius as Earth’s.

In practice, though, great circles often relate to maintaining the same distance from the center of the Earth and therefore have something to do with gravity. However, even if we’re not bound by gravitational constraints, we might still be traveling along a great circle for other reasons.

Consider, for example, the motion of electrons around an atom:

The force that generates the curved motion is electromagnetic, not gravitational, and yet the resulting motion takes place along a great circle around the nucleus. **In general, if the motion we’re studying relates to movement across a spherical surface, then we can apply the haversine formula to calculate its length**.

### 2.5. What Should a Distance Formula Contain?

We can now draft some expectations about the content of a formula for calculating distances over great circles in a spherical surface.

We know that the radius of the sphere somehow matters, and therefore that the greater the radius of a sphere, the larger the distance to travel between any two points of its surface.

**This, therefore, tells us that the distance should be a monotonically increasing function of the radius of the sphere**. If is the radius and is the distance as a function of , we can thus say that .

We can also imagine measuring the degenerate distance between two points that correspond to one another, . By definition the distance is expected to be zero, for any value assumed by . **There is, therefore, an operator which equals zero when the two points and correspond**, and which multiplies the radius in the formula for , such that .

The operator is thus lower-bound by 0. Further, because the distance between two points on a spherical surface is maximum if the two points are opposite with respect to the center, **the operator is also upper bound**. Because we know that multiplies in ‘s formula, and because the distance is upper bound by , this means that if and are opposite.

This is therefore the list of constraints to a great circle distance formula that we discussed so far:

The haversine formula satisfies all of these requirements. In the next section, we’ll see how to derive it from considerations regarding distances in flat spaces and then from the application of trigonometric laws to curved spaces.

## 3. Distances on Flat and Spherical Surfaces

### 3.1. Euclidean Distance

In flat surfaces, we normally calculate distances between points by using the norm of the vectors that represent the two points. The norm is what we earlier called the Euclidean distance.

In bidimensional surfaces, if and are two points, we can use Pythagora’s equivalence to calculate the distance as :

More in general, if the vector space is -dimensional, we can calculate distances as , where is the -th component of .

### 3.2. Distance by Cosine

If, however, the bidimensional plane is polar, one of the two components of the point-vector no longer indicates distances; but rather, an angle:

For this reason, the previous formula doesn’t apply to polar coordinate systems. If we define the points and , we can then compute their distance as .

**For a surface with positive curvature, however, this is inapplicable**. The considerations we made so far are, in fact, valid only for distances calculated on planar surfaces but are not generally valid otherwise.

For this reason, we will now see how to extend the polar coordinate system to the three dimensions first, and then how to calculate distances in the new system.

## 4. Haversine Formula

### 4.1. Three-Dimensional Polar Coordinate Systems

There are two main methods for extending a polar coordinate system to the third dimension.

**The first is to consider the additional, third dimension, as the distance from an arbitrarily selected plane, orthogonal to the axis of the planar polar system**:

This system is called a cylindric coordinate system. The other option, which is the one that we use in geospatial analysis, takes instead the name of a spherical coordinate system.

**A spherical coordinate system is a polar system in which the extra dimension is also polar**. In it, a spherical surface is defined as the set of points with the constant radial distance from the origin:

In a spherical coordinate system, we can also define great circles as the set of points located at the intersection between a plane passing through the origin, and the spherical surface itself, as we did earlier:

In spherical coordinate systems, distances between two points on a spherical surface are normally defined as great circle distances. This means that we measure it as the length of the arc of the great circle that comprises the two points. Since only one great circle passes through any given pair of points on a spherical surface, this distance is unique.

### 4.2. Definition of the Haversine Formula

We can now define the formula of haversine for calculating the distance between two points in the spherical coordinate system. The formula itself is simple, and it works for any pair of points that are defined according to their radial coordinates for a given radius:

Let’s define the two points and according to their respective latitudes and longitude , as and , respectively, expressed in radians. Let’s also define the great circle distance between and as .

Since is an arc of a great circle with radius and central angle , we know that . In this system, the law of haversine corresponding to the central angle is:

Once we compute for the pair of points , we can then calculate by using the inverse haversine function . If we do that, we can finally calculate as:

In its computation, we then simply replace with its full expression defined above, in terms of the latitudes and longitudes of the points and .

### 4.3. The Earth Isn’t Spherical Though

This equation is parametric for the radius of the spherical surface. **In the case of coordinates on Earth’s surface, which isn’t perfectly spherical, it’s common to use an approximate measure for the radius**. This is because the radius of the surface varies between and , so an average value is needed.

In general, the value of is the most commonly used. However, if we have prior knowledge that the two points are placed closer to the poles or the equator, we might then consider replacing it with or , respectively.

## 5. Conclusion

In this article, we studied the haversine formula for calculating great circle distances in spherical surfaces.

We first studied the rationale behind the formula by identifying examples of motions that take place along great circles.

Then, we discussed the problem of calculating distances in the Cartesian and polar coordinate system.

Finally, we generalized the problem of calculating distances to polar spherical systems. In that context, we then studied the haversine formula for great circle distances on spherical surfaces.