Function Newbie question...
Dar Scott
dsc at swcp.com
Fri Dec 6 13:01:00 EST 2002
On Friday, December 6, 2002, at 03:17 AM, Malte Brill wrote:
> O.K., but if I have more than two objects (lets say about 5). how
> could I
> check the distances between all of them efficiantly?
>
> 1->2 2->1
> 1->3 2->3
> 1->4 2->4
> 1->5 2->5 ... 5->4
>
> Using a repeat structure seams not being a good idea to me.
What about 20? 300?
If you want to _calculate_ the distances among n objects you will need
to to calculate n(n-1)/2 distances. It may be the easiest way to do
this is to do n^2 calculations, or n(n-1) calculations as Ken Ray
suggested. One way to do the n(n-1)/2 calculations is to have a list of
objects and go through the list calculating the distances of each object
to the objects below it.
However, if you want to _check_ distances, say find out which pairs are
closer than some threshold, there are probably ways to cut down on the
number of calculations. You can sort them into bins and only check
pairs in the same or adjacent bins.
If you need to calculate/check only a few, then something like Ken's
example should work. (If you want to tweak that, try changing the start
value for y to x+1 and leaving out the equality test. And the last loop
for x is not needed.)
Note that his example used your distance function and built upon it.
To calculate the pairs of distances, the repeat is probably the best way
to go. If you are comfortable with recursion, then you might want to
try that.
Dar Scott
More information about the Use-livecode
mailing list