Calculating the distance between two points is a fundamental operation in programming, used in everything from game development (collision detection) to logistics (route planning). However, the method you choose depends heavily on the context: are you working on a 2D Cartesian plane (like a screen or map projection) or a spherical surface (like the Earth)?
This guide covers how to calculate Euclidean distance for flat surfaces and Geodesic distance (using the Haversine formula) for geographical coordinates (Latitude/Longitude).
Euclidean Distance (2D/3D Planes)
For flat surfaces (like a game grid) or 3D space, the distance between two points (x_1, y_1) and (x_2, y_2) is calculated using the Pythagorean theorem: d = sqrt((x_2 - x_1)^2 + (y_2 - y_1)^2).
Using the math Module
Python's math module provides straightforward tools for this. While you can write the formula manually, math.hypot (hypotenuse) is cleaner and often faster.
import math
p1 = (5, 10)
p2 = (8, 14)
dist_manual = math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
print(f"Manual Calc: {dist_manual}")
dist_clean = math.dist(p1, p2)
print(f"Clean Calc: {dist_clean}")Output:
Manual Calc: 5.0
Clean Calc: 5.0Geographic Distance (Latitude/Longitude)
You cannot use Euclidean distance for latitude and longitude. Earth is a sphere (oblate spheroid), meaning the distance represented by one degree of longitude shrinks as you move from the Equator to the Poles.
The Haversine Formula
To calculate the "Great Circle" distance (the shortest path over the earth's surface), we use the Haversine formula.
import math
def haversine_distance(lat1, lon1, lat2, lon2):
R = 6371.0
lat1_rad, lon1_rad = math.radians(lat1), math.radians(lon1)
lat2_rad, lon2_rad = math.radians(lat2), math.radians(lon2)
dlat = lat2_rad - lat1_rad
dlon = lon2_rad - lon1_rad
a = math.sin(dlat / 2)**2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return R * c
ny = (40.7128, -74.0060)
lon = (51.5074, -0.1278)
distance = haversine_distance(ny[0], ny[1], lon[0], lon[1])
print(f"Distance: {distance:.2f} km")Output:
Distance: 5570.22 kmUsing External Libraries (geopy)
For production applications, avoid writing your own Haversine implementation. Edge cases (like the Earth not being a perfect sphere) are handled better by established libraries like geopy.
Installation:
pip install geopyfrom geopy.distance import geodesic
coords_1 = (40.7128, -74.0060) # New York
coords_2 = (51.5074, -0.1278) # London
distance_km = geodesic(coords_1, coords_2).kilometers
distance_miles = geodesic(coords_1, coords_2).miles
print(f"Distance: {distance_km:.2f} km")
print(f"Distance: {distance_miles:.2f} miles")Output:
Distance: 5585.23 km
Distance: 3470.50 milesHigh Performance with NumPy
If you need to calculate distances between thousands of points (e.g., finding the nearest neighbor in a dataset), Python loops are too slow. Use numpy for vectorized calculations.
import numpy as np
p1 = np.array([1, 2, 3])
p2 = np.array([4, 5, 6])
dist = np.linalg.norm(p1 - p2)
print(f"NumPy Distance: {dist:.4f}")Output:
NumPy Distance: 5.1962Conclusion
- 2D/3D Planes: Use
math.dist(p1, p2)for simplicity. - Geolocation (Quick): Use the Haversine formula if you cannot install external dependencies.
- Geolocation (Accurate): Use
geopy.distance.geodesicfor the most accurate, production-ready results. - Large Datasets: Use
numpy.linalg.normfor performance.