/
brownian_motion.pde
84 lines (72 loc) · 1.45 KB
/
brownian_motion.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Simulation of Brownian motion
// J. Carlson, 12/23/2010
// http://square-the-circle.com/2013/02/08/brownian-motion-2/
PFont font;
int maxFrameCount = 100000;
float SIZE = 600;
// Origin
float xo = SIZE/2;
float yo = SIZE/2;
int threads = 1;
float r = 255;
float g = 0;
float b = 0;
float a = 255;
// Current position of radom walker
float x, y;
float stepSize = SIZE/100.0;
void step() {
float x2 = x + random(-stepSize, stepSize);
float y2 = y + random(-stepSize, stepSize);
// line(x,y,x2,y2);
ellipse(x,y,2,2);
x = x2;
y = y2;
}
///////////////////////////////////////////////
void setup() {
size(600,750);
background(0);
frameRate(120);
fill(255,0,0);
smooth();
x = xo;
y = yo;
// Code for text:
font = loadFont("AndaleMono-36.vlw");
textFont(font);
textSize(24);
}
float norm(float x, float y) {
return max(abs(x-xo), abs(y-yo));
}
void draw() {
if (frameCount % maxFrameCount == 0) {
x = xo; y = yo;
fill(0);
rect(0,0,600,600);
threads = 1;
}
fill(r,g,b,a);
step();
if( norm(x,y) > SIZE/2 ) {
threads += 1;
x = xo;
y = yo;
r = random(0,255);
g = random(0,255);
b = random(0,255);
a = random(254,255);
}
if (frameCount % 1000 == 0) {
saveFrame();
}
fill(0);
noStroke();
rect(0,660,width, 90);
fill(128);
text("Markov 2", 250, 700);
fill(90);
text("T:"+threads+" N:" + frameCount, 15, 700);
text("R:" + round(norm(x,y)), 500, 700);
}