Which is Closest
BlitzMax Forums/BlitzMax Beginners Area/Which is Closest
| ||
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. |
| ||
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! |
| ||
I tried that same thing with a little different implementation... I will try this and thanks for your time. |
| ||
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 :) |
| ||
If you don't square the distance (wich is a very good thing), intialize the maxdistance to something much bigger. (as instance 999999999999) |
| ||
And x*x instead of x^2 (unless this has been optimised recently) |