-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsierpinski.py
53 lines (40 loc) · 1.24 KB
/
sierpinski.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
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 22 16:51:30 2019
@author: toabi
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
choice = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,1])
C = np.array([[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,1,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0]])
A = np.random.randint(2, size=(50, 50))
#Z = np.random.choice(choice,size = (200,200))
Z = np.zeros((200,200))
Z[100,100] = 1
#Z[99,99] = 1
print(Z)
def animate(i):
def iterate(Z):
# Count neighbours
N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
Z[1:-1,0:-2] + Z[1:-1,2:] +
Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:])
#rules
birth = (N==1) & (Z[1:-1,1:-1]==0)
survive = ((N==1) | (N==2)) & (Z[1:-1,1:-1]==1)
Z[...] = 0
Z[1:-1,1:-1][birth | survive] = 1
return Z
iterate(Z)
p.set_data(Z)
fig = plt.figure()
p = plt.imshow(Z,interpolation='nearest', animated = True)
plt.set_cmap('rainbow')
ani = animation.FuncAnimation(fig, animate, interval=1)
plt.show()