# Type y#!/usr/bin/env python3
# -*- coding: utf-8 -*-
importnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlib.animationasanimation# Path of ffmpeg executable for animation
plt.rcParams['animation.ffmpeg_path']=r'/Volumes/Data/Youtube/[ffmpeg]/ffmpeg'###############################################################################
defevolve(X):''' Evolves a board of Game of Life for one turn '''# Dead cells as a boundary condition
# Count neighbours
# Alive if 3 neighbours or 2 neighbours and already alive
Xi=X.astype(int)neigh=np.zeros(Xi.shape)neigh[1:-1,1:-1]=(Xi[:-2,:-2]+Xi[:-2,1:-1]+Xi[:-2,2:]+Xi[1:-1,:-2]+Xi[1:-1,2:]+Xi[2:,:-2]+Xi[2:,1:-1]+Xi[2:,2:])returnnp.logical_or(neigh==3,np.logical_and(Xi==1,neigh==2))###############################################################################
defget_history(B,T):''' Returns the evolution of a board B after T generations '''history=np.zeros((T,B.shape[0],B.shape[1]),dtype=bool)fortinrange(T):history[t,:,:]=BB=evolve(B)print(t)returnhistory###############################################################################
defplotcells(X,filename=False):''' Plots a board of Game of Life + optionally saving the figure '''LW=0.5if(X.shape[0]>200):USE_IMSHOW=Trueelse:USE_IMSHOW=Falsefig=plt.figure(figsize=(16,9),dpi=120)ifUSE_IMSHOW==False:# Light blue lines as cells boundaries
plt.pcolor(X.T,cmap="gray_r",edgecolors='cadetblue',linewidths=LW)else:plt.imshow(X[:,::-1].T,cmap="gray_r")plt.gca().get_xaxis().set_visible(False)plt.gca().get_yaxis().set_visible(False)fig.tight_layout()if (filename!=False):plt.savefig(filename,dpi=90)else:plt.show()###############################################################################
defmakeMovie(history,filename,trim=False):''' Create the movie from a history of a game of life'''# History is the boolean history (non inverted i.e. True = alive)
# Inversion is done in the colormap
# Filename should be *.mp4
FIGSIZE=(16,9)DPI=240LW=0.5if(history.shape[1]>200):USE_IMSHOW=Trueelse:USE_IMSHOW=False# Trim boundaries
iftrim:history=history[:,3:-3,3:-3]# Create the plot and its starting point
print("Create initial plot")my_cmap=plt.get_cmap('gray_r')fig=plt.figure(figsize=FIGSIZE,dpi=DPI)ax=fig.add_subplot(111)ifUSE_IMSHOW==False:# First option : use pcolor
pc=ax.pcolor(history[0,:,:].T,cmap=my_cmap,edgecolors='cadetblue',linewidths=LW)else:# Second option : use imshow
im=ax.imshow(history[0,:,::-1].T,cmap=my_cmap)cnt=ax.text(0.01,0.99,str(0),color='red',fontsize=30,verticalalignment='top',horizontalalignment='left',transform=ax.transAxes)ax.get_xaxis().set_visible(False)ax.get_yaxis().set_visible(False)fig.tight_layout()# The function as it is called at the n-th iteration
# It directly modifies the data within the image
defupdate_img(n):# Revert and scale from 0-1 to 0-255
print('Frame '+str(n))ifUSE_IMSHOW==False:new_color=my_cmap(255*history[n,:,:].T.ravel())pc.update({'facecolors':new_color})else:im.set_data(history[n,:,::-1].T)#
cnt.set_text(str(n))# # if needed, can modify the field of view
# fov =
# ax.set_xlim()
# ax.set_ylim()
returnTrue# Create the animation and save it
print("Make animation")ani=animation.FuncAnimation(fig,update_img,history.shape[0],interval=30)# 30ms per frame
writer=animation.FFMpegWriter(fps=30,bitrate=5000)print("Save movie")ani.save(filename,writer=writer,dpi=DPI)print("Saved")###############################################################################
defreadRLE_OLD(filename,Bshape=(50,50),position=(10,10),rH=False,rV=False):''' Read the RLE file and returns a binary matrix '''# see http://www.conwaylife.com/wiki/RLE
# Open file and cast it into a unique string
f=open(filename,"r")s=''whileTrue:l=f.readline()ifl=='':# Empty indicates end of file. An empty line would be '\n'
breakifl[0]=='#':continueifl[0]=='x':continues=s+l[:-1]# To remove EOL
f.close()# Create matrix
B=np.zeros(Bshape).astype(bool)initX,initY=position# We parse each character and decide accordingly what to do
# If the character is a digit, we keep going until we reach 'b' or 'o'
curX,curY=initX,initYqs=''forcins:# End of file
ifc=='':break# Next Line
ifc=='$':q=1ifqs==''elseint(qs)curY+=qcurX=initXqs=''# Digit (check ascii code for a digit from 0 to 9)
iford(c)>47andord(c)<58:#
qs=qs+c# Alive (o) or Dead (b) cell
ifc=='b'orc=='o':q=1ifqs==''elseint(qs)foriinrange(q):B[curX,curY]=Falseifc=='b'elseTruecurX+=1qs=''ifrV:B=B[:,::-1]ifrH:B=B[::-1,:]returnB.astype(bool)###############################################################################
defreadRLE(filename,Cshape=(50,50),position=(10,10),rH=False,rV=False,tp=False):''' Read the RLE file and returns a binary matrix '''# see http://www.conwaylife.com/wiki/RLE
# Open file and cast it into a unique string
f=open(filename,"r")s=''whileTrue:l=f.readline()ifl=='':# Empty indicates end of file. An empty line would be '\n'
breakifl[0]=='#':continueifl[0]=='x':continues=s+l[:-1]# To remove EOL
f.close()# Create matrix
SHAPE_MAX=(2500,2500)B=np.zeros(SHAPE_MAX).astype(bool)# We parse each character and decide accordingly what to do
# If the character is a digit, we keep going until we reach 'b' or 'o'
curX,curY=0,0qs=''forcins:# End of file
ifc=='':break# Next Line
ifc=='$':q=1ifqs==''elseint(qs)curY+=qcurX=0qs=''# Digit (check ascii code for a digit from 0 to 9)
iford(c)>47andord(c)<58:#
qs=qs+c# Alive (o) or Dead (b) cell
ifc=='b'orc=='o':q=1ifqs==''elseint(qs)foriinrange(q):B[curX,curY]=Falseifc=='b'elseTruecurX+=1qs=''posX,posY=positionBshapeY=max(np.where(sum(B)>0)[0])+1BshapeX=max(np.where(sum(B.T)>0)[0])+1B=B[0:BshapeX,0:BshapeY]ifrV:B=B[:,::-1]ifrH:B=B[::-1,:]iftp:B=B.TC=np.zeros(Cshape)C[posX:(posX+B.shape[0]),posY:(posY+B.shape[1])]=np.copy(B)returnC.astype(bool)ourtexthere
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.