-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconway3.2dpy
134 lines (106 loc) · 3.68 KB
/
conway3.2dpy
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def Init():
# --- author : rickb
# --- Set parameter names
STR.SetFParam0Name("Decay")
STR.SetFParam1Name("Zoom")
STR.SetFParam2Name("Line Dir")
GLB.xmode = 0
# --- accent: Flat Lines
# --- trigger: Rand Move
GLB.N = 20
GLB.M = 15
GLB.FRAME = 0
GLB.grid = np.array([])
GLB.lastGrid = np.zeros(shape=(GLB.N,GLB.M))
GLB.color = np.zeros(shape=(GLB.N,GLB.M))
# Put gosper/glider stuff here
def addGlider(i, j, grid):
glider = np.array([[0, 0, 255],
[255, 0, 255],
[0, 255, 255]])
grid[i:i+3, j:j+3] = glider
GLB.addGlider = addGlider
cr.set_source_rgba(0,0,0,1)
cr.rectangle(0,0,320,240)
cr.fill()
def randomGrid(N,M):
GLB.color = np.random.choice([0,1,2,3], N*M, p=[0.25, 0.25,0.25,0.25]).reshape(N,M)
return np.random.choice([1,0], N*M, p=[0.2, 0.8]).reshape(N,M)
GLB.randomGrid = randomGrid
def update(grid,N,M):
GLB.FRAME = GLB.FRAME + 1
# copy grid since we require 8 neighbors
# for calculation and we go line by line
newGrid = grid.copy()
for i in range(N):
for j in range(M):
# compute 8-neghbor sum
# using toroidal boundary conditions - x and y wrap around
# so that the simulaton takes place on a toroidal surface.
total = int(grid[i, (j-1)%M] + grid[i, (j+1)%M] +
grid[(i-1)%N, j] + grid[(i+1)%N, j] +
grid[(i-1)%N, (j-1)%M] + grid[(i-1)%N, (j+1)%M] +
grid[(i+1)%N, (j-1)%M] + grid[(i+1)%N, (j+1)%M])
# apply Conway's rules
if grid[i, j] == 1:
if (total < 2) or (total > 3):
newGrid[i, j] = 0
else:
if total == 2:
GLB.color[i, j]=0
else:
GLB.color[i, j]=1
else:
if total == 3:
newGrid[i, j] = 1
GLB.color[i, j]=2
x = i*16 - 6
y = j*16 - 6
if newGrid[i,j] == 1:
c = GLB.color[i,j]
if STR.CheckAccent():
c = 3
ox = 0
oy = 0
if GLB.xmode:
ox = random.randint(0,20)-10
oy = random.randint(0,20)-10
if c==1:
cr.move_to(x+ox,y+oy)
cr.line_to(x+ox+18*GLB.r,y+oy+18*GLB.r)
elif c==2:
cr.move_to(x+ox,y+oy+18)
cr.line_to(x+ox+18*GLB.r,y+oy)
else:
cr.move_to(x+ox,y+oy)
cr.line_to(x+ox+18*GLB.r,y+oy)
cr.stroke()
compa = grid == newGrid
compb = GLB.lastGrid == newGrid
if compa.all() or compb.all() or GLB.FRAME==600:
GLB.FRAME=0
GLB.grid = GLB.randomGrid(GLB.N,GLB.M)
else:
if GLB.FRAME==300:
GLB.addGlider(0,0,GLB.grid)
GLB.lastGrid = grid.copy()
grid[:] = newGrid[:]
GLB.update = update
GLB.grid = GLB.randomGrid(GLB.N,GLB.M)
def Render(cr):
trig = STR.CheckTrigger()
GLB.f0 = STR.F0()
GLB.f1 = STR.F1mix(0,1)
GLB.f2 = STR.F2()
cr.set_source_rgba(0,0,0,GLB.f0)
cr.rectangle(0,0,320,240)
cr.fill()
if trig:
GLB.xmode = 1 if GLB.xmode==0 else 0
cr.set_source_rgba(1,1,1,0.5)
cr.translate(160,120)
s = 2.5*GLB.f1*GLB.f1+0.25*GLB.f1+0.250
cr.scale(s,s)
cr.translate(-160,-120)
GLB.r = 2.5*GLB.f2*GLB.f2+0.25*GLB.f2+0.250
GLB.update(GLB.grid,GLB.N,GLB.M)