Advertisement

Fast Distance(Yes I know this has been brought up before)

Started by May 30, 2003 06:35 PM
49 comments, last by jmoses 21 years, 8 months ago
I was wondering if this would work: I know the distance formulas has been talked about a million times before in this forum, but nonetheless Normally we do something like sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ); and I know we can speed it up by taking off the sqrt so now we have (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) but do we really need the multiply, wouldnt this be faster? fabs(a.x-b.x) + fabs(a.y-b.y) I know this isnt the TRUE distance, but it works and it is fast, right?
- John Moses
Sure, but it''s quite possibly the worst distance funtion I''ve ever seen. The "distance formula" is simply the pythagarian theorum with the starting point, so it''s like using a+b=c as the pythagorian therum... stupid.




-~-The Cow of Darkness-~-

If you see the image I am online
-~-The Cow of Darkness-~-
Advertisement
Hehe, cool, thanks ;-)

Well I guess A+B=C in my case, becuase I want it nice and fast

but is there any faster way to find the absolute value without fabs() I know I can multiply it by itself, but is there anything faster like bitwise operators?


------------------------------
And also wouldn't this be faster:
float x = a.x-b.x;
float y = a.y-b.y;
x*x+y*y;
than this:
(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
becuase it does the subtraction twice instead of 4 times?

[edited by - Jmoses on May 30, 2003 8:17:24 PM]
- John Moses
quote:
Original post by jmoses
and I know we can speed it up by taking off the sqrt



Umm, no?
I thought I made myself clear, but your algorithm simply does not work. This is not meant as a flame, but it''s the truth.




-~-The Cow of Darkness-~-

If you see the image I am online
-~-The Cow of Darkness-~-
yes if only using it to compare with other distances calculated in the same way, taking off sqrt is totally acceptable since sqrt(x) is a strictly increasing function (x increases -> sqrt(x) increases).
Advertisement
Hmmm... I guess I see your guys points now, but to C-Junkie," Umm.. Yes"
- John Moses
quote:
Original post by Anonymous Poster
yes if only using it to compare with other distances calculated in the same way, taking off sqrt is totally acceptable since sqrt(x) is a strictly increasing function (x increases -> sqrt(x) increases).


That''s a good true point, but still, a^2 + b^2=c^2 is not A+B=C
-~-The Cow of Darkness-~-
The last metric provided:
fabs(a.x-b.x) + fabs(a.y-b.y)
will only work in a rough sense. Under the standard metric you get that all points the same distance away from a given point is a circle, under the new metric (called taxicab by some) all the points the same distance away is a diamond (square rotated 45 degrees).

Suppose you had three points: A(0,0), B(2,0), and C(1,1). The distance from A to B is 2 and from A to C is root 2 under the standard metric. Under the taxicab metric, the distances are both 2. That may be good enough but is definately different from the other metric. You can remove the square root on the standard metric as mentioned if only relative distance is important.
Oh alright, I got it now, thanks guys. Yeah, All I wanted is a fast distance check, I dont care if its a diamond instead of a circle, or if it isn''t that exact, but I guess this will work for me.

Thanks again
- John Moses

This topic is closed to new replies.

Advertisement