Closest-pair problem: Difference between revisions
Content added Content deleted
(Closest-pair problem en Yabsic) |
m (→{{header|REXX}}: added/changed whitespace and comments.) |
||
Line 4,191: | Line 4,191: | ||
<br>manifest itself as a minimum distance of zero (the variable <big> <tt> '''dd''' </tt> </big> on line 17). |
<br>manifest itself as a minimum distance of zero (the variable <big> <tt> '''dd''' </tt> </big> on line 17). |
||
<lang rexx>/*REXX program solves the closest pair of points problem (in two dimensions). */ |
<lang rexx>/*REXX program solves the closest pair of points problem (in two dimensions). */ |
||
parse arg N |
parse arg N LO HI seed . /*obtain optional arguments from the CL*/ |
||
if |
if N=='' | N=="," then N= 100 /*Not specified? Then use the default.*/ |
||
if |
if LO=='' | LO=="," then LO= 0 /* " " " " " " */ |
||
if |
if HI=='' | HI=="," then HI= 20000 /* " " " " " " */ |
||
if datatype(seed, 'W') then call random ,,seed /*seed for RANDOM (BIF) repeatability.*/ |
if datatype(seed, 'W') then call random ,,seed /*seed for RANDOM (BIF) repeatability.*/ |
||
w= length( |
w= length(HI); w= w + (w//2==0) /*W: for aligning the output columns.*/ |
||
/*╔══════════════════════╗*/ do j=1 for N /*generate N random points*/ |
/*╔══════════════════════╗*/ do j=1 for N /*generate N random points*/ |
||
/*║ generate N points. ║*/ @x.j= random( |
/*║ generate N points. ║*/ @x.j= random(LO, HI) /* " a " X */ |
||
/*╚══════════════════════╝*/ @y.j= random( |
/*╚══════════════════════╝*/ @y.j= random(LO, HI) /* " a " Y */ |
||
end /*j*/ /*X & Y make the point.*/ |
end /*j*/ /*X & Y make the point.*/ |
||
A= 1; B= 2 /* [↓] MIND is actually the squared */ |
|||
minD= (@x.A - @x.B)**2 + (@y.A - @y.B)**2 /* distance between the 1st two points.*/ |
|||
/* [↓] use of XJ & YJ speed things up.*/ |
/* [↓] use of XJ & YJ speed things up.*/ |
||
do j=1 for N-1; xj= @x.j; yj= @y.j /*find minimum distance between a ··· */ |
do j=1 for N-1; xj= @x.j; yj= @y.j /*find minimum distance between a ··· */ |
||
do k=j+1 for N-j-1 /* ··· point and all the other points.*/ |
do k=j+1 for N-j-1 /* ··· point and all the other points.*/ |
||
sd= (xj - @x.k)**2 + (yj - @y.k)**2 /*compute squared distance from points.*/ |
|||
if |
if sd<minD then parse value sd j k with minD A B |
||
end /*k*/ /* [↑] needn't take SQRT of |
end /*k*/ /* [↑] needn't take SQRT of SD (yet).*/ |
||
end /*j*/ /* [↑] when done, A & B are the points*/ |
end /*j*/ /* [↑] when done, A & B are the points*/ |
||
$= 'For ' N " points, the minimum distance between the two points: " |
$= 'For ' N " points, the minimum distance between the two points: " |
||
say $ center("x", w, '═')" " |
say $ center("x", w, '═')" " center('y', w, "═") ' is: ' sqrt( abs(minD)) / 1 |
||
say left('', length($) - 1) |
say left('', length($) - 1) "["right(@x.A, w)',' right(@y.A, w)"]" |
||
say left('', length($) - 1) |
say left('', length($) - 1) "["right(@x.B, w)',' right(@y.B, w)"]" |
||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |