Which is Closest

BlitzMax Forums/BlitzMax Beginners Area/Which is Closest

Eric(Posted 2005) [#1]
Lets Say I have My mouse pointer and 50 objects on the screen. What would be the best way to determine which one is closest. I've tried various methods using the distance formula but I can't seem to get it right. The 50 objects are in a Tlist collection.

Can someone help.


ziggy(Posted 2005) [#2]
something like:
function closest()
  var maxdistance:long = 100000
  var obj:object
  for o:object eachin mylist
      if maxdistance>distance(mouse,o) then
          obj = o
          maxdistance = distance(mouse,o)
      endif
  next
  return obj
endfunction


Be sure to return the distance in an absolute value, not negative distances please!


Eric(Posted 2005) [#3]
I tried that same thing with a little different implementation... I will try this and thanks for your time.


Dreamora(Posted 2005) [#4]
The best thing would be a scanline
Optimation of the upper one: Don't use sqrt to calculate the distance, just compare squared distance, this is worlds faster :)


ziggy(Posted 2005) [#5]
If you don't square the distance (wich is a very good thing), intialize the maxdistance to something much bigger. (as instance 999999999999)


Who was John Galt?(Posted 2005) [#6]
And x*x instead of x^2 (unless this has been optimised recently)