Euclidean Distance: Formula for 2D, 3D, and Vectors
Calculate Euclidean distance with worked examples in two, three, and more dimensions. Compare Manhattan distance and feature scaling.
TL;DR
Euclidean distance is the straight-line distance between two points. Subtract matching coordinates, square the differences, add them, and take the nonnegative square root. The same procedure works in any finite dimension.
Key Points
- In two dimensions it is the hypotenuse of a right triangle.
- In three dimensions and higher, include one squared difference per coordinate.
- Squared Euclidean distance gives the same nearest-point ranking, but it fails the triangle inequality.
- Manhattan distance sums absolute coordinate differences and can rank points differently.
- Put physical lengths in common units, or use a stated scaling rule for different kinds of features.
What is Euclidean distance?
Euclidean distance measures the length of the straight segment from one point to another. In two dimensions, the horizontal and vertical changes are perpendicular legs. The coordinate distance formula follows from the Pythagorean theorem.
For example, if the coordinate differences are 3 and 4, the distance is . That segment measures 5 units, even though a path that makes the same two changes one direction at a time measures 7. The order of subtraction does not change the distance because every signed difference is squared.
How do you calculate it in three or more dimensions?
In three dimensions, find the squared horizontal distance in the -plane and add the squared vertical change. The circle equation guide uses the two-dimensional version to collect points at a fixed distance from the centre. The three-dimensional version gives a sphere when the distance is fixed and positive.
| Dimension | Formula |
|---|---|
| 1 | |
| 2 | |
| 3 | |
In the last row, and are coordinates in the same position in the two vectors. The notation means the Euclidean length of their difference. OpenStax develops this length for three-dimensional vectors. SciPy's documentation uses the same definition for arrays of coordinates.
Which properties distinguish Euclidean distance from other measures?
Euclidean distance is a metric. It is nonnegative, vanishes only when all coordinates agree, and stays the same if the points are reversed. It also satisfies the triangle inequality. For any points ,
Squared Euclidean distance keeps rankings because squaring preserves the order of nonnegative lengths. But while . The squared measure fails the triangle inequality. Use it for a nearest-point comparison if you only need the order.
| Measure | How you calculate it | Where the choice matters |
|---|---|---|
| Euclidean | Square differences, sum, then take the square root | Straight-line geometry or features compared in a chosen common scale |
| Manhattan | Sum the absolute differences | Grid paths where movement takes one axis at a time |
| Chebyshev | Take the largest absolute difference | Grids where a diagonal step can change two coordinates at once |
A choice of metric can change a nearest-neighbour result, as the University of Illinois lecture material illustrates. When features have very different scales, large-scale features can dominate the direct calculation. A standardised Euclidean comparison can divide each coordinate difference by a positive feature scale such as its standard deviation. State the scales you used. A constant feature with standard deviation zero needs to be excluded or handled separately before standardising.
Worked Euclidean Distance Examples
Example 1
Find the distance between and .
- The changes are and .
- Their squares sum to .
- The Euclidean distance is units.
Example 2
Find the distance between and .
- The differences are and .
- Square both differences to get 16 and 9.
- The result is units. Subtracting in the reverse order gives the same length.
Example 3
Find the three-dimensional distance between and .
- The changes are , , and .
- The sum of squares is .
- The straight-line distance is units.
Example 4
Find the distance between and .
- Subtract matching entries to get .
- The squared distance is .
- The distance is .
Example 5
Which is closer to the origin, or ? Check both Euclidean and Manhattan distances.
- Euclidean distance to is 4, while the distance to is .
- Manhattan distance to is 4, while the distance to is .
- Euclidean distance ranks nearer. Manhattan distance ranks nearer.
Example 6
Compare two candidates on two scores. Relative to a target, candidate differs by and candidate by . The feature standard deviations are 2 and 10.
- Without scaling, the respective squared distances are and . Candidate is nearer.
- Divide the two coordinate differences by 2 and 10 respectively. The squared distances become and .
- With this scaling, is nearer. The change reflects the relative weight given to the two features.
Mistakes That Change a Distance
Mistake 1: Adding differences before squaring them
For differences 3 and , use . Squaring their sum would give 1 and would allow unrelated coordinate changes to cancel.
Mistake 2: Reporting the sum of squares as the length
A sum of squared metre differences is in square metres. Taking the square root restores metres. Distinguish a squared comparison from a distance report.
Mistake 3: Combining different coordinate units without conversion
For a physical grid with one change given as and another as , use 2 and 1.5 in metres. The distance is . Compare features of different kinds only after choosing and explaining their scales.
Try It Yourself
- Find the distance between and .
- Find the distance between and .
- Compare the Euclidean and Manhattan distances from to .
Answers and Reasoning
- The squares add to 169, so the distance is 13.
- Only the third coordinate changes. Its absolute difference is 5, the distance.
- Euclidean distance is . The Manhattan distance is .
Questions About Euclidean Distance
Does switching the points change the distance?
No. Every signed difference changes sign, and squaring removes that change. This is the symmetry property.
Can latitude and longitude be used directly as Euclidean coordinates?
For real travel over Earth's surface, degrees of latitude and longitude don't give straight distances in a flat metric. Use an appropriate projection for local distances or a geodesic method for a calculation on Earth's surface.
Conclusion
- Match coordinates and put physical lengths in common units.
- Square the differences, add them, then take the square root.
- Choose a metric and feature scaling that match the comparison.
Read More
Research Behind This Article
- OpenStax, Calculus Volume 3 — Vectors in Three Dimensions. OpenStax
- SciPy, Euclidean Distance Between One-Dimensional Arrays. SciPy documentation
- Universitat Politècnica de Catalunya, Basic Notions in Computational Geometry. UPC
- University of Illinois Urbana-Champaign, CS 277 — Nearest Neighbour Search. Illinois