Closest-pair problem: Difference between revisions

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