Brownian tree: Difference between revisions
Content deleted Content added
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 . /*
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
if width==0 then width=word(_, 2)-1
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*/
motion=0 /*turn off the Brownian motion flag. */
if snapshot\==0 then if winks//snapshot==0
if snaptime\==0 then do; t=time('S')
if t\==tim & t//snaptime==0 then do; tim=
end
minX=loX; maxX=hiX
minY=loY; maxY=hiY
loX= width; hiX=1
loY=height; hiY=1
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: hiX=max(X
if y<loY then loY=y; if y>hiY then hiY=y /*faster than: hiY=max(y
if @.xm.y ==tree then do;
if @.xp.y ==tree then do;
ym=y-1
if @.x.ym ==tree then do;
if @.xm.ym==tree then do;
if @.xp.ym==tree then do;
yp=y+1
if @.x.yp ==tree then do;
if @.xm.yp==tree then do;
if @.xp.yp==tree then do;
motion=1 /* [↓] Brownian motion is coming. */
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=hole /*"empty out" the old mote position. */
Line 3,358 ⟶ 3,355:
call crop /*crops (or truncates) the mote field.*/
end /*winks*/ /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
call show
Line 3,377 ⟶ 3,374:
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: clearScr /*¬ necessary, but everything speeds up*/
do ys=height
say aRow
end
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).
<br>The '''SCRSIZE.REX''' REXX program is included here ──► [[SCRSIZE.REX]]. <br><br>
|