If you are taking distance measurements, watch out for square roots. They have an unpleasant habit of dropping half of your accuracy. If you stack a few of these calculations up, you can get big problems quickly. Here is the distance function I used.
double Distance(double x0, double y0, double x1, double y1) { double a, b, dx, dy; dx = abs(x1 - x0); dy = abs(y1 - y0); a = max(dx, dy)); if (a == 0) return 0; b = min(dx, dy); return a * sqrt( 1 + (b*b) / (a*a) ); }
Since the last operation is not a square root, you no longer lose accuracy.
I found this in a project that I was working on. After studying it and finding out what he did, I tracked down the programmer who I thought was responsible to congratulate him, but he had no idea what I was talking about.
source share