Fast approximate 2D distance formula

164 Views Asked by At

I want to compute 2D distance faster.
We all know the base formula: $$ \left(x\right)^{2}+\left(y\right)^{2}=r^{2} $$ but it require $$ \sqrt{r} $$ which is very heavy computing, my case is 4 millions loop.

I've made this formula $$ a=\frac{\left|x\right|+\left|y\right|}{2\max\left(\left|x\right|,\left|y\right|\right)} $$ $$ \frac{\left(a\left(\left|x\right|+\left|y\right|\right)+2\left(1-a\right)\cdot\max\left(\left|x\right|,\left|y\right|\right)\ \right)}{1.5}=\ r $$ which doesn't require to calculate the square root at all! It is low accuracy but it is acceptable.

Desmos's graph showcase

Are there any quicker distance calculating than this? Thank you very much!


Update

I know this not a good way to approximate the distance, compare to the square root one. So that is why I am asking to learn any better formula (faster calculating) available. Thanks!

p/s: Not so much precision is need but need to be more precise than the Manhattan distance.

1

There are 1 best solutions below

0
On

$$\begin{align}r&=x\sqrt{1+\left (\frac{y}{x}\right)^2}, x\ne 0\\ &=x\cdot \sec t, \tan t=\frac{y}{x}\end{align}$$

If x=0 then you factor the y.

If you implemented a table of secants then you’re a linear interpolation (or a mean average) plus a multiplication away from the result.