In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import collections, colors, transforms
import numpy as np
from matplotlib import animation, rc
from IPython.display import HTML
In [47]:
n = 100
north,south,east,west = getgrid(n*2)
north[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)
south[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)
east[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)
west[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)

fig = plt.figure()
fig=plt.figure(figsize=(10, 10), dpi= 80, facecolor='w', edgecolor='k')

j = 0
image = []
for i in range(500):
    j += 1
    north,south,east,west = propegate(north,south,east,west)
    im = plt.imshow(north*11.02**10+south*11.05**10+east*11.01**10+west*11.03**10, animated=True,alpha=1,cmap=plt.get_cmap('viridis'))
    image.append([im])

anim = animation.ArtistAnimation(fig, image, interval=100, blit=True, repeat = True)
plt.close()
HTML(anim.to_html5_video())
Out[47]:
<matplotlib.figure.Figure at 0x139f9da58>
In [48]:
n = 100
north,south,east,west = getgrid(n*2)
north[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)
south[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)
east[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)
west[int(n-10):int(n+10),int(n-10):int(n+10)] = np.ones(20)

j = 0
while j < 10*n:
    north[np.random.randint(1, n*2),np.random.randint(1, n*2)] = 1
    east[np.random.randint(1, n*2),np.random.randint(1, n*2)] = 1
    south[np.random.randint(1, n*2),np.random.randint(1, n*2)] = 1
    west[np.random.randint(1, n*2),np.random.randint(1, n*2)] = 1    
    j+=1

fig = plt.figure()
fig=plt.figure(figsize=(10, 10), dpi= 80, facecolor='w', edgecolor='k')

j = 0
image = []
for i in range(500):
    j += 1
    north,south,east,west = propegate(north,south,east,west)
    im = plt.imshow(north*11.02**10+south*11.05**10+east*11.01**10+west*11.03**10, animated=True,alpha=1,cmap=plt.get_cmap('viridis'))
    image.append([im])

anim = animation.ArtistAnimation(fig, image, interval=100, blit=True, repeat = True)
plt.close()
HTML(anim.to_html5_video())
Out[48]:
<matplotlib.figure.Figure at 0x14ec04b00>
In [39]:
n = 100
flag = True
w,sw,se,e,ne,nw = getHexgrid(n)
odd = w*0
for i in range(0,n):
    if(i%2):
        odd[i,:] = np.ones((1,n))

