Home » Community » Coffee corner » Approximate distance calculation
Approximate distance calculation [message #48704] 
Sat, 26 August 2017 09:55 

mirek
Messages: 13664 Registered: November 2005

Ultimate Member 


While working on OSD firmware for my FPV plane, I have started playing with the idea how to calculate fast distance approximation:
int ihypot(int x, int y)
{
if(x < 0)
x = x;
if(y < 0)
y = y;
if(x < y)
Swap(x, y);
if(y < (x >> 2) + (x >> 3))
return x + (y >> 3) + (y >> 5);
y = (x >> 2) + (x >> 5);
return x + (y >> 1) + (y >> 4);
}
This is approximation of sqrt(x*x + y*y).
 if max(x, y) < 168, absolute error < 4 (this has more to do with integer rounding)
 otherwise, the error is less than 2%
Putting it here so that perhaps it can be googled if somebody is looking for something like it...



Goto Forum:
Current Time: Mon Aug 15 17:32:23 CEST 2022
Total time taken to generate the page: 0.01136 seconds
