Brownian tree: Difference between revisions

m
→‎{{header|REXX}}: tided up some code.
m (→‎{{header|REXX}}: elided a statement.)
m (→‎{{header|REXX}}: tided up some code.)
Line 3,703:
mote = '·' /*character for a loose mote (of dust).*/
hole = ' ' /* " " an empty spot in field.*/
clearScr = 'CLS' /*(DOS) command to clear the screen. */
seedPos = 0 /*if =0, then use middle of the field.*/
/* " -1, " " a random placement.*/
/*otherwise, place the seed at seedPos.*/
/*use RANDSEED for RANDOM repeatability*/
parse arg heightsd widthsw motes tree randSeed . /*obtain optional arguments from the CL*/
if height sd=='' | heightsd=="," then heightsd= 0 /*Not specified? Then use the default.*/
if width sw=='' | widthsw=="," then width then sw= 0 /* " " " " " " */
if motes=='' | motes=="," then motes= '2118%' /*The % dust motes in the field, */
/* [↑] either a # ─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 heightsd==0 | widthsw==0 then _= scrsize() /*Note: not all REXXes have SCRSIZE BIF*/
if heightsd==0 then heightsd= word(_, 1) -3 2 /*adjust usable height depth for the border. */
if sw==0 width==0 then widthsw= word(_, 2) - 1 /* " " width " " " */
seedAt= seedPos /*assume a seed position (initial pos).*/
if seedPos== 0 then seedAt= width%2(sw height% 2) (sd % 2) /*if it's a zero, start in the middle.*/
if seedPos==-1 then seedAt= random(1, widthsw) random(1,heightsd) /*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= heightsd * widthsw * strip(motes, , '%') % 100
@.= hole /*create the Brownian field, all empty.*/
do j=1 for motes /*sprinkle a # of dust motes randomly.*/
rx= random(1, widthsw); ry= random(1, heightsd); @.rx.ry= mote
end /*j*/ /* [↑] place a mote at random in field*/
/*plant thea seed from which the tree will grow from*/
@.xs.ys= tree /*dust motes that affix themselves to willthe growtree. from dust motes that */
@.xs.ys= tree call show; loX= 1; hiX= sw /*show field before we mess it affixed themselves to others. up again*/
call show; loX loY= 1; hiXhiY= widthsd /*show fieldused beforeto weoptimize messthe it upmote againsearching.*/
loY= 1; hiY= height /*used to optimize the mote searching.*/
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ soooo, this is Brownian motion.*/
do winksBrownian=1 until \motion; call show /*show Brownion motion until no motion.*/
motionminx= 0loX; maxX= call show hiX; loX= sw; hiX= 1 /*turnas offthe Browniantree motiongrows, flag;the search for show.the*/
minXminY= loXloY; maxXmaxY= hiX hiY; loY= sd; hiy= 1 /*as the tree grows, thedust searchmotes forgets faster due to croping*/
minY=call loY;BM maxY= hiY /* /*invoke the dustBrownian motesmovement gets fasterroutine. */
if loX= width;>1 & hiX= 1; <sw & loY=>1 height;& hiY<sd hiy= 1 then iterate /*used to limit the moteNeed searching.cropping? No, then keep moving*/
call crop /* [↓] delete motes (moved off petri field).*/
 
end /*winksBrownian*/ /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
do x =minX to maxX; xm= x - 1; xp= x + 1 /*two handy─dandy values. */
clearScrexit 0 = 'CLS' /*(DOS)stick a commandfork toin clearit, the screen.we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
crop: do yc=-1 to sd+1 by sd+2; do xc=-1 to widthsw+1; if @.xc.yc== hole then iterate; end @./*xc.yc= hole*/
end /*yc*/
do xc=-1 to sw+1 by sw+2; do yc=-1 to heightsd+1; if @.xc.yc== hole then iterate; end @.xc./*yc= hole*/
exit 0 end /*xc*/; /*stick a fork in it, we're all done. */return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: 'CLS'; motion= 0; do do ys=height sd for heightsd by -1; aRow=
do xs=1 for widthsw; aRow= aRow || @.xs.ys
loY= 1; hiY= height /*usedend to optimize the mote searching./*xs*/
end /*xs*/ say aRow
end /*xcys*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
BM: do x =minX to maxX; xm= x - 1; xp= x + 1 /*two 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? */
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 /* [↓] Brownian motion is coming. */
xb= x + random(1, 3) - 2 /* apply Brownian motion for X. */
Line 3,762 ⟶ 3,774:
@.x.y= hole /*"empty out" the old mote position. */
@.xb.yb= mote /*move the mote (or possibly not). */
if xb<loX then loX= max(1, xb); if xb>hiX then hiX= min( widthsw, xb)
if yb<loY then loY= max(1, yb); if yb>hiY then hiY= min(heightsd, yb)
end /*y*/ /* [↑] limit mote's movement to field.*/
end /*x*/; return</lang>
 
call crop /*crops (or truncates) the mote field.*/
end /*winks*/ /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
 
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
/*──────────────────────────────────────────────────────────────────────────────────────*/
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).