Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit 1766bbc

Browse files
author
Flavio Poletti
committed
Add content for distance between points
1 parent 643a024 commit 1766bbc

File tree

1 file changed

+140
-1
lines changed

1 file changed

+140
-1
lines changed

docs/distance-between-points.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,143 @@ title: Distance between points
44
sidebar_label: Distance between points
55
---
66

7-
[Open a pull request](https://github.com/AllAlgorithms/algorithms/tree/master/docs/distance-between-points.md) to add the content for this algorithm.
7+
The **distance between two points** can have several different
8+
definitions depending on the specific situation, although without any
9+
further specification it usually refers to the length of the segment
10+
connecting the two points (also called [*Euclidean distance*][euclidean]).
11+
12+
## Euclidean Distance
13+
14+
The [Euclidean distance][euclidean] is generally defined as the lenght
15+
of the segment connecting two points.
16+
17+
<img
18+
src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Euclidean_distance_2d.svg/500px-Euclidean_distance_2d.svg.png"
19+
alt="Euclidean distance">
20+
21+
### Formula
22+
23+
The [Euclidean distance][euclidean] can be easily calculated when the
24+
coordinates of the two points are known. For example, in the XY plane,
25+
the formulas would be the following:
26+
27+
<img
28+
src="https://latex.codecogs.com/gif.latex?P%20%3D%20%28P_x%2C%20P_y%29"
29+
alt="P = (P_x, P_y)">
30+
31+
<img
32+
src="https://latex.codecogs.com/gif.latex?Q%20%3D%20%28Q_x%2C%20Q_y%29"
33+
alt="Q = (Q_x, Q_y)">
34+
35+
<img
36+
src="https://latex.codecogs.com/gif.latex?d%28P%2C%20Q%29%20%3D%20%5Csqrt%7B%28P_x%20-%20Q_x%29%5E2%20&plus;%20%28P_y%20-%20Q_y%29%5E2%7D"
37+
alt="d(P, Q) = \sqrt{(P_x - Q_x)^2 + (P_y - Q_y)^2}">
38+
39+
The formula above can be generalized for vector spaces of any dimension,
40+
by taking the square root of the sum of the squares of differences in
41+
each dimension:
42+
43+
<img
44+
src="https://latex.codecogs.com/gif.latex?P%20%3D%20%28P_1%2C%20P_2%2C%20...%2C%20P_n%29"
45+
alt="P = (P_1, P_2, ..., P_n)">
46+
47+
<img
48+
src="https://latex.codecogs.com/gif.latex?Q%20%3D%20%28Q_1%2C%20Q_2%2C%20...%2C%20Q_n%29"
49+
alt="Q = (Q_1, Q_2, ..., Q_n)">
50+
51+
<img
52+
src="https://latex.codecogs.com/gif.latex?d%28P%2C%20Q%29%20%3D%20%5Csqrt%7B%28P_1%20-%20Q_1%29%5E2%20&plus;%20%28P_2%20-%20Q_2%29%5E2%20&plus;%20...%20&plus;%20%28P_n%20-%20Q_n%29%5E2%7D"
53+
alt="d(P, Q) = \sqrt{(P_1 - Q_1)^2 + (P_2 - Q_2)^2 + ... + (P_n - Q_n)^2}">
54+
55+
<img
56+
src="https://latex.codecogs.com/gif.latex?d%28P%2C%20Q%29%20%3D%20%5Csqrt%7B%5Csum_%7Bi%3D1%7D%5En%20%28P_i%20-%20Q_i%29%5E2%7D"
57+
alt="d(P, Q) = \sqrt{\sum_{i=1}^n (P_i - Q_i)^2}">
58+
59+
### Algorithm
60+
61+
The algorithm for the [Euclidean distance][euclidean] can be derived
62+
directly from the defining formula above:
63+
64+
```
65+
/* Assume that P and Q are N-dimensional arrays */
66+
distance(P, Q) {
67+
N = dim(P)
68+
square_sum = 0.0 /* initialize running sum */
69+
for dimension i from 1 to N { /* iterate over all dimensions */
70+
square_sum = square_sum + (P[i] - Q[i])^2 /* sum squares of differences */
71+
}
72+
distance = sqrt(square_sum); /* distance is the square root of the sum of squared differences */
73+
return distance;
74+
}
75+
```
76+
77+
## Manhattan Distance
78+
79+
Among the many possible definitions of distance, another one commonly
80+
found in computer science is the [Manhattan distance][manhattan]. This
81+
is typically a distance used when movements are constrained to happen
82+
over a grid:
83+
84+
<img
85+
src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Manhattan_distance.svg/500px-Manhattan_distance.svg.png"
86+
alt="Manhattan distance">
87+
88+
### Formula
89+
90+
The formula below applies when calculating the [Manhattan
91+
distance][manhattan] over an N-dimensional grid where adjacent nodes are
92+
assumed to have distance 1:
93+
94+
<img
95+
src="https://latex.codecogs.com/gif.latex?P%20%3D%20%28P_1%2C%20P_2%2C%20...%2C%20P_n%29"
96+
alt="P = (P_1, P_2, ..., P_n)">
97+
98+
<img
99+
src="https://latex.codecogs.com/gif.latex?Q%20%3D%20%28Q_1%2C%20Q_2%2C%20...%2C%20Q_n%29"
100+
alt="Q = (Q_1, Q_2, ..., Q_n)">
101+
102+
<img
103+
src="https://latex.codecogs.com/gif.latex?d%28P%2C%20Q%29%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20%7CP_i%20-%20Q_i%7C"
104+
alt="d(P, Q) = \sum_{i=1}^n |P_i - Q_i|">
105+
106+
### Algorithm
107+
108+
The algorithm can be implemented as a direct derivation of the formula:
109+
110+
```
111+
/* Assume that P and Q are N-dimensional arrays */
112+
manhattan_distance(P, Q) {
113+
N = dim(P)
114+
distance = 0.0 /* initialize running sum */
115+
for dimension i from 1 to N { /* iterate over all dimensions */
116+
distance = distance + abs(P[i] - Q[i]) /* sum squares of differences */
117+
}
118+
return distance;
119+
}
120+
```
121+
122+
## Performance
123+
124+
Both algorithms for each respective distance definition are linear
125+
(O(n)) with respect to the number of dimensions with which each point is
126+
represented.
127+
128+
129+
## Implementations
130+
131+
| | Language | Link |
132+
|:-: | :-: | :-: |
133+
| | | |
134+
135+
## Helpful Links
136+
137+
- [Euclidean distance][euclidean]
138+
- [Manhattan distance][manhattan]
139+
- [Norm][norm] a generalization of the concept of distance
140+
- [L<sup>p</sup> space][lp-space] a generalization of a class of norms
141+
142+
143+
[euclidean]: https://en.wikipedia.org/wiki/Euclidean_distance
144+
[manhattan]: https://en.wikipedia.org/wiki/Taxicab_geometry
145+
[norm]: https://en.wikipedia.org/wiki/Norm_(mathematics)
146+
[lp-space]: https://en.wikipedia.org/wiki/Lp_space

0 commit comments

Comments
 (0)