Brownian tree: Difference between revisions

Content deleted Content added
Edmund (talk | contribs)
Added ZX81 BASIC
m →‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, optimized code for the Brownian movement.
Line 3,276:
Program note:   to keep things simple, the (system) command to clear the screen was hard-coded as   '''CLS'''.
<lang rexx>/*REXX program animates and displays Brownian motion of dust in a field (with one seed).*/
parse arg height width motes randSeed . /*getobtain argsoptional arguments from the C.L. CL*/
if height=='' | height=="," then height=0 /*Not specified? Then use the default.*/
if width=='' | width=="," then width=0 /* " " " " " " */
Line 3,296:
/* [↑] set the first random number. */
if height==0 | width==0 then _=scrsize() /*Note: not all REXXes have SCRSIZE BIF*/
if height==0 then height=word(_, 1)-3 /*adjust useable height for the border.*/
if width==0 then width=word(_, 2)-1 /* " " width " " " */
 
seedAt=seedPos
if seedPos== 0 then seedAt=width%2 height%2 /*if it's a zero, start in the middle.*/
if seedPos==-1 then seedAt=random(1, width) random(1,height) /*if negative, use random.*/
parse var seedAt xs ys . /*obtain the X and Y seed coördinates*/
/* [↓] if right-most ≡ '%', then use %*/
if right(motes, 1) == '%' then motes=height * width * strip(motes, , '%') % 100
@.=hole /*create the Brownian field, all empty.*/
 
do j=1 for motes /*sprinkle a # of dust motes randomly.*/
rx=random(1, width); ry=random(1, height); @.rx.ry=mote
end /*j*/ /* [↑] place a mote at random in field*/
/*plant the seed from which the tree */
Line 3,317 ⟶ 3,316:
loX=1; hiX= width /*used to optimize the mote searching.*/
loY=1; hiY=height /* " " " " " " */
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ soooo, this is Brownian motion.*/
 
do winks=1 for eons until \motion /*EONs is used instead of ∞,; close 'nuf*/
/*═══════════════════════════════════════ soooo, this is Brownian motion. */
do winks=1 for eons until \motion /*EONs is used instead of ∞, close 'nuf*/
motion=0 /*turn off the Brownian motion flag. */
if snapshot\==0 then if winks//snapshot==0 then call show
if snaptime\==0 then do; t=time('S')
if t\==tim & t//snaptime==0 then do; tim=time('s')t; call show
call showend
end
end
minX=loX; maxX=hiX /*as the tree grows, the search for */
minY=loY; maxY=hiY /* dust motes gets faster. */
loX= width; hiX=1 /*used to limit the mote searching. */
loY=height; hiY=1 /* " " " " " " */
 
do x =minX to maxX; xm=x-1; xp=x+1 xp=x+1 /*a couple handy-dandy values*/
do y=minY to maxY; if @.x.y\==mote then iterate /*Not a mote: keep looking. */
if x<loX then loX=x; if x>hiX then hiX=x /*faster than: hiX=max(X ,hiX)*/
if y<loY then loY=y; if y>hiY then hiY=y /*faster than: hiY=max(y ,hiY)*/
if @.xm.y ==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
if @.xp.y ==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
ym=y-1
if @.x.ym ==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
if @.xm.ym==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
if @.xp.ym==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
yp=y+1
if @.x.yp ==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
if @.xm.yp==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
if @.xp.yp==tree then do; @.x.y=tree; iterate; end /*there a neighbor of tree? /*neighbor?*/
motion=1 /* [↓] Brownian motion is coming. */
xb=x + random(1, 3) - 2 /* apply Brownian motion for X. */
yb=y + random(1, 3) - 2 /* " " " " Y. */
if @.xb.yb\==hole then iterate /*can the mote actually move to there ?*/
@.x.y=hole /*"empty out" the old mote position. */
Line 3,358 ⟶ 3,355:
 
call crop /*crops (or truncates) the mote field.*/
end /*winks*/ /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
end /*winks*/
 
call show
Line 3,377 ⟶ 3,374:
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: clearScr /*¬ necessary, but everything speeds up*/
do ys=height for height by -1; aRow=
do xs=1 for width; aRow=aRow || @.xs.ys
end /*xs*/
say aRow
end /*ys*/
return</lang>
This REXX program makes use of &nbsp; '''scrsize''' &nbsp; REXX program (or BIF) which is used to determine the screen size of the terminal (console).
<br>The &nbsp; '''SCRSIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[SCRSIZE.REX]]. <br><br>