September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,4 +1,4 @@
[[File:Brownian_tree.jpg|600px||right]]
[[File:Brownian_tree.jpg|240px||right]]
;Task:

View file

@ -0,0 +1,60 @@
' version 16-03-2017
' compile with: fbc -s gui
Const As ULong w = 400
Const As ULong w1 = w -1
Dim As Long x, y, lastx, lasty
Dim As Long count, max = w * w \ 4
ScreenRes w, w, 8 ' windowsize 400 * 400, 8bit
' hit any key to stop or mouseclick on close window [X]
WindowTitle "hit any key to stop and close the window"
Palette 0, 0 ' black
Palette 1, RGB( 1, 1, 1) ' almost black
Palette 2, RGB(255, 255, 255) ' white
Palette 3, RGB( 0, 255, 0) ' green
Line (0, 0) - (w1, w1), 0, BF ' make field black
Line (0, 0) - (w1, w1), 1, B ' draw border in almost black color
Randomize Timer
x = Int(Rnd * 11) - 5
y = Int(Rnd * 11) - 5
PSet(w \ 2 + x, w \ 2 + y), 3 ' place seed near center
Do
Do ' create new particle
x = Int(Rnd * w1) + 1
y = Int(Rnd * w1) + 1
Loop Until Point(x, y) = 0 ' black
PSet(x, y), 2
Do
lastx = x
lasty = y
Do
x = lastx + Int(Rnd * 3) -1
y = lasty + Int(Rnd * 3) -1
Loop Until Point(x, y) <> 1
If Point(x, y) = 3 Then
PSet(lastx, lasty), 3
Exit Do ' exit do loop and create new particle
End If
PSet(lastx, lasty), 0
PSet(x,y), 2
If Inkey <> "" Or Inkey = Chr(255) + "k" Then
End
End If
Loop
count += 1
Loop Until count > max
Beep : Sleep 5000
End

View file

@ -0,0 +1,14 @@
## plotff.gp 11/27/16 aev
## Plotting from any data-file with 2 columns (space delimited), and writing to png-file.
## Especially useful to plot colored fractals using points.
## Note: assign variables: clr, filename and ttl (before using load command).
reset
set terminal png font arial 12 size 640,640
ofn=filename.".png"
set output ofn
unset border; unset xtics; unset ytics; unset key;
set size square
dfn=filename.".dat"
set title ttl font "Arial:Bold,12"
plot dfn using 1:2 with points pt 7 ps 0.5 lc @clr
set output

View file

@ -0,0 +1,41 @@
## BTff.gp 11/27/16 aev
## Plotting 6 Brownian tree pictures.
## dat-files are PARI/GP generated output files:
## http://rosettacode.org/wiki/Brownian_tree#PARI.2FGP
#cd 'C:\gnupData'
##BT1
clr = '"dark-green"'
filename = "BTAH1"
ttl = "Brownian Tree v.#1"
load "plotff.gp"
##BT2
clr = '"brown"'
filename = "BTOC1"
ttl = "Brownian Tree v.#2"
load "plotff.gp"
##BT3
clr = '"navy"'
filename = "BTSE1"
ttl = "Brownian Tree v.#3"
load "plotff.gp"
##BT41
clr = '"red"'
filename = "BTPB1"
ttl = "Brownian Tree v.#4-1"
load "plotff.gp"
##BT42
clr = '"red"'
filename = "BTPB2"
ttl = "Brownian Tree v.#4-2"
load "plotff.gp"
##BT43
clr = '"red"'
filename = "BTPB3"
ttl = "Brownian Tree v.#4-3"
load "plotff.gp"

View file

@ -0,0 +1,64 @@
// version 1.1.2
import java.awt.Graphics
import java.awt.image.BufferedImage
import java.util.*
import javax.swing.JFrame
class BrownianTree : JFrame("Brownian Tree"), Runnable {
private val img: BufferedImage
private val particles = LinkedList<Particle>()
private companion object {
val rand = Random()
}
private inner class Particle {
private var x = rand.nextInt(img.width)
private var y = rand.nextInt(img.height)
/* returns true if either out of bounds or collided with tree */
fun move(): Boolean {
val dx = rand.nextInt(3) - 1
val dy = rand.nextInt(3) - 1
if ((x + dx < 0) || (y + dy < 0) || (y + dy >= img.height) ||
(x + dx >= img.width)) return true
x += dx
y += dy
if ((img.getRGB(x, y) and 0xff00) == 0xff00) {
img.setRGB(x - dx, y - dy, 0xff00)
return true
}
return false
}
}
init {
setBounds(100, 100, 400, 300)
defaultCloseOperation = EXIT_ON_CLOSE
img = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
img.setRGB(img.width / 2, img.height / 2, 0xff00)
}
override fun paint(g: Graphics) {
g.drawImage(img, 0, 0, this)
}
override fun run() {
(0 until 20000).forEach { particles.add(Particle()) }
while (!particles.isEmpty()) {
val iter = particles.iterator()
while (iter.hasNext()) {
if (iter.next().move()) iter.remove()
}
repaint()
}
}
}
fun main(args: Array<String>) {
val b = BrownianTree()
b.isVisible = true
Thread(b).start()
}

