/
poisson.pyde
56 lines (46 loc) · 1.32 KB
/
poisson.pyde
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
from __future__ import division
import random
import collections
R = 20
active = []
inactive = []
def setup():
size(400, 400, P2D)
smooth(16)
pixelDensity(displayDensity())
global active
r = random.random() * 200 * 200
theta = random.random() * 2 * PI
x = 200 + sqrt(r) * sin(theta)
y = 200 + sqrt(r) * cos(theta)
active.append((x, y))
def draw():
global active, inactive, R
background(255)
noFill()
stroke(0, 0, 0)
ellipse(200, 200, 400, 400)
for (x, y) in active:
fill(255, 0, 0)
noStroke()
ellipse(x, y, 5, 5)
for (x, y) in inactive:
fill(0, 0, 0)
noStroke()
ellipse(x, y, 5, 5)
if len(active):
x, y = active[-1]
for _ in range(20):
r = random.random() * 4 * R * R + R * R
theta = random.random() * 2 * PI
px = x + sqrt(r) * sin(theta)
py = y + sqrt(r) * cos(theta)
if (px - 200)**2 + (py - 200)**2 < 200*200:
for (ox, oy) in active + inactive:
if (ox - px)**2 + (oy - py)**2 < R*R:
break
else:
active.insert(-2, (px, py))
break
else:
inactive.append(active.pop())