This commit is contained in:
Miloslav Ciz 2025-06-19 02:56:49 +02:00
parent f216e89f03
commit 0e12dc9efe
18 changed files with 2071 additions and 1974 deletions

View file

@ -7,7 +7,7 @@ Distance is similar/related to [length](length.md), the difference is that dista
There are many ways to define distance within given space. Most common and implicitly assumed distance is the **[Euclidean distance](euclidean_distance.md)** (basically the "straight line from point A to point B" whose length is computed with [ Euclidean Theorem](euclidean_theorem.md)), but other distances are possible, e.g. the [taxicab distance](taxicab_distance.md) (length of the kind of perpendicular path taxis take between points A and B in Manhattan, usually longer than straight line). Mathematically a space in which distances can be measured are called [metric spaces](metric_space.md), and a distance within such space can be any [function](function.md) *dist* (called a *distance* or *metric* function) that satisfies these [axioms](axiom.md):
1. *dist(p,p) = 0* (distance from identical point is zero)
2. Values given by *dist* are never negative.
2. *dist(p,q) > 0* if *p != q* (distance between two distinct points is always positive)
3. *dist(p,q) = dist(q,p)* ([symmetry](symmetry.md), distance between two points is the same in both directions).
4. *dist(a,c) <= dist(a,b) + dist(b,c)* (triangle inequality)
@ -16,7 +16,7 @@ There are many ways to define distance within given space. Most common and impli
Computing Euclidean distance requires multiplication and most importantly [square root](sqrt.md) which is usually a pretty slow operation, therefore many times we look for simpler [approximations](approximation.md). Note that a possible approach here may also lead through computing the distance normally but using a fast approximation of the square root.
Two very basic and rough approximations of Euclidean distance, both in 2D and 3D, are [taxicab](taxicab.md) (also Manhattan) and [Chebyshev](chebyshev.md) distances. Taxicab distance
simply adds the absolute coordinate differences along each principal axis (*dx*, *dy* and *dz*) while Chebyshev takes the maximum of them. In [C](c.md) (for generalization to 3D just add one coordinate of course):
simply adds the absolute coordinate differences along each principal axis (*dx*, *dy* and *dz*) while Chebyshev takes the maximum of them (NOTE: taking minimum isn't possible due to the definition which requires two distinct points to always have positive distance). In [C](c.md) (for generalization to 3D just add one coordinate of course):
```
int distTaxi(int x0, int y0, int x1, int y1)
@ -38,6 +38,48 @@ int distCheb(int x0, int y0, int x1, int y1)
Both of these distances approximate a [circle](circle.md) in 2D with a square or a sphere in 3D with a cube, the difference is that taxicab is an upper estimate of the distance while Chebyshev is the lower estimate. For speed of execution ([optimization](optimization.md)) it may also be important that taxicab distance only uses the operation of addition while Chebyshev may result in [branching](branch.md) (*if*) in the max function which is usually not good for performance.
```
--------------------------------------------------------------------------
Euclidean _____
_.'' ''._
sqrt( B / \ F
(B.x - A.x)^2 + _,-+ / C d \ _-+
(B.y - A.y)^2) _,--' ( +-------) _,-'
_,--' \ / _,-'
_,--' \_ _/ _,-'
A _,--' '--_____--' E _,-'
+-' +-'
--------------------------------------------------------------------------
Taxicab (Manhattan) _A_
B _/ \_ F
+ _/ \_ ,----+
abs(B.x - A.x) + | _/ C d \_ ,'
abx(B.y - A.y) | <_ +-------> ,'
| \_ _/ ,'
| \_ _/ ,'
A | \_ _/ E ,'
+-------------------------' V +----'
--------------------------------------------------------------------------
Chebyshev _______________
B | | F
max( + | | ,+
abs(B.x - A.x), | C d | ,'
abs(B.y - A.y)) | +-------| ,'
| | ,--------'
| | ,'
A |_______________| E ,'
+-------------------------- +'
--------------------------------------------------------------------------
```
*Three commonly used distance measures: the distance is the length of the path illustrated between points A and B; next are shown "circles" (sets of points with distance d from point C) and "lines" (one diagonal of a rhombus in which points E and F are opposite to each other) drawn by respective measures.*
A bit more accuracy can be achieved by averaging the taxicab and Chebyshev distances which in 2D approximates a circle with an 8 segment polygon and in 3D approximates a sphere with 24 sided [polyhedron](polyhedron.md). The integer-only [C](c.md) code is following:
```