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
int n = 1000;
int t;
Particle[] particles = new Particle[n];

int hsb = 16;

void setup() {
  colorMode(HSB, hsb);
  size(200,200);

  smooth();
  for (int i=0; i < n; i++) {
    Particle p = new Particle(width/2, height/2, 1);
    particles[i] = p;
  }
}

void draw() {
  t += 1;
  float xm = 0;
  float ym = 0;
  for (int i=0; i < n; i++) {
    Particle p = particles[i];
    p.move();
    xm += p.x;
    ym += p.y;
    p.render();
  }

  println(t + " " + xm/n + " " + ym/n); 
  
  rectMode(CORNER);
  fill(hsb, hsb * 0.25);
  rect(0, 0, width, height);
}

class Particle {
  int x;
  int y;
  int d;
  
  Particle(int x_, int y_, int d_) {
    x = x_;
    y = y_;
    d = d_;
  }
  
  void move() {
    float r = random(1);
    if (r < 0.25) {
       x += d;
    } else if (r < 0.5) {
      x -= d;
    } else if (r < 0.75) {
      y += d;
    } else {
      y -= d;
    }
  }
  
  void render() {
    point(x, y);
  }
}