-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathseeds.py
43 lines (36 loc) · 981 Bytes
/
seeds.py
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
import numpy as np
import time
from lib import fft_convolve2d
import matplotlib.pyplot as plt
plt.ion()
def seeds(state, k):
"""
'Seeds' cellular automaton state transition
http://www.conwaylife.com/wiki/Seeds
"""
b = fft_convolve2d(state,k).round()
c = np.zeros(b.shape)
# checks the values, and sets alive vs. dead state
c[np.where((b == 2) & (state == 0))] = 1
# return new state
return c
if __name__ == "__main__":
# set up board
m,n = 100,200
A = np.zeros((m,n))
A[20,20] = 1
A[20,21] = 1
A[75,30] = 1
A[76,30] = 1
# construct convolution kernel
k = np.zeros((m, n))
k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])
# plot each frame
plt.figure()
img_plot = plt.imshow(A, interpolation="nearest", cmap = plt.cm.gray)
plt.show(block=False)
while True:
A = seeds(A, k)
img_plot.set_data(A)
plt.draw()
time.sleep(0.01)