Function Newbie question...
jhurley at infostations.com
Fri Dec 6 10:49:01 EST 2002
>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
>It works for me if I pass the values to it on a mouseUp event.
> 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)
>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->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
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
The following will determine the distance between the 5 graphics
"kreis1" to "kreis5".
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
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)
More information about the Use-livecode