Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Euclidean distances


1. Euclidean distances
The Euclidean distance between two points is the straight line distance between the points in a non-curved space.

2. Pythagoras
Pythagorean theoremThe idea is directly related to the Pythagorean theorem of right triangles (in any dimension).

In the image, the (red) vertical/rise y distance is 3, the (green) horizontal/run x distance is 4, and the (blue) hypotenuse distance is 5.
52 = 42 + 32


3. One dimension
The distance d between points x1 and x2 on a line is determined as follows.

1d distance formula
The above math formula can be written as the following coding expressions (as assignment statements).
d = sqrt( pow(x2-x1,2)) d = x2 - x1


4. Two dimensions
The distance d between points (x1, y1) and (x2, y2) in a plane is determined as follows.

2d distance formula
The above math formula can be written as the following coding expression (as an assignment statement).
d = sqrt( pow(x2-x1,2) + pow(y2-y1,2))

If intermediate variables are used, such as dx and dy, then the above can be written as the following assignment statements. Note: As is always the case, variable declarations may be required.
dx = x2 - x2 dy = y2 - y1 d = sqrt(dx*dx + dy*dy)

Note: Once dx and dy are defined, one can just square the distances instead of using the pow function to avoid repeating expressions.

5. Three dimensions
The distance d between points (x1, y1, z1) and (x2, y2, z2) in a space is determined as follows.

3d distance formula
The above math formula can be written as the following coding expression (as an assignment statement).
d = sqrt( pow(x2-x1,2) + pow(y2-y1,2) + pow(z2-z1, 2))


6. Generalization
This idea can be generalized to n-dimensional spaces as needed.

7. End of page

8. Multiple choice questions for this page