The Euclidean distance between two points is the straight line distance between the points in a non-curved space.
The 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
The distance
d between points x
1 and x
2 on a line is determined as follows.
The above math formula can be written as the following coding expressions (as assignment statements).
d = sqrt( pow(x2-x1,2))
d = x2 - x1
The distance
d between points (x
1, y
1) and (x
2, y
2) in a plane is determined as follows.
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.
The distance
d between points (x
1, y
1, z
1) and (x
2, y
2, z
2) in a 3-D space is determined as follows.
The above math formula can be written as the following coding expression (as an assignment statement) where
sqrt is the square root function and
pow is the power function.
d = sqrt( pow(x2-x1,2) + pow(y2-y1,2) + pow(z2-z1, 2))
This distance is called the Euclidean distance and uses the Pythagorean theorem. This distance idea is used in many areas of statistics in distributions, error analysis, etc.
This idea can be generalized to n-dimensional spaces as needed.