Function Newbie question...

Jim Hurley jhurley at infostations.com
Fri Dec 6 10:49:01 EST 2002


>Hi list,
>
>I wrote a small function that returns the distance between two Points.
>
>Function getDistance x1,y1,x2,y2
>   put sqrt((X1 - X2) ^ 2 + (Y1 - Y2) ^ 2) into theDistance
>   return theDistance
>end getDistance
>
>It works for me if I pass the values to it on a mouseUp event.
>
>on mouseUp
>   put item 1 of the loc of graphic "kreis1" into x1
>   put item 1 of the loc of graphic "kreis2" into x2
>   put item 2 of the loc of graphic "kreis1" into y1
>   put item 2 of the loc of graphic "kreis2" into y2
>   put getDistance (x1,y1,x2,y2)
>end mouseUp
>
>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. So here I am
>stuck, as the number of objects I want to check might vary from time to
>time.
>
>Any Ideas?
>
>Regards,
>
>Malte

Malte,

The  distance function is a little bit easier to work with if you use 
the 2 values of the loc() as input  rather than the the 4 individual 
coordinates.

The  following will determine the distance between the 5 graphics 
"kreis1" to "kreis5".

on mouseUP
   put empty into field 1
   repeat with i = 1 to 4
     repeat with j = i+1 to 5
       put "kreis" & i into n
       put "kreis" & j into m
       put i & comma & j && distance(the loc of graphic n, the loc of 
graphic m )& return after field 1
     end repeat
   end repeat
end mouseUP

function distance pt1,pt2
   put item 1 of pt1 into x1
   put item 2 of pt1 into y1
   put item 1 of pt2 into x2
   put item 2 of pt2 into y2
   return sqrt((x1-x2)^2 + (y1-y2)^2)
end distance

Jim
-- 
Jim Hurley



More information about the use-livecode mailing list