Understanding the Manhattan distance calculator — concept and principle
This calculator finds the Manhattan (taxicab or city-block) distance between two points in 2-D or 3-D, the distance travelled when you may only move along grid lines. It compares it with the straight-line distance and counts the shortest grid routes. Use it for city navigation puzzles, grid games, warehouse layouts and data science.
How it is calculated
Manhattan distance = |x₁ − x₂| + |y₁ − y₂| (+ |z₁ − z₂| in 3-D)
Straight-line (Euclidean) distance = √(Δx² + Δy² + Δz²)
Shortest grid routes in 2-D with whole-number steps = C(|Δx| + |Δy|, |Δx|)
The principle
On a street grid you cannot cut diagonally across blocks, so you must cover the full east–west difference and the full north–south difference, in any order. That is why the distance is the sum of the absolute differences. Every shortest route uses the same number of east steps and north steps; only their order changes, so the number of routes is the number of ways to choose which steps go east. The straight line is never longer, and the two distances are equal only when the points differ in just one coordinate.
Worked example
Default inputs: A = (1, 2, 0), B = (7, 10, 0).
- |Δx| = |1 − 7| = 6; |Δy| = |2 − 10| = 8; |Δz| = 0.
- Manhattan distance = 6 + 8 + 0 = 14.
- Straight-line distance = √(36 + 64) = 10.
- Ratio Manhattan ÷ straight-line = 1.4.
- Shortest grid routes = C(14, 6) = 3,003.
| Point B: y₂ | Manhattan distance |
|---|---|
| 2.5 | 6.5 |
| 5 | 9 |
| 7.5 | 11.5 |
| 10 | 14 |
| 12.5 | 16.5 |
| 15 | 19 |
| 20 | 24 |
How to read the result
A ratio of 1.4 means travelling on the grid is 40 % longer than flying straight here. The ratio is largest, √2 ≈ 1.41 in 2-D, when the move is diagonal with equal steps. In data science the Manhattan distance is less influenced by a single large difference than the Euclidean distance.
Common mistakes
- Forgetting the absolute values, so differences cancel.
- Adding squares instead of absolute differences.
- Counting routes when steps are not whole grid units.
- Expecting the Manhattan distance to be shorter than the straight line.
Frequently asked questions
What is the Manhattan distance between (1, 2) and (7, 10)?
14.
Why is it called taxicab distance?
It is how far a taxi drives on a rectangular street grid.
How many shortest routes are there?
C(Δx + Δy, Δx); here C(14, 6) = 3,003.
Sources: E. W. Weisstein, "Taxicab Metric", Wolfram MathWorld