[Home](https://learningfolio.de5.net/) / [Math](https://learningfolio.de5.net/math/) / [Math glossary](https://learningfolio.de5.net/math/glossary/) / Euclidean Distance: Formula for 2D, 3D, and Vectors

[Math glossary](https://learningfolio.de5.net/math/glossary/)

# 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.

Written by **Learningfolio Team**

We’re subject-matter experts in math, science, parent support, and education, with 100 years of combined experience. We believe in learning, experimenting, and growing, and we review one another’s work.

## On this page

* [TL;DR](https://learningfolio.de5.net/math/euclidean-distance/#tldr)
* [Key Points](https://learningfolio.de5.net/math/euclidean-distance/#key-points)
* [What is Euclidean distance?](https://learningfolio.de5.net/math/euclidean-distance/#what-is-euclidean-distance)
* [How do you calculate it in three or more dimensions?](https://learningfolio.de5.net/math/euclidean-distance/#how-do-you-calculate-it-in-three-or-more-dimensions)
* [Which properties distinguish Euclidean distance from other measures?](https://learningfolio.de5.net/math/euclidean-distance/#which-properties-distinguish-euclidean-distance-from-other-measures)
* [Worked Euclidean Distance Examples](https://learningfolio.de5.net/math/euclidean-distance/#worked-euclidean-distance-examples)
* [Mistakes That Change a Distance](https://learningfolio.de5.net/math/euclidean-distance/#mistakes-that-change-a-distance)
* [Try It Yourself](https://learningfolio.de5.net/math/euclidean-distance/#try-it-yourself)
* [Questions About Euclidean Distance](https://learningfolio.de5.net/math/euclidean-distance/#questions-about-euclidean-distance)
* [Conclusion](https://learningfolio.de5.net/math/euclidean-distance/#conclusion)
* [Read More](https://learningfolio.de5.net/math/euclidean-distance/#read-more)
* [Research Behind This Article](https://learningfolio.de5.net/math/euclidean-distance/#research-behind-this-article)

## 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](https://learningfolio.de5.net/math/formula/distance-formula/) follows from the Pythagorean theorem.

$$
d_2(P,Q)=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}.
$$

For example, if the coordinate differences are 3 and 4, the distance is $\sqrt{9+16}=5$. 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.

![A coordinate grid shows a straight segment of length five and a horizontal-plus-vertical route of length seven between the same points](https://learningfolio.de5.net/assets/versioned/euclidean-distance-grid-paths.4cbfe09ccd6e.webp)


The two paths connect the same points. Only the straight segment gives Euclidean distance.

## How do you calculate it in three or more dimensions?

In three dimensions, find the squared horizontal distance in the $xy$-plane and add the squared vertical change. The [circle equation guide](https://learningfolio.de5.net/math/equation-of-a-circle/) 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 | $d=\lvert x_2-x_1\rvert$ |
| 2 | $d=\sqrt{(\Delta x)^2+(\Delta y)^2}$ |
| 3 | $d=\sqrt{(\Delta x)^2+(\Delta y)^2+(\Delta z)^2}$ |
| $n$ | $d=\sqrt{\sum_{i=1}^n(p_i-q_i)^2}=\lVert p-q\rVert_2$ |

In the last row, $p_i$ and $q_i$ are coordinates in the same position in the two vectors. The notation $\lVert p-q\rVert_2$ 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 $P,Q,R$,

$$
d(P,R)\le d(P,Q)+d(Q,R).
$$

Squared Euclidean distance keeps rankings because squaring preserves the order of nonnegative lengths. But $d(0,2)^2=4$ while $d(0,1)^2+d(1,2)^2=2$. 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 $P=(1,1)$ and $Q=(4,5)$.

1. The changes are $4-1=3$ and $5-1=4$.
2. Their squares sum to $9+16=25$.
3. The Euclidean distance is $\sqrt{25}=5$ units.

### Example 2

Find the distance between $(-3,2)$ and $(1,-1)$.

1. The differences are $1-(-3)=4$ and $-1-2=-3$.
2. Square both differences to get 16 and 9.
3. The result is $\sqrt{25}=5$ units. Subtracting in the reverse order gives the same length.

### Example 3

Find the three-dimensional distance between $(2,-1,4)$ and $(-1,3,16)$.

1. The changes are $-3$, $4$, and $12$.
2. The sum of squares is $9+16+144=169$.
3. The straight-line distance is $13$ units.

### Example 4

Find the distance between $(1,-1,2,4)$ and $(3,-1,-1,0)$.

1. Subtract matching entries to get $(2,0,-3,-4)$.
2. The squared distance is $4+0+9+16=29$.
3. The distance is $\sqrt{29}\approx5.39$.

### Example 5

Which is closer to the origin, $A=(4,0)$ or $B=(2.5,2.5)$? Check both Euclidean and Manhattan distances.

1. Euclidean distance to $A$ is 4, while the distance to $B$ is $\sqrt{2.5^2+2.5^2}=\sqrt{12.5}<4$.
2. Manhattan distance to $A$ is 4, while the distance to $B$ is $2.5+2.5=5$.
3. Euclidean distance ranks $B$ nearer. Manhattan distance ranks $A$ nearer.

### Example 6

Compare two candidates on two scores. Relative to a target, candidate $A$ differs by $(1,6)$ and candidate $B$ by $(4,1)$. The feature standard deviations are 2 and 10.

1. Without scaling, the respective squared distances are $37$ and $17$. Candidate $B$ is nearer.
2. Divide the two coordinate differences by 2 and 10 respectively. The squared distances become $0.5^2+0.6^2=0.61$ and $2^2+0.1^2=4.01$.
3. With this scaling, $A$ 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 $-4$, use $3^2+(-4)^2=25$. 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 $2\text{ m}$ and another as $150\text{ cm}$, use 2 and 1.5 in metres. The distance is $2.5\text{ m}$. Compare features of different kinds only after choosing and explaining their scales.

## Try It Yourself

1. Find the distance between $(0,0)$ and $(5,12)$.
2. Find the distance between $(3,2,-4)$ and $(3,2,1)$.
3. Compare the Euclidean and Manhattan distances from $(0,0)$ to $(2,2)$.

### Answers and Reasoning

1. The squares add to 169, so the distance is 13.
2. Only the third coordinate changes. Its absolute difference is 5, the distance.
3. Euclidean distance is $\sqrt8=2\sqrt2$. The Manhattan distance is $2+2=4$.

## 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

* [Plot and compare ordered pairs](https://learningfolio.de5.net/math/coordinate-plane-and-ordered-pairs/)
* [Use the Pythagorean theorem to find an unknown side](https://learningfolio.de5.net/math/geometry/pythagoras-theorem/)

## Research Behind This Article

1. OpenStax, *Calculus Volume 3 — Vectors in Three Dimensions*. [OpenStax](https://openstax.org/books/calculus-volume-3/pages/2-2-vectors-in-three-dimensions)
2. SciPy, *Euclidean Distance Between One-Dimensional Arrays*. [SciPy documentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.euclidean.html)
3. Universitat Politècnica de Catalunya, *Basic Notions in Computational Geometry*. [UPC](https://dccg.upc.edu/wp-content/uploads/2023/09/GeoC-BasicNotions.pdf)
4. University of Illinois Urbana-Champaign, *CS 277 — Nearest Neighbour Search*. [Illinois](https://courses.grainger.illinois.edu/cs277/sp2023/assets/lectures/slides/sp23/cs277-19-nns.pdf)
