What is the Manhattan distance?
The Manhattan distance is a distance metric between two points. It's the sum of the absolute differences between these points' coordinates. It's also known by other names:
- The taxicab distance;
- The city block distance; and
- The snake distance.
Picture this: you're in a city like New York or San Francisco, where the streets are neatly laid out in a grid. To get from point A to point B, you have to walk from intersection to intersection along the grid — you can't walk diagonally through the city's blocks. In mathematical terms, you're constrained to traveling along the city's coordinate axes, unlike a bird. The number of city blocks you'd have to walk is called the Manhattan distance, and you can probably already see where its other names — taxicab distance, city block distance, and snake distance — come from.
Compare this with the straight-line Euclidean distance. The Pythagorean theorem states that c = √(a² + b²). While this is true, it gives you the Euclidean distance. If you were to rewrite the Pythagorean theorem for the Manhattan distance, it would instead be simply c = a + b. The Manhattan distance is longer, and you can reach the destination by more than one path of equal length.
Why is the Manhattan distance used?
Beyond determining how far away that coffee shop is in New York City, the Manhattan distance has a surprising number of applications. Here are just a few:
- In chess, the distance between squares for a rook is measured with the Manhattan distance, as this piece can only travel horizontally and vertically.
- In data science and machine learning, the Manhattan distance is used in speech recognition and image processing.
- In molecular biology, the Manhattan distance is used to decide where to splice genes and other molecules.
- In the popular Snake video game genre, the number of steps your snake has to move towards the next food item can be determined with the Manhattan distance; this is how it earned the nickname "snake distance".
What is the formula for the Manhattan distance?
With two N-dimensional points a⃗ = [a₁, …, aₙ] and b⃗ = [b₁, …, bₙ], the Manhattan distance d can be calculated as:
d = |a₁ − b₁| + … + |aₙ − bₙ|
In two dimensions, with points P₁(x₁, y₁) and P₂(x₂, y₂), this simplifies to:
d = |x₂ − x₁| + |y₂ − y₁|
Let's see how to calculate the Manhattan distance for a real-world example set in real-world Manhattan. Manhattan's east-west roads are called "streets", and its north-south roads are called "avenues". Say you live on the corner of 9th Street and 2nd Avenue, making your starting point a⃗ = [2, 9]. You want to buy some milk at the grocery store on the corner of 5th Street and 3rd Avenue, so your destination is b⃗ = [3, 5]. Using the Manhattan distance formula, the total distance you'd have to walk along the streets of New York is:
- |3 − 2| + |5 − 9| = |1| + |−4| = 1 + 4 = 5 blocks.
By comparison, the straight-line (Euclidean) distance would be √(1² + 4²) = √17 ≈ 4.12 — shorter, but a route only a bird could take.
Metric vs. US (Imperial) Units
This calculator supports both measurement systems, and the Manhattan distance formula is unit-agnostic — the mathematics is identical regardless of units:
- Metric system: millimeters (mm), centimeters (cm), meters (m), kilometers (km). Used worldwide in science and most countries.
- US / Imperial system: inches (in), feet (ft), yards (yd), miles (mi). Commonly used in the United States.
Simply enter both points in the same unit, and the result will be expressed in that same unit.
How to use the Manhattan distance calculator
- Choose your unit system (metric or US) and the unit of measurement.
- Enter the coordinates of the first point: x₁ and y₁.
- Enter the coordinates of the second point: x₂ and y₂.
- Read off the Manhattan distance. The calculator also shows |Δx|, |Δy|, a full step-by-step solution, and the Euclidean distance for comparison.
Negative coordinates are perfectly fine — because we take absolute values, the Manhattan distance is always zero or positive, and the order of the two points never changes the result.
FAQs
- Can I enter negative coordinates?
- Yes. The Manhattan distance works for any real-number coordinates, including negatives. The result is always positive (or zero) because we sum absolute differences.
- Is the Manhattan distance ever equal to the Euclidean distance?
- Only when the two points share an x-coordinate or a y-coordinate (i.e., they lie on the same horizontal or vertical line). In every other case, the Manhattan distance is strictly larger.
- Does the order of the points matter?
- No. The distance from P₁ to P₂ equals the distance from P₂ to P₁, since we take the absolute value of each difference.
- What if both points are the same?
- The Manhattan distance is 0. If x₁ = x₂ and y₁ = y₂, then |Δx| = |Δy| = 0 and d = 0.
- How does this extend to more dimensions?
- Just keep adding absolute differences: in 3D, d = |x₂−x₁| + |y₂−y₁| + |z₂−z₁|. This calculator covers the common 2D case.