# The Brownian motion of a particle (the positions that the particle occupies tend to
# be equidistributed). Every 100 displacements the percentage of positions reached in
# the 4 quadrants is displayed.
#
# Copy and paste in R:
#
# standard:  dt=1/6.  To speed up choose dt = 1/100
dt=1/6
BF=4;HF=4.2; BOXW(0,10,0,11.5)
text(5,10.6,"press ESC to stop")
x=2; y=2; x0=x; y0=y; e=0.15; Q=c(0,0,0,0); i=1
for(j in 1:1e5) {
polyline(c(-e,10+e, 10+e,-e,-e),c(-e,-e, 10+e,10+e,-e),"red")
line(5,-0.1,5,10.1,"red"); line(-0.1,5,10.1,5,"red")
Point(x,y, "brown"); if(x>5 & y>5) N=2 else {if(x>5) N=4 else {if(y>5) N=1 else N=3}}; Q[N]=Q[N]+1
if(i/1e2 == floor(i/1e2)) {CLEAN(0,10,10.2,11.5); text(5,10.6,"press ESC to stop");
  text(1.5,11.2,round(Q[1]/i*100,1));text(3.5,11.2,round(Q[2]/i*100,1));text(5.5,11.2,round(Q[3]/i*100,1));text(7.5,11.2,round(Q[4]/i*100,1));text(9,11.2,"%")}
wait(dt)
a=runif(1,0,2*pi); d=runif(1,-1,1); x=x+d*cos(a); y=y+d*sin(a)
if(x>10) x=10; if(x<0) x=0; if(y>10) y=10; if(y<0) y=0
Point(x0,y0,"grey"); line(x0,y0,x,y,"grey"); x0=x; y0=y; i=i+1
}
# The