Brownian tree: Difference between revisions

m
→‎{{header|REXX}}: fixed a typo, added/changed whitespace and comments, elided the timing and snapshot statements, changed the mote percentage.
(→‎{{header|Rust}}: Update library to rand = "^0.8")
m (→‎{{header|REXX}}: fixed a typo, added/changed whitespace and comments, elided the timing and snapshot statements, changed the mote percentage.)
Line 3,698:
With a little more REXX code, a &nbsp; ''petri dish'' &nbsp; option could be added, that is, when a particle hits the edge, <br>
it "bounces" back. &nbsp; Also, the field could then be displayed as a round area &nbsp; (like a petri dish).
 
REXX code was added to display snapshots of the field, either after so many cycles, and/or after some <br>
elapsed time has elapsed (whole seconds only). &nbsp; This makes for some fascinating observations.
 
Program note: &nbsp; to keep things simple, the (system) command to clear the screen was hard-coded as &nbsp; '''CLS'''.
Line 3,707 ⟶ 3,704:
hole = ' ' /* " " an empty spot in field.*/
clearScr = 'CLS' /*(DOS) command to clear the screen. */
eons = 1000000 /*number cycles for Brownian movement.*/
snapshot = 0 /*every N winks, display a snapshot.*/
snaptime = 1 /* " " secs, " " " */
seedPos = 30 45 /*place a seed in this field position. */
seedPos = 0 /*if =0, then use middle of the field.*/
Line 3,716 ⟶ 3,710:
/*use RANDSEED for RANDOM repeatability*/
parse arg height width motes tree randSeed . /*obtain optional arguments from the CL*/
if height=='' | height=="," then height=0 0 /*Not specified? Then use the default.*/
if width=='' | width=="," then width=0 0 /* " " " " " " */
if motes=='' | motes=="," then motes= '1021%' /*The % dust motes in the field, */
/* [↑] either a # -or-─or─ a # with a %.*/
if tree=='' | tree==mote then tree='*' "*" /*the character used to show the tree. */
if length(tree)==2 then tree=x2c(tree) /*tree character was specified in hex. */
if datatype(randSeed,'W') then call random ,,randSeed /*if an integer, use the seed.*/
/* [↑] 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 useableusable height for the border. */
if width==0 then width= word(_, 2)-1 /* " " width " " " */
seedAt= seedPos /*assume a seed position (initial pos).*/
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-mostright─most ≡ '%', then use %*/
if right(motes, 1) == '%' then motes= height * width * strip(motes, , '%') % 100
@.=hole hole /*create the Brownian field, all empty.*/
eons = 1000000 do j=1 for motes /*numbersprinkle cyclesa for # of dust Brownianmotes movementrandomly.*/
 
do j=1 for motes rx= random(1, width); ry= random(1, height); @.rx.ry= /*sprinkle a # of dust motes randomly.*/mote
rx=random(1, width); ry=random(1,end height); /*j*/ @.rx.ry= /* [↑] place a mote at random in field*/
end /*j*/ /* [↑] place a mote at random in field*/
/*plant the seed from which the tree */
/* will grow from dust motes that */
@.xs.ys=tree tree /* affixed themselves to others. */
call show; loX= 1; hiX= width /*show field before we mess it up again*/
tim=0 loY= 1; hiY= height /*theused timeto inoptimize secondsthe of lastmote displaysearching. */
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 /*show Brownion motion until no motion.*/
motion= 0; call show /*turn off the Brownian motion flag.; show.*/
minX= loX; maxX=hiX hiX /*as the tree grows, the search for */
if snapshot\==0 then if winks//snapshot==0 then call show
snapshot minY= 0loY; maxY= hiY /* /*every N winks,dust displaymotes agets snapshotfaster. */
if snaptime\==0 then do; t=time('S')
loX=1 width; hiX= width 1; loY= height; hiy= 1 /*used to optimizelimit the mote searching. */
if t\==tim & t//snaptime==0 then do; tim=t; call show
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 /*atwo couple handy-dandyhandy─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? */
if @.xp.y ==tree then do; @.x.y= tree; iterate; end /*there a neighbor of tree? */
ym= y - 1
if @.x.ym ==tree then do; @.x.y= tree; iterate; end /*there a neighbor of tree? */
if @.xm.ym==tree then do; @.x.y= tree; iterate; end /*there a neighbor of tree? */
if @.xp.ym==tree then do; @.x.y= tree; iterate; end /*there a neighbor of tree? */
yp = y + 1
if @.x.yp ==tree then do; @.x.y= tree; iterate; end /*there a neighbor of tree? */
if @.xm.yp==tree then do; @.x.y= tree; iterate; end /*there a neighbor of tree? */
if @.xp.yp==tree then do; @.x.y= tree; iterate; end /*there a neighbor of tree? */
motion=1 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 hole /*"empty out" the old mote position. */
@.xb.yb=mote mote /*move the mote (or possibly not). */
if xb<loX then loX= max(1, xb); if xb>hiX then hiX= min( width, xb)
if yb<loY then loY= max(1, yb); if yb>hiY then hiY= min(height, yb)
end /*y*/ /* [↑] limit mote's movement to field.*/
end /*x*/
Line 3,787 ⟶ 3,772:
 
call show
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
crop: if loX>1 & hiX<width & loY>1 & hiY<height then return /*are we cropping?*/
/* [↓] delete motes (moved off field).*/
do yc=-1 to height+1 by height+2
do xc=-1 to width+1; if @.xc.yc==hole then iterate; @.xc.yc= hole
end /*xc*/
end /*yc*/
/* [↓] delete motes (moved off field).*/
do xc=-1 to width+1 by width+2
do yc=-1 to height+1; if @.xc.yc==hole then iterate; @.xc.yc= hole
end /*yc*/
end /*xc*/; return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
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).
return</lang>
 
This REXX program makes use of '''scrsize''' REXX program (or BIF) which is used to determine the screen size of the terminal (console).
The &nbsp; '''SCRSIZE.REX''' &nbsp; REXX program is included at &nbsp; ───► &nbsp; [[SCRSIZE.REX]].
 
Final{{out|final output|text=&nbsp; when using the following inputs (screen size was 160&times;160): &nbsp; &nbsp; <tt> , &nbsp; , &nbsp; , &nbsp; fe </tt>}}
The '''SCRSIZE.REX''' REXX program is included at [[SCRSIZE.REX]].
 
(Shown at one-sixththone─sixth size.)
Final output when using the following inputs (screen size was 160&times;160): &nbsp; &nbsp; <tt> , &nbsp; , &nbsp; , &nbsp; fe </tt>
(Shown at one-sixthth size.)
<pre style="font-size:17%;font-weight:bold;">