Distance.bas
This calculates an approximation for the distance formula r=SQR(x2 + y2),
based on two parameters, x and y. The return value is not guaranteed to be accurate -
and indeed can be as high as 10% inaccurate as x and y approach 255 (the upper limit for input).
The return value is an integer - chosen because screen is 256 pixels wide, and the diagonal across the screen
is bigger than 1 byte can hold.
If you need accurate results, you should go with iSqrt or fSqrt from this library.
For speed, this can't be beaten, however.
Comparing -
answer = distance(i, j) against
answer = iSqrt(i * i + j * j) shows over a range of i and j 1..250:
- distance 8.98 seconds
iSqrt 50.1 seconds
Distance is definitely faster, if you're willing to accept the greater inaccuracy (you probably are).
By the by - standard floating point square root:
fSqrtfunction: 44 minutes (2625.14 seconds)SQR(ROM) - 122 minutes. (7336.86 seconds)
Shows how awful that ROM SQR routine really is...
Formula is: in a right angle triangle with sides A and B, and hypotenuse H, as an estimate of length of H, it returns (A + B) - (half the smallest of A and B) - (1/4 the smallest of A and B) + (1/16 the smallest of A and B)