Update
This commit is contained in:
parent
1f9cd12678
commit
630e25d688
12 changed files with 98 additions and 22 deletions
31
distance.md
31
distance.md
|
@ -1,6 +1,15 @@
|
|||
# Distance
|
||||
|
||||
TODO
|
||||
Distance is a measure of how far away from each other two points are. Most commonly distance refers to physical separation in space, e.g. as in distance of planets from the Sun, but more generally distance can refer to any kind of parameter space and in any number of [dimensions](dimension.md), e.g. the distance of events in time measured in seconds (1D distance) or distance of two text strings as the amount of their dissimilarity ([Levenshtein distance](levenshtein_distance.md)). Distances are extremely important in [computer science](compsci.md) and [math](math.md) as they allow us to do such things as [clustering](clustering.md), path searching, physics simulations, various comparisons, [sorting](sort.md) etc.
|
||||
|
||||
Distance is similar/related to [length](length.md), the difference is that distance is computed between two points while length is the distance of one point from some implicit origin.
|
||||
|
||||
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.
|
||||
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)
|
||||
|
||||
## Approximations
|
||||
|
||||
|
@ -93,4 +102,24 @@ int32_t dist48(
|
|||
}
|
||||
```
|
||||
|
||||
A similar approximation for 2D distance is (from a 1984 book *Problem corner*) this: *sqrt(dx^2 + dy^2) ~= 0.96 * dx + 0.4 * dy* for *dx >= dy >= 0*. The error is <= 4%. This can be optionally modified to use the closest power of 2 constants so that the function becomes much faster to compute, but the maximum error increases (seems to be about 11%). C code with fixed point follows (commented out line is the faster, less accurate version):
|
||||
|
||||
```
|
||||
int dist2DApprox(int x0, int y0, int x1, int y1)
|
||||
{
|
||||
x0 = x0 > x1 ? (x0 - x1) : (x1 - x0);
|
||||
y0 = y0 > y1 ? (y0 - y1) : (y1 - y0);
|
||||
|
||||
if (x0 < y0)
|
||||
{
|
||||
x1 = x0; // swap
|
||||
x0 = y0;
|
||||
y0 = x1;
|
||||
}
|
||||
|
||||
return (123 * x0 + 51 * y0) / 128; // max error = ~4%
|
||||
//return x0 + y0 / 2; // faster, less accurate
|
||||
}
|
||||
```
|
||||
|
||||
TODO: this https://www.flipcode.com/archives/Fast_Approximate_Distance_Functions.shtml
|
Loading…
Add table
Add a link
Reference in a new issue