View file

@ -1,11 +1,28 @@
\\ 2 plotting helper functions 3/2/16 aev
\\ insm(): x,y are inside matrix mat (+/- p deep).
\\ 2 old plotting helper functions 3/2/16 aev
\\ insm(): Check if x,y are inside matrix mat (+/- p deep).
insm(mat,x,y,p=0)={my(xz=#mat[1,],yz=#mat[,1]);
return(x+p>0 && x+p<=xz && y+p>0 && y+p<=yz && x-p>0 && x-p<=xz && y-p>0 && y-p<=yz)}
\\ plotmat(): Simple plotting using matrix mat (filled with 0/1).
\\ plotmat(): Simple plotting using a square matrix mat (filled with 0/1).
plotmat(mat)={
my(xz=#mat[1,],yz=#mat[,1],vx=List(),vy=vx,x,y);
for(i=1,yz, for(j=1,xz, if(mat[i,j]==0, next, listput(vx,i); listput(vy,j))));
plothraw(Vec(vx),Vec(vy));
print(" *** matrix(",xz,"x",yz,") ",#vy, " DOTS");
my(xz=#mat[1,],yz=#mat[,1],vx=List(),vy=vx,x,y);
for(i=1,yz, for(j=1,xz, if(mat[i,j]==0, next, listput(vx,i); listput(vy,j))));
print(" *** matrix(",xz,"x",yz,") ",#vy, " DOTS");
plothraw(Vec(vx),Vec(vy));
}
\\ 2 new plotting helper functions 11/27/16 aev
\\ wrtmat(): Writing file fn containing X,Y coordinates from matrix mat.
\\ Created primarily for using file in Gnuplot, also for re-plotting.
wrtmat(mat, fn)={
my(xz=#mat[1,],yz=#mat[,1],ws,d=0);
for(i=1,yz, for(j=1,xz, if(mat[i,j]==0, next, d++; ws=Str(i," ",j); write(fn,ws))));
print(" *** matrix(",xz,"x",yz,") ",d, " DOTS put in ",fn);
}
\\ plotff(): Plotting from a file written by the wrtmat().
\\ Saving possibly huge generation time if re-plotting needed.
plotff(fn)={
my(F,nf,vx=List(),vy=vx,Vr);
F=readstr(fn); nf=#F;
print(" *** Plotting from: ", fn, " - ", nf, " DOTS");
for(i=1,nf, Vr=stok(F[i],","); listput(vx,eval(Vr[1])); listput(vy,eval(Vr[2])));
plothraw(Vec(vx),Vec(vy));
}

View file

@ -1,20 +1,26 @@
\\ 3/8/2016
BrownianTree1(size,lim)={
my(Myx=matrix(size,size),sz=size-1,sz2=sz\2,x,y,ox,oy);
x=sz2; y=sz2; Myx[y,x]=1; \\ seed in center
print(" *** START: ",x,"/",y);
for(i=1,lim,
x=random(sz)+1; y=random(sz)+1;
while(1,
ox=x; oy=y;
x+=random(3)-1; y+=random(3)-1;
if(insm(Myx,x,y)&&Myx[y,x],
if(insm(Myx,ox,oy), Myx[oy,ox]=1; break));
if(!insm(Myx,x,y), break);
);\\wend
);\\ fend i
plotmat(Myx);
}
{\\ Executing:
BrownianTree1(400,15000);
\\ Brownian tree v.#1. Translated from AutoHotkey
\\ 3/8/2016, upgraded 11/27/16 aev
\\ Where: size - size of a square matrix; lim - limit of testing dots;
\\ fn - file name (fn=""-only plot, fn!=""-only writing file)..
BrownianTree1(size,lim, fn="")={
my(Myx=matrix(size,size),sz=size-1,sz2=sz\2,x,y,ox,oy);
x=sz2; y=sz2; Myx[y,x]=1; \\ seed in center
print(" *** BT1 SEED: ",x,"/",y);
for(i=1,lim,
x=random(sz)+1; y=random(sz)+1;
while(1,
ox=x; oy=y;
x+=random(3)-1; y+=random(3)-1;
if(insm(Myx,x,y)&&Myx[y,x],
if(insm(Myx,ox,oy), Myx[oy,ox]=1; break));
if(!insm(Myx,x,y), break);
);\\wend
);\\ fend i
if(fn=="", plotmat(Myx), wrtmat(Myx, fn));
}
\\ Executing 1 or 2 lines below:
BrownianTree1(400,15000); \\BTAH1.png
{BrownianTree1(400,15000,"c:\\pariData\\BTAH1.dat");
plotff("c:\\pariData\\BTAH1.dat");} \\BTAH1.png

View file

@ -1,18 +1,24 @@
\\ 3/11/2016
BrownianTree2(size,lim)={
my(Myx=matrix(size,size),sz=size-1,dx,dy,x,y);
x=random(sz); y=random(sz); Myx[y,x]=1; \\ random seed
print(" *** START: ",x,"/",y);
for(i=1,lim,
x=random(sz)+1; y=random(sz)+1;
while(1,
dx=random(3)-1; dy=random(3)-1;
if(!insm(Myx,x+dx,y+dy), x=random(sz)+1; y=random(sz)+1,
if(Myx[y+dy,x+dx], Myx[y,x]=1; break, x+=dx; y+=dy));
);\\wend
);\\fend i
plotmat(Myx);
}
{\\ Executing:
BrownianTree2(1000,3000);
\\ Brownian tree v.#2. Translated from Octave
\\ 3/8/2016, upgraded 11/27/16 aev
\\ Where: size - size of a square matrix; lim - limit of testing dots;
\\ fn - file name (fn=""-only plot, fn!=""-only writing file)..
BrownianTree2(size,lim, fn="")={
my(Myx=matrix(size,size),sz=size-1,dx,dy,x,y);
x=random(sz); y=random(sz); Myx[y,x]=1; \\ random seed
print(" *** BT2 SEED: ",x,"/",y);
for(i=1,lim,
x=random(sz)+1; y=random(sz)+1;
while(1,
dx=random(3)-1; dy=random(3)-1;
if(!insm(Myx,x+dx,y+dy), x=random(sz)+1; y=random(sz)+1,
if(Myx[y+dy,x+dx], Myx[y,x]=1; break, x+=dx; y+=dy));
);\\wend
);\\fend i
if(fn=="", plotmat(Myx), wrtmat(Myx, fn));
}
\\ Executing 1 or 2 lines below:
BrownianTree2(1000,3000); \\BTOC1.png
{BrownianTree2(1000,3000,"c:\\pariData\\BTOC1.dat");
plotff("c:\\pariData\\BTOC1.dat");} \\BTOC1.png

View file

@ -1,9 +1,12 @@
\\ 3/14/2016
BrownianTree3(size,lim)={
my(Myx=matrix(size,size),sz=size-2,x,y,dx,dy,b=0);
x=random(sz); y=random(sz); Myx[y,x]=1; \\ random seed
print("*** START: ", x,"/",y);
for(i=1,lim,
\\ Brownian tree v.#3. Translated from Seed7
\\ 3/8/2016, upgraded 11/27/16 aev
\\ Where: size - size of a square matrix; lim - limit of testing dots;
\\ fn - file name (fn=""-only plot, fn!=""-only writing file)..
BrownianTree3(size,lim, fn="")={
my(Myx=matrix(size,size),sz=size-2,x,y,dx,dy,b=0);
x=random(sz); y=random(sz); Myx[y,x]=1; \\ random seed
print("*** BT3 SEED: ", x,"/",y);
for(i=1,lim,
x=random(sz); y=random(sz);
b=0; \\ bumped not
while(!b,
@ -12,9 +15,12 @@ for(i=1,lim,
if(Myx[y+dy,x+dx]==1, Myx[y,x]=1; b=1, x+=dx; y+=dy);
);
);\\wend
);\\fend i
plotmat(Myx);
}
{\\ Executing:
BrownianTree3(400,5000);
);\\fend i
if(fn=="", plotmat(Myx), wrtmat(Myx, fn));
}
\\ Executing 1 or 2 lines below:
BrownianTree3(400,5000); \\BTSE1.png
{BrownianTree3(400,5000,"c:\\pariData\\BTSE1.dat");
plotff("c:\\pariData\\BTSE1.dat");} \\BTSE1.png

View file

@ -1,27 +1,39 @@
\\ 3/17/2016
\\ Brownian tree v.#4. Translated from PureBasic
\\ 3/8/2016, upgraded 11/27/16 aev
\\ Where: size - size of a square matrix; lim - limit of testing dots;
\\ fn - file name (fn=""-only plot, fn!=""-only writing file)..
\\ s=1/2(random seed/seed in the center); p=0..n (level of the "deep" checking).
BrownianTree4(size,lim,s=1,p=0)={
my(Myx=matrix(size,size),sz=size-3,x,y);
\\ seed s=1 for BTPB1, s=2 for BTPB2, BTPB3
if(s==1,x=random(sz); y=random(sz), x=sz\2; y=sz\2); Myx[y,x]=1;
print(" *** START: ",x,"/",y);
for(i=1,lim,
if(!(i==1&&s==2), x=random(sz)+1; y=random(sz)+1);
while(insm(Myx,x,y,1)&&
BrownianTree4(size,lim, fn="",s=1,p=0)={
my(Myx=matrix(size,size),sz=size-3,x,y);
\\ seed s=1 for BTPB1, s=2 for BTPB2, BTPB3
if(s==1,x=random(sz); y=random(sz), x=sz\2; y=sz\2); Myx[y,x]=1;
print(" *** BT4 SEED: ",x,"/",y);
for(i=1,lim,
if(!(i==1&&s==2), x=random(sz)+1; y=random(sz)+1);
while(insm(Myx,x,y,1)&&
(Myx[y+1,x+1]+Myx[y+1,x]+Myx[y+1,x-1]+Myx[y,x+1]+
Myx[y-1,x-1]+Myx[y,x-1]+Myx[y-1,x]+Myx[y-1,x+1])==0,
x+=random(3)-1; y+=random(3)-1;
\\ p=0 for BTPB1, BTPB2; p=5 for BTPB3
if(!insm(Myx,x,y,p), x=random(sz)+1; y=random(sz)+1;);
);\\wend
Myx[y,x]=1;
);\\fend i
plotmat(Myx);
x+=random(3)-1; y+=random(3)-1;
\\ p=0 for BTPB1, BTPB2; p=5 for BTPB3
if(!insm(Myx,x,y,p), x=random(sz)+1; y=random(sz)+1;);
);\\wend
Myx[y,x]=1;
);\\fend i
if(fn=="", plotmat(Myx), wrtmat(Myx, fn));
}
\\ Executing 1 or 2 lines below:
{
\\ Executing:
BrownianTree4(200,4000); \\BTPB1.png
BrownianTree4(200,4000,2); \\BTPB2.png
BrownianTree4(200,4000,2,5); \\BTPB3.png
}
BrownianTree4(200,4000); \\BTPB1.png
{BrownianTree4(200,4000,"c:\\pariData\\BTPB1.dat");
plotff("c:\\pariData\\BTPB1.dat");} \\BTPB1.png
BrownianTree4(200,4000,,2); \\BTPB2.png
{BrownianTree4(200,4000,"c:\\pariData\\BTPB2.dat",2);
plotff("c:\\pariData\\BTPB2.dat");} \\BTPB2.png
BrownianTree4(200,4000,,2,5); \\BTPB3.png
{BrownianTree4(200,4000,"c:\\pariData\\BTPB3.dat",2,5);
plotff("c:\\pariData\\BTPB3.dat");} \\BTPB3.png

View file

@ -1,4 +1,7 @@
include ..\pGUI\pGUI.e
--
-- demo\rosetta\BrownianTree.exw
--
include pGUI.e
Ihandle dlg, canvas
cdCanvas cddbuffer, cdcanvas
@ -50,7 +53,7 @@ function esc_close(Ihandle /*ih*/, atom c)
end function
procedure main()
IupOpen("..\\pGUI\\")
IupOpen()
canvas = IupCanvas(NULL)
IupSetAttribute(canvas, "RASTERSIZE", "200x200") -- fixed size

View file

@ -0,0 +1,39 @@
# plotmat(): Simple plotting using a square matrix mat (filled with 0/1). v. 8/31/16
# Where: mat - matrix; fn - file name; clr - color; ttl - plot title;
# dflg - writing dump file flag (0-no/1-yes): psz - picture size.
plotmat <- function(mat, fn, clr, ttl, dflg=0, psz=600) {
m <- nrow(mat); d <- 0;
X=NULL; Y=NULL;
pf = paste0(fn, ".png"); df = paste0(fn, ".dmp");
for (i in 1:m) {
for (j in 1:m) {if(mat[i,j]==0){next} else {d=d+1; X[d] <- i; Y[d] <- j;} }
};
cat(" *** Matrix(", m,"x",m,")", d, "DOTS\n");
# Dumping if requested (dflg=1).
if (dflg==1) {dump(c("X","Y"), df); cat(" *** Dump file:", df, "\n")};
# Plotting
plot(X,Y, main=ttl, axes=FALSE, xlab="", ylab="", col=clr, pch=20);
dev.copy(png, filename=pf, width=psz, height=psz);
# Cleaning
dev.off(); graphics.off();
}
# plotv2(): Simple plotting using 2 vectors (dumped into ".dmp" file by plotmat()).
# Where: fn - file name; clr - color; ttl - plot title; psz - picture size.
# v. 8/31/16
plotv2 <- function(fn, clr, ttl, psz=600) {
cat(" *** START:", date(), "clr=", clr, "psz=", psz, "\n");
cat(" *** File name -", fn, "\n");
pf = paste0(fn, ".png"); df = paste0(fn, ".dmp");
source(df);
d <- length(X);
cat(" *** Source dump-file:", df, d, "DOTS\n");
cat(" *** Plot file -", pf, "\n");
# Plotting
plot(X, Y, main=ttl, axes=FALSE, xlab="", ylab="", col=clr, pch=20);
# Writing png-file
dev.copy(png, filename=pf, width=psz, height=psz);
# Cleaning
dev.off(); graphics.off();
cat(" *** END:", date(), "\n");
}

View file

@ -0,0 +1,33 @@
# Generate and plot Brownian tree. Version #1.
# 7/27/16 aev
# gpBrownianTree1(m, n, clr, fn, ttl, dflg, psz)
# Where: m - defines matrix m x m; n - limit of the number of moves;
# fn - file name (.ext will be added); ttl - plot title; dflg - 0-no dump,
# 1-dump: psz - picture size.
gpBrownianTree1 <- function(m, n, clr, fn, ttl, dflg=0, psz=600)
{
cat(" *** START:", date(), "m=",m, "n=",n, "clr=",clr, "psz=", psz, "\n");
M <- matrix(c(0), ncol=m, nrow=m, byrow=TRUE);
# Seed in center
x <- m%/%2; y <- m%/%2;
M[x,y]=1;
pf=paste0(fn, ".png");
cat(" *** Plot file -", pf, "\n");
# Main loops
for (i in 1:n) {
if(i>1) {
x <- sample(1:m, 1, replace=FALSE)
y <- sample(1:m, 1, replace=FALSE)}
while(1) {
ox = x; oy = y;
x <- x + sample(-1:1, 1, replace=FALSE);
y <- y + sample(-1:1, 1, replace=FALSE);
if(x<=m && y<=m && x>0 && y>0 && M[x,y])
{if(ox<=m && oy<=m && ox>0 && oy>0) {M[ox,oy]=1; break}}
if(!(x<=m && y<=m && x>0 && y>0)) {break}
}
}
plotmat(M, fn, clr, ttl, dflg, psz);
cat(" *** END:",date(),"\n");
}
gpBrownianTree1(400,15000,"red", "BT1R", "Brownian Tree v.1", 1);

View file

@ -0,0 +1,37 @@
# Generate and plot Brownian tree. Version #2.
# 7/27/16 aev
# gpBrownianTree2(m, n, clr, fn, ttl, dflg, psz)
# Where: m - defines matrix m x m; n - limit of the number of moves;
# fn - file name (.ext will be added); ttl - plot title; dflg - 0-no dump,
# 1-dump; psz - picture size.
gpBrownianTree2 <- function(m, n, clr, fn, ttl, dflg=0, psz=600)
{
cat(" *** START:", date(), "m=",m, "n=",n, "clr=",clr, "psz=", psz, "\n");
M <- matrix(c(0), ncol=m, nrow=m, byrow=TRUE);
# Random seed always
x <- sample(1:m, 1, replace=FALSE); y <- sample(1:m, 1, replace=FALSE);
M[x,y]=1;
pf=paste0(fn,".png");
cat(" *** Plot file -",pf,"Seed:",x,"/",y,"\n");
# Main loops
for (i in 1:n) {
if(i>1) {
x <- sample(1:m, 1, replace=FALSE)
y <- sample(1:m, 1, replace=FALSE)}
while(1) {
dx <- sample(-1:1, 1, replace=FALSE);
dy <- sample(-1:1, 1, replace=FALSE);
nx=x+dx; ny=y+dy;
if(!(nx<=m && ny<=m && nx>0 && ny>0)) {
x <- sample(1:m, 1, replace=FALSE); y <- sample(1:m, 1, replace=FALSE)}
else {if(M[nx,ny]) {M[x,y]=1; break}
else{x=nx; y=ny;}}
}
}
plotmat(M, fn, clr, ttl, dflg, psz);
cat(" *** END:",date(),"\n");
}
gpBrownianTree2(400,5000,"brown", "BT2R", "Brownian Tree v.2", 1);
## Rename BT2R.dmp to BT2aR.dmp
plotv2("BT2aR", "orange", "Brownian Tree v.2a", 640)

View file

@ -0,0 +1,39 @@
# Generate and plot Brownian tree. Version #3.
# 7/27/16 aev
# gpBrownianTree3(m, n, clr, fn, ttl, dflg, seed, psz):
# Where: m - defines matrix m x m; n - limit of the number of moves;
# fn - file name (.ext will be added); ttl - plot title; dflg - 0-no dump,
# 1-dump; seed - 0-center, 1-random: psz - picture size.
gpBrownianTree3 <- function(m, n, clr, fn, ttl, dflg=0, seed=0, psz=600)
{
cat(" *** START:", date(),"m=",m,"n=",n,"clr=",clr, "psz=",psz, "\n");
M <- matrix(c(0), ncol=m, nrow=m, byrow=TRUE);
# Random seed
if(seed==1)
{x <- sample(1:m, 1, replace=FALSE);y <- sample(1:m, 1, replace=FALSE)}
# Seed in center
else {x <- m%/%2; y <- m%/%2}
M[x,y]=1;
pf=paste0(fn,". png");
cat(" *** Plot file -", pf, "Seed:",x,"/",y, "\n");
# Main loops
for (i in 1:n) {
if(i>1) {
x <- sample(1:m, 1, replace=FALSE)
y <- sample(1:m, 1, replace=FALSE)}
b <- 0;
while(b==0) {
dx <- sample(-1:1, 1, replace=FALSE)
dy <- sample(-1:1, 1, replace=FALSE)
if(!(x+dx<=m && y+dy<=m && x+dx>0 && y+dy>0))
{ x <- sample(1:m, 1, replace=FALSE)
y <- sample(1:m, 1, replace=FALSE)
}
else{if(M[x+dx,y+dy]==1) {M[x,y]=1; b=1}
else {x=x+dx; y=y+dy;} }
}
}
plotmat(M, fn, clr, ttl, dflg, psz);
cat(" *** END:", date(), "\n");
}
gpBrownianTree3(400,5000,"dark green", "BT3R", "Brownian Tree v.3", 1);

View file

@ -0,0 +1,41 @@
# Generate and plot Brownian tree. Version #4.
# 7/27/16 aev
# gpBrownianTree4(m, n, clr, fn, ttl, dflg, seed, psz)
# Where: m - defines matrix m x m; n - limit of the number of moves;
# fn - file name (.ext will be added); ttl - plot title; dflg - 0-no dump,
# 1-dump; seed - 0-center, 1-random: psz - picture size.
gpBrownianTree4 <- function(m, n, clr, fn, ttl, dflg=0, seed=0, psz=600)
{
cat(" *** START:", date(), "m=",m, "n=",n, "clr=",clr, "psz=",psz, "\n");
M <- matrix(c(0), ncol=m, nrow=m, byrow=TRUE);
# Random seed
if(seed==1)
{x <- sample(1:m, 1, replace=FALSE);y <- sample(1:m, 1, replace=FALSE)}
# Seed in center
else {x <- m%/%2; y <- m%/%2}
M[x,y]=1;
pf=paste0(fn,".png");
cat(" *** Plot file -",pf,"Seed:",x,"/",y,"\n");
# Main loops
for (i in 1:n) {
if(i>1) {
x <- sample(1:m, 1, replace=FALSE)
y <- sample(1:m, 1, replace=FALSE)}
while((x<=m && y<=m && x>0 && y>0)) {
if(!(x+1<=m && y+1<=m && x-1>0 && y-1>0)) {break;}
b=M[x+1,y+1]+M[x,y+1]+M[x-1,y+1]+M[x+1,y];
b=b+M[x-1,y-1]+M[x-1,y]+M[x,y-1]+M[x+1,y-1];
if(b!=0) {break;}
x <- x + sample(-1:1, 1, replace=FALSE)
y <- y + sample(-1:1, 1, replace=FALSE)
if(!(x<=m && y<=m && x>0 && y>0))
{ x <- sample(1:m, 1, replace=FALSE)
y <- sample(1:m, 1, replace=FALSE)
}
}
M[x,y]=1;
}
plotmat(M, fn, clr, ttl, dflg, psz);
cat(" *** END:",date(),"\n");
}
gpBrownianTree4(400,15000,"navy", "BT4R", "Brownian Tree v.4", 1);

View file

@ -1,37 +1,37 @@
/*REXX program animates and displays Brownian motion of dust in a field (with one seed).*/
parse arg height width motes randSeed . /*get args from the C.L. */
if height=='' | height=="," then height=0 /*Not specified? Then use the default.*/
if width=='' | width=="," then width=0 /* " " " " " " */
if motes=='' | motes=="," then motes='10%' /*The % dust motes in the field, */
/* [↑] either a # -or- a # with a %.*/
tree = '*' /*an affixed dust speck, start of tree.*/
mote = '·' /*character for a loose mote (of dust).*/
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, " " " */
snaptime = 1 /* " " secs, " " " */
seedPos = 30 45 /*place a seed in this field position. */
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 height width motes tree randSeed . /*obtain optional arguments from the CL*/
if height=='' | height=="," then height=0 /*Not specified? Then use the default.*/
if width=='' | width=="," then width=0 /* " " " " " " */
if motes=='' | motes=="," then motes='10%' /*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 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 " " " */
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.*/
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
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
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 */
/* will grow from dust motes that */
@ -40,38 +40,36 @@ call show /*show field before we mess it
tim=0 /*the time in seconds of last display. */
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')
call show
end
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=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 /* " " " " " " */
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 /*a couple handy-dandy values*/
do x =minX to maxX; xm=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 /*neighbor?*/
if @.xp.y ==tree then do; @.x.y=tree; iterate; end /*neighbor?*/
ym=y-1
if @.x.ym ==tree then do; @.x.y=tree; iterate; end /*neighbor?*/
if @.xm.ym==tree then do; @.x.y=tree; iterate; end /*neighbor?*/
if @.xp.ym==tree then do; @.x.y=tree; iterate; end /*neighbor?*/
yp=y+1
if @.x.yp ==tree then do; @.x.y=tree; iterate; end /*neighbor?*/
if @.xm.yp==tree then do; @.x.y=tree; iterate; end /*neighbor?*/
if @.xp.yp==tree then do; @.x.y=tree; iterate; end /*neighbor?*/
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. */
yb=y + random(1, 3) - 2 /* " " " " Y. */
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. */
@.xb.yb=mote /*move the mote (or possibly not). */
@ -81,7 +79,7 @@ loY=1; hiY=height /* " " " " "
end /*x*/
call crop /*crops (or truncates) the mote field.*/
end /*winks*/
end /*winks*/ /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
call show
exit /*stick a fork in it, we're all done. */
@ -100,9 +98,9 @@ crop: if loX>1 & hiX<width & loY>1 & hiY<height then return /*are we c
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*/
do ys=height for height by -1; aRow=
do xs=1 for width; aRow=aRow || @.xs.ys
end /*xs*/
say aRow
end /*ys*/
return
end /*ys*/
return

View file

@ -0,0 +1,47 @@
import <Utilities/Random.sl>;
import <Utilities/Sequence.sl>;
POINT ::= (X: int, Y: int);
RET_VAL ::= (World: int(2), Rand: RandomGenerator<int, int>, Point: POINT);
randomWalk(x, y, world(2), rand) :=
let
randX := getRandom(rand);
randY := getRandom(randX.Generator);
nextX := x + (randX.Value mod 3) - 1;
nextY := y + (randY.Value mod 3) - 1;
newStartX := (randX.Value mod (size(world) - 2)) + 2;
newStartY := (randY.Value mod (size(world) - 2)) + 2;
numNeighbors := world[y-1,x-1] + world[y-1,x] + world[y-1,x+1] +
world[y,x-1] + world[y,x+1] +
world[y+1,x-1] + world[y+1,x] + world[y+1,x+1];
outOfBounds := nextX <= 1 or nextY <= 1 or nextX >= size(world) or nextY >= size(world);
in
randomWalk(newStartX, newStartY, world, randY.Generator) when world[y,x] = 1 or outOfBounds
else
(X: x, Y: y) when numNeighbors > 0
else
randomWalk(nextX, nextY, world, randY.Generator);
step(rand, world(2)) :=
let
walkSeed := getRandom(rand);
newParticle := randomWalk(size(world)/2,size(world)/2, world, seedRandom(walkSeed.Value));
newWorld[j] :=
world[j] when j /= newParticle.Y
else
setElementAt(world[j], newParticle.X, 1);
in
(World: newWorld, Rand: walkSeed.Generator, Point: newParticle);
initialWorld(worldSize, seed) :=
let
world[i,j] := 1 when i = worldSize / 2 and j = worldSize / 2 else 0
foreach i within 1 ... worldSize,
j within 1 ... worldSize;
in
(World: world, Rand: seedRandom(seed), Point: (X: worldSize / 2, Y: worldSize / 2));

View file

@ -0,0 +1,44 @@
#include <time.h>
#include <cstdlib>
#include "CImg.h"
#include "SL_Generated.h"
using namespace std;
using namespace cimg_library;
int main(int argc, char ** argv)
{
int threads = 0;
int worldSize = 300; if(argc > 1) worldSize = atoi(argv[1]);
int seed = time(NULL); if(argc > 2) seed = atoi(argv[2]);
int scale = 2; if(argc > 3) scale = atoi(argv[3]);
sl_init(threads);
_sl_RET_VAL current;
_sl_RET_VAL result;
const unsigned char black[] = {0};
CImg<unsigned char> visu(worldSize * scale, worldSize * scale, 1, 1, 0);
CImgDisplay draw_disp(visu);
cout << "Brownian Tree in SequenceL" << endl << "Threads: " << threads << endl;
draw_disp.set_title("Brownian Tree in SequenceL: %d Threads", threads);
visu.fill(255);
sl_initialWorld(worldSize, seed, threads, current);
while(!draw_disp.is_closed())
{
visu.draw_circle((current.Point.val().Y - 1) * scale, (current.Point.val().X - 1) * scale, scale/2, black, 1);
visu.display(draw_disp);
sl_step(current.Rand.val(), current.World, threads, result);
current = result;
draw_disp.wait(1);
}
sl_done();
return 0;
}

View file

@ -0,0 +1,24 @@
10 DIM A$(20,20)
20 LET A$(10,10)="1"
30 FOR Y=42 TO 23 STEP -1
40 FOR X=0 TO 19
50 PLOT X,Y
60 NEXT X
70 NEXT Y
80 UNPLOT 9,33
90 FOR I=1 TO 80
100 LET X=INT (RND*18)+2
110 LET Y=INT (RND*18)+2
120 IF A$(X,Y)="1" THEN GOTO 100
130 UNPLOT X-1,43-Y
140 IF A$(X+1,Y+1)="1" OR A$(X+1,Y)="1" OR A$(X+1,Y-1)="1" OR A$(X,Y+1)="1" OR A$(X,Y-1)="1" OR A$(X-1,Y+1)="1" OR A$(X-1,Y)="1" OR A$(X-1,Y-1)="1" THEN GOTO 230
150 PLOT X-1,43-Y
160 LET X=X+INT (RND*3)-1
170 LET Y=Y+INT (RND*3)-1
180 IF X=1 THEN LET X=19
190 IF X=20 THEN LET X=2
200 IF Y=1 THEN LET Y=19
210 IF Y=20 THEN LET Y=2
220 GOTO 130
230 LET A$(X,Y)="1"
240 NEXT I

View file

@ -0,0 +1,33 @@
w:=h:=400; numParticles:=20000;
bitmap:=PPM(w+2,h+2,0); // add borders as clip regions
bitmap[w/2,h/2]=0xff|ff|ff; // plant seed
bitmap.circle(w/2,h/2,h/2,0x0f|0f|0f); // plant seeds
fcn touching(x,y,bitmap){ // is (x,y) touching another pixel?
// (x,y) isn't on the border/edge of bitmap so no edge conditions
var [const] box=T(T(-1,-1),T(0,-1),T(1,-1),
T(-1, 0), T(1, 0),
T(-1, 1),T(0, 1),T(1, 1));
box.filter1('wrap([(a,b)]){ bitmap[a+x,b+y] }); //-->False: not touching, (a,b) if is
}
while(numParticles){
c:=(0x1|00|00).random(0x1|00|00|00) + (0x1|00).random(0x1|00|00) + (0x1).random(0x1|00);
reg x,y;
do{ x=(1).random(w); y=(1).random(h); }while(bitmap[x,y]); // find empty spot
while(1){ // stagger around until bump into a particle, then attach barnicle
if(touching(x,y,bitmap)){
bitmap[x,y]=c;
bitmap.write(f:=File("foo.ppm","wb")); // tell ImageViewer to update image
numParticles-=1;
break;
}
x+=(-1).random(2); y+=(-1).random(2); // [-1,0,1]
if( not ((0<x<w) and (0<y<h)) ){ // next to border --> color border
bitmap[x,y]=c;
break;
}
}
}
bitmap.write(File("foo.ppm","wb")); // the final image