Anonymous user
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 ''petri dish'' option could be added, that is, when a particle hits the edge, <br>
it "bounces" back. Also, the field could then be displayed as a round area (like a petri dish).
Program note: to keep things simple, the (system) command to clear the screen was hard-coded as '''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.*/▼
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=
if width=='' | width=="," then width=
if motes=='' | motes=="," then motes= '
/* [↑] either a #
if tree=='' | tree==mote then 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()
if height==0 then height= word(_,
if width==0 then width= word(_,
seedAt= seedPos /*assume a seed position (initial pos).*/
if seedPos== 0 then seedAt= width%2 height%2
if seedPos==-1 then seedAt= random(1, width) random(1,height)
parse var seedAt xs ys . /*obtain the X and Y seed coördinates*/
/* [↓] if
if right(motes, 1)
@.=
▲
/*plant the seed from which the tree */
/* will grow from dust motes that */
@.xs.ys=
call show; loX= 1; hiX=
loX=1; hiX= width /*used to optimize the mote searching.*/▼
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ soooo, this is Brownian motion.*/
do winks=1
motion= 0; call
▲ minX=loX; maxX=hiX /*as the tree grows, the search for */
do x =minX to maxX; xm= x - 1;
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
if y<loY then loY=y; if y>hiY then hiY=y /*faster than
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=
xb= x + random(1, 3) - 2
yb= y + random(1, 3) - 2
if @.xb.yb\==hole then iterate /*can the mote actually move to there ?*/
@.x.y=
@.xb.yb=
if xb<loX then loX= max(1, xb);
if yb<loY then loY= max(1, yb);
end /*y*/ /* [↑] limit mote's movement to field.*/
end /*x*/
Line 3,787 ⟶ 3,772:
call show
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
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;
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;
end /*yc*/
end /*xc*/; 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 '''scrsize''' REXX program (or BIF) which is used to determine the screen size of the terminal (console). ▼
▲This REXX program makes use of '''scrsize''' REXX program (or BIF) which is used to determine the screen size of the terminal (console).
The '''SCRSIZE.REX''' REXX program is included at ───► [[SCRSIZE.REX]].▼
▲The '''SCRSIZE.REX''' REXX program is included at [[SCRSIZE.REX]].
▲Final output when using the following inputs (screen size was 160×160): <tt> , , , fe </tt>
▲(Shown at one-sixthth size.)
<pre style="font-size:17%;font-weight:bold;">
■
|