n = np.int_(n/2)
g = 15
nw[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
sw[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
ne[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
se[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
e[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
w[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
n = n*2
    
offsets = hexgrid(n)/100
fig, axes = plt.subplots(figsize=(15, 15), dpi= 80, facecolor='w', edgecolor='k')
plt.ylim(-1, n*2+1)
plt.xlim(-1, n*2+1)
plt.axis('off')
#fc = np.linspace(10,0,n*n)
j = 0
image = []
for i in range(100):
    j += 1   
    w,sw,se,e,ne,nw,fc,flag = hexPropagate(w,sw,se,e,ne,nw,odd,flag)
    #north,south,east,west = propegate(north,south,east,west)
    #fc = np.roll(fc,1,0)
    facecolors = [plt.cm.inferno(x) for x in fc*100]
    col = collections.RegularPolyCollection(
    numsides=6, # a pentagon
    rotation=0, sizes=(40,),
    facecolors = facecolors,
    edgecolors = ((0,0,0,1)),
    linewidths = (1.5,),
    offsets = offsets,
    transOffset = axes.transData,
    )
    
    axes.autoscale_view()
    im = axes.add_collection(col)
    image.append([im])

anim = animation.ArtistAnimation(fig, image, interval=100, blit=True, repeat = True)
plt.close()
HTML(anim.to_html5_video())
Out[39]:
In [41]:
n = 100
flag = True
w,sw,se,e,ne,nw = getHexgrid(n)
odd = w*0
for i in range(0,n):
    if(i%2):
        odd[i,:] = np.ones((1,n))
        
n = np.int_(n/2)
g = 15
nw[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
sw[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
ne[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
se[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
e[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
w[int(n-g):int(n+g),int(n-g):int(n+g)] = np.ones((2*g,2*g))
n = n*2

j = 0
while j < 10*n:
    w[np.random.randint(1, n-1),np.random.randint(1, n-1)] = 1
    e[np.random.randint(1, n-1),np.random.randint(1, n-1)] = 1
    ne[np.random.randint(1, n-1),np.random.randint(1, n-1)] = 1
    se[np.random.randint(1, n-1),np.random.randint(1, n-1)] = 1   
    nw[np.random.randint(1, n-1),np.random.randint(1, n-1)] = 1
    sw[np.random.randint(1, n-1),np.random.randint(1, n-1)] = 1  
    j+=1
    
offsets = hexgrid(n)/100
fig, axes = plt.subplots(figsize=(15, 15), dpi= 80, facecolor='w', edgecolor='k')
plt.ylim(-1, n*2+1)
plt.xlim(-1, n*2+1)
plt.axis('off')
#fc = np.linspace(10,0,n*n)
j = 0
image = []
for i in range(100):
    j += 1   
    w,sw,se,e,ne,nw,fc,flag = hexPropagate(w,sw,se,e,ne,nw,odd,flag)
    #north,south,east,west = propegate(north,south,east,west)
    #fc = np.roll(fc,1,0)
    facecolors = [plt.cm.inferno(x) for x in fc*100]
    col = collections.RegularPolyCollection(
    numsides=6, # a pentagon
    rotation=0, sizes=(40,),
    facecolors = facecolors,
    edgecolors = ((0,0,0,1)),
    linewidths = (1.5,),
    offsets = offsets,
    transOffset = axes.transData,
    )
    
    axes.autoscale_view()
    im = axes.add_collection(col)
    image.append([im])

anim = animation.ArtistAnimation(fig, image, interval=100, blit=True, repeat = True)
plt.close()
HTML(anim.to_html5_video())
Out[41]:
In [43]:
def getgrid(n):
    north = np.tile(0,[n,n])
    south = np.tile(0,[n,n])
    east = np.tile(0,[n,n])
    west = np.tile(0,[n,n])
    return(north, south, east, west)
In [44]:
def propegate(north,south,east,west):
    temp1 = (north + south==2)
    temp2 = (east + west ==2)
    
    north = north - temp1
    south = south - temp1
    east = east + temp1
    west = west + temp1
    
    east = east - temp2
    west = west - temp2
    north = north + temp2
    south = south + temp2
    
    north = np.roll(north,-1,axis =0)
    south = np.roll(south,1,axis =0)
    east = np.roll(east,-1,axis =1)
    west = np.roll(west,1,axis =1)
    return(north, south, east, west)
                    
In [38]:
def hexPropagate(w,sw,se,e,ne,nw,odd,flag):
    # get three way collisions
    temp1 = (se+e+ne==3)
    temp2 = (sw+w+nw==3)
    # get two way collisions
    temp3 = (sw+ne==2)
    temp4 = (w+e==2)
    temp5 = (nw+se==2)   
    # resolve three way collisions
    ne-=temp1
    e-=temp1
    se-=temp1
    nw+=temp1
    w+=temp1
    sw+=temp1
    
    sw-=temp2
    w-=temp2
    nw-=temp2
    ne+= temp2
    e+=temp2
    se+=temp2
    # resolve two way collisions
    sw-=temp3
    ne-=temp3
    

            
    if flag:#np.random.rand(1)>0.5:
        nw+=temp3
        se+=temp3       
    else:
        e+=temp3
        w+=temp3
        
    w-=temp4
    e-=temp4
    
    if flag:
        sw+=temp4
        ne+=temp4 
    else:
        nw+=temp4
        se+=temp4 
        
    nw-=temp5
    se-=temp5
    
    if flag:
        e+=temp5
        w+=temp5
    else:
        ne+=temp3
        sw+=temp3
        

    
    # the rest
    w = np.roll(w,-1,axis = 1)
    e = np.roll(e,1,axis = 1)
    
    nwo = nw*odd
    neo = ne*odd
    swo = sw*odd
    seo = se*odd
    
    #roll even rows in the right direction
    nw = np.roll(nw-nwo,-1,axis =0)
    nw = np.roll(nw,-1,axis =1)
    
    ne = np.roll(ne-neo,-1,axis =0)
    
    sw = np.roll(sw-swo,1,axis =0)
    sw = np.roll(sw,-1,axis =1)
    
    se = np.roll(se-seo,1,axis =0)
    
    # roll odd rows
    nwo = np.roll(nwo,-1,axis =0)
    
    neo = np.roll(neo,-1,axis =0)
    neo = np.roll(neo,1,axis =1)
    
    swo = np.roll(swo,1,axis =0)
    
    seo = np.roll(seo,1,axis =0)
    seo = np.roll(seo,1,axis =1)
    
    #recombine
    nw+=nwo
    ne+=neo
    se+=seo
    sw+=swo
    
    fc = w.ravel() + e.ravel() + nw.ravel() + ne.ravel() + sw.ravel() + se.ravel() 
    flag = not flag
    return(w,sw,se,e,ne,nw,fc,flag)
In [4]:
def hexPropagateDraft(w,sw,se,e,ne,nw,flag):
    # get three way collisions
    temp1 = (se+e+ne==3)
    temp2 = (sw+w+nw==3)
    # get two way collisions
    temp3 = (sw+ne==2)
    temp4 = (w+e==2)
    temp5 = (nw+se==2)   
    # resolve three way collisions
    ne-=temp1
    e-=temp1
    se-=temp1
    nw+=temp1
    w+=temp1
    sw+=temp1
    
    sw-=temp2
    w-=temp2
    nw-=temp2
    ne+= temp2
    e+=temp2
    se+=temp2
    # resolve two way collisions
    sw-=temp3
    ne-=temp3
    nw+=temp3
    se+=temp3
    w-=temp4
    e-=temp4
    sw+=temp4
    ne+=temp4    
    nw-=temp5
    se-=temp5
    e+=temp5
    w+=temp5
    
    # the rest
    w = np.roll(w,-1,axis = 1)
    e = np.roll(e,1,axis = 1)  
    nw = np.roll(nw,-1,axis =0)
    ne = np.roll(ne,-1,axis =0)
    sw = np.roll(sw,1,axis =0)
    se = np.roll(se,1,axis =0)
    if flag:
        nw = np.roll(nw,-1,axis =1)
        ne = np.roll(ne,1,axis =1)
        sw = np.roll(sw,-1,axis =1)
        se = np.roll(se,1,axis =1)
    wflag = not flag    
    fc = w.ravel() + e.ravel() + nw.ravel() + ne.ravel() + sw.ravel() + se.ravel() 
    return(w,sw,se,e,ne,nw,fc,flag)
In [42]:
def getHexgrid(n):
    w = np.tile(0,[n,n])
    sw = np.tile(0,[n,n])
    s = np.tile(0,[n,n])
    se = np.tile(0,[n,n])
    e = np.tile(0,[n,n])
    ne = np.tile(0,[n,n])
    nw = np.tile(0,[n,n])
    return(w,sw,se,e,ne,nw)
In [2]:
def hexgrid(n):
    
    offsets = np.zeros((n**2+1,2))

    for j in range(0,n):
        offsets[n+j,0] = 100+(2*86)*j #x positions for odd rows
        offsets[n+j,1] = 150
        offsets[0+j,0] = (100-86)+(2*86)*j #x position for even rows
        offsets[0+j,1] = 0
        
    for i in range(2,n):
        for j in range(0,n+1):
            if i%2: #odd
                offsets[(i*n)+j,0] = 100+(2*86)*j #x positions for odd rows
                offsets[(i*n)+j,1] = i*150
            else:   
                offsets[(i*n)+j,0] = (100-86)+(2*86)*j #x position for even rows
                offsets[(i*n)+j,1] = i*150
    offsets = np.delete(offsets,-1,0)
    return(offsets)