This commit is contained in:
Miloslav Ciz 2025-09-20 17:33:12 +02:00
parent 0e9129c535
commit 349045e2b8
28 changed files with 2076 additions and 2028 deletions

View file

@ -107,7 +107,7 @@ And a picture for summary:
{ The following is an approximation I came up with when working on [tinyphysicsengine](tpe.md). While I measured the average and maximum error of the taxi/Chebyshev average in 3D at about 16% and 22% respectively, the following gave me 3% and 12% values. ~drummyfish }
Yet more accurate approximation of 3D Euclidean distance can be made with a 48 sided [polyhedron](polyhedron.md). The principle is following: take absolute values of all three coordinate differences and order them by magnitude so that *dx >= dy >= dz >= 0*. This gets us into one of 48 possible slices of space (the other slices have the same shape, they just differ by ordering or signs of the coordinates but the distance in them is of course equal). In this slice we'll approximate the distance linearly, i.e. with a [plane](plane.md). We do this by simply computing the distance of our point from a plane that goes through origin and whose normal is approximately {0.8728,0.4364,0.2182} (it points in the direction that goes through the middle of space slice). The expression for the distance from this plane simplifies to simply *0.8728 * dx + 0.4364 * dy + 0.2182 * dz*. The following is an integer-only implementation in [C](c.md) (note that the constants above have been converted to allow division by 1024 for possible [optimization](optimization.md) of division to a bit shift):
Yet a more accurate approximation of 3D Euclidean distance can be achieved with a 48 sided [polyhedron](polyhedron.md). The principle is following: take absolute values of all three coordinate differences and order them by magnitude so that *dx >= dy >= dz >= 0*. This gets us into one of 48 possible slices of space (the other slices have the same shape, they just differ by ordering or signs of the coordinates but the distance in them is of course equal). In this slice we'll approximate the distance linearly, i.e. with a [plane](plane.md). We do this by simply computing the distance of our point from a plane that goes through origin and whose normal is approximately {0.8728,0.4364,0.2182} (it points in the direction that goes through the middle of space slice). The expression for the distance from this plane simplifies to simply *0.8728 * dx + 0.4364 * dy + 0.2182 * dz*. The following is an integer-only implementation in [C](c.md) (note that the constants above have been converted to allow division by 1024 for possible [optimization](optimization.md) of division to a bit shift):
```
int32_t dist48(