Calculating Distance Between 2 Points

The Distance Formula Explained

Calculating the distance between two points is one of the most common tasks in math, programming, and real-world problem solving. Whether you're building a mapping app, designing a game, or just solving homework problems, you need to know which formula to use and when.

There are three main ways to measure distance: Euclidean, Manhattan, and Haversine. Each serves a different purpose. Using the wrong one will give you garbage results.

Euclidean Distance: The Straight Line

Euclidean distance is what most people think of when they hear "distance." It's the shortest path between two points in a straight line. This is the classic Pythagorean theorem in action.

The Formula (2D)

For two points (x₁, y₁) and (x₂, y₂):

d = √[(x₂ - x₁)² + (y₂ - y₁)²]

Simple. Take the difference in x-coordinates, square it. Take the difference in y-coordinates, square it. Add them together, then take the square root.

The Formula (3D)

Adding the z-axis doesn't change anything:

d = √[(x₂ - x₁)² + (y₂ - y₁)² + (z₂ - z₁)²]

This works for any number of dimensions. Just keep adding squared differences.

Manhattan Distance: Block by Block

Manhattan distance measures distance the way you'd walk through a city grid. You can only move horizontally or vertically, not diagonally. Think of it as the sum of absolute differences.

d = |x₂ - x₁| + |y₂ - y₁|

Also called "taxicab distance" or "L1 norm." Useful when movement is restricted to a grid—like robots on a floor, or calculating driving distance in a city with a proper grid layout.

Distance on a Sphere: Haversine Formula

Here's where most beginners screw up. If you're calculating distance between geographic coordinates (latitude/longitude), Euclidean distance is useless. The Earth is a sphere. You need the Haversine formula.

This accounts for the curvature of the Earth. The math looks intimidating, but it's just a few steps:

a = sin²(Δφ/2) + cos(φ₁) · cos(φ₂) · sin²(Δλ/2)

c = 2 · atan2(√a, √(1-a))

d = R · c

Where φ is latitude, λ is longitude, and R is Earth's radius (6,371 km or 3,959 miles).

How to Calculate Distance: Step by Step

Let's walk through a real example using Euclidean distance.

Problem: Find the distance between points A(3, 4) and B(9, 12).

Step 1: Calculate differences

x₂ - x₁ = 9 - 3 = 6

y₂ - y₁ = 12 - 4 = 8

Step 2: Square the differences

6² = 36

8² = 64

Step 3: Add them together

36 + 64 = 100

Step 4: Take the square root

√100 = 10

Distance = 10 units. That's it.

For geographic coordinates, you need to convert degrees to radians first, then apply the Haversine formula. Don't skip the conversion—90 degrees ≠ 90 units of anything useful.

Formula Comparison

Formula Use Case Input Format Output
Euclidean Straight-line distance in 2D/3D space (x, y) or (x, y, z) coordinates Units matching input
Manhattan Grid-based movement, city blocks (x, y) coordinates Units matching input
Haversine Earth surface distance, GPS coordinates (lat, lon) in degrees Kilometers or miles

Code Examples

Python

Euclidean distance:

import math
def euclidean(p1, p2):
return math.sqrt(sum((a - b) ** 2 for a, b in zip(p1, p2)))

Haversine distance:

import math
def haversine(lat1, lon1, lat2, lon2):
R = 6371 # Earth radius in km
phi1, phi2 = math.radians(lat1), math.radians(lat2)
dphi = math.radians(lat2 - lat1)
dlambda = math.radians(lon2 - lon1)
a = math.sin(dphi/2)**2 + math.cos(phi1) * math.cos(phi2) * math.sin(dlambda/2)**2
return R * 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))

JavaScript

Euclidean distance:

const euclidean = (p1, p2) => Math.sqrt(p1.reduce((sum, a, i) => sum + (a - p2[i])**2, 0));

Haversine distance:

const haversine = (lat1, lon1, lat2, lon2) => {
const R = 6371;
const toRad = x => x * Math.PI / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat/2)**2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon/2)**2;
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
};

Common Mistakes to Avoid

Which Formula Do You Actually Need?

Building a game with free movement? Euclidean.

Calculating how far a taxi has to drive in a grid city? Manhattan.

Finding the distance between two cities using GPS coordinates? Haversine.

The formula you choose depends entirely on your coordinate system and movement constraints. Pick wrong, and your answer is useless—no matter how perfectly you execute the math.