The Distance Formula Is Pythagoras
The straight-line distance between two points is the hypotenuse of a right triangle whose legs are the coordinate differences:
d = √((x₂ − x₁)² + (y₂ − y₁)²)
In three dimensions a z term joins under the root. The pattern extends to any number of dimensions, which is how distance is measured in machine learning feature spaces with hundreds of them.
Distance Formula Chart
| Type | Formula | Used for |
|---|---|---|
| 2D Euclidean | √(Δx² + Δy²) | Plane geometry, screen coordinates |
| 3D Euclidean | √(Δx² + Δy² + Δz²) | Space, physics, 3D graphics |
| Haversine | Great-circle on a sphere | Latitude and longitude |
| Manhattan | |Δx| + |Δy| | Grid movement, city blocks |
Distance on a Sphere
Coordinates on the Earth cannot use the flat formula — a degree of longitude is 111 km at the equator and nearly zero at the poles. The haversine formula computes the great-circle distance, the shortest path across the surface:
a = sin²(Δφ/2) + cos φ₁ × cos φ₂ × sin²(Δλ/2)
d = 2R × atan2(√a, √(1−a))
Using a mean Earth radius of 6,371 km, it is accurate to about 0.5% — the residual error coming from the Earth being an oblate spheroid rather than a sphere.
Why Flight Paths Look Curved
The shortest route between two points on a globe is a great circle, and on a Mercator projection a great circle appears as a curve. A flight from London to Los Angeles passing over Greenland is taking the direct route, not a detour.
Manhattan Distance
Where movement is restricted to a grid, the diagonal is not available and distance is the sum of the axis differences. It applies to city blocks, chip layouts, warehouse routing and certain clustering algorithms. It is never shorter than the Euclidean distance and can be up to √2 times longer.
Frequently Asked Questions
Does the order of the points matter?
No. The differences are squared, so the signs disappear and distance is symmetric.
How accurate is haversine for real journeys?
It gives the great-circle distance, which is a lower bound. Actual road distances typically run 20–40% longer, and flight distances slightly longer than the great circle because of airways and weather routing.
What is Vincenty's formula?
A more accurate method that models the Earth as an ellipsoid, achieving sub-millimetre precision. Haversine is simpler and sufficient for almost all practical purposes.