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
|
h=20#hauteur
n=30#largeur
delta=500#millisecondes entre deux images
motif=[\
[0,0,0,0],\
[0,0,1,1],\
[0,1,0,1],\
[0,1,1,1],\
[1,1,0,1],\
]
#---------------------------------------
#on manipule une liste de lignes
#L[i,j] sera compris si on l'ecrit L[j][i] dans le code Python
def creer_liste_vide():
Ll=[]#point de depart
for i in range(h):
Ll.append([0]*n)#attention [[0]*n]*h pose des pb
# car alors les lignes sont des repliques et se modifient
# simultanement des qu'une d'elles est modifiee.
return(Ll)
L=creer_liste_vide()
def autour(i,j):#renvoie ce que le point (i,j) devient : 1 ou 0
#0<i<n-1
#0<j<h-1
somme=L[j-1][i-1]+\
L[j-1][i]+\
L[j-1][i+1]+\
L[j][i-1]+\
L[j][i]+\
L[j][i+1]+\
L[j+1][i-1]+\
L[j+1][i]+\
L[j+1][i+1]
ici=L[j][i]
if(ici):#cas ici==1
return((somme==2 or somme==3)*1)#le *1
#pour avoir un int et non un bool
else:#cas ici==0
return((somme==3)*1)
def nouvelle():
Lnew=creer_liste_vide()
for j in range(1,h-1):
for i in range(1,n-1):
Lnew[j][i]=autour(i,j)
return(Lnew)
def afficher(liste):
for ligne in liste:
print(ligne)
#remplissage de L avec le motif choisi -------------
h_motif=len(motif)
n_motif=len(motif[0])#en esperant que len(motif[i])=Cste
h_depart=h//2-h_motif//2
n_depart=n//2-n_motif//2
h_i=0
n_i=0
while(h_i<h_motif):#remplit L avec le motif quelque part ~ au centre
while(n_i<n_motif):
L[h_i+h_depart][n_i+n_depart]=motif[h_i][n_i]
n_i+=1
h_i+=1
n_i=0
def transfuser(L1,L2):
#Li du type Li[j][i]
for j in range(h):
for i in range(n):
L2[j][i]=L1[j][i]
def animate(i):
#im = ax.imshow(L)
transfuser(nouvelle(),L)
im.set_array(L)
return [im]
#import numpy as np
import matplotlib
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
im = plt.imshow(L, interpolation='none', aspect='auto', vmin=0, vmax=1)#vmin, vmax = les couleurs
anim = FuncAnimation(fig, animate,\
# init_func = init,
frames = 10,
interval = delta, \
#blit = True)
save_count=10
)
ax.set_title(str(h)+" x "+str(n))
fig.tight_layout()
plt.show() |