SHARE
TWEET

Untitled

a guest Nov 14th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy
  2.  
  3. def  nowa_plansza(H, W, pola=None):
  4.     L = numpy.arange(H*W).reshape((H,W)) * 0
  5.     for i,j in pola:
  6.         L[i][j] = 1
  7.     return L
  8.  
  9.  
  10. def sasiedzi(plansza, i ,j):
  11.     suma = 0
  12.     if((i-1 >= 0)and(j-1 >= 0)):
  13.         if(plansza[i-1][j-1] == 1):
  14.             suma += 1
  15.    
  16.     if(j-1 >= 0):
  17.         if(plansza[i][j-1] == 1):
  18.             suma += 1
  19.  
  20.     if((i+1 < plansza.shape[0])and(j-1 >= 0)):
  21.         if(plansza[i+1][j-1] == 1):
  22.             suma += 1
  23.  
  24.     if(i-1 >= 0):
  25.         if(plansza[i-1][j] == 1):
  26.             suma += 1
  27.  
  28.     if((i-1 >= 0)and(j+1 < plansza.shape[1])):
  29.         if(plansza[i-1][j+1] == 1):
  30.             suma += 1
  31.  
  32.     if(j+1 < plansza.shape[1]):
  33.         if(plansza[i][j+1] == 1):
  34.             suma += 1
  35.  
  36.     if((i+1 < plansza.shape[0])and(j+1 < plansza.shape[1])):
  37.         if(plansza[i+1][j+1] == 1):
  38.             suma += 1
  39.  
  40.     if(i+1 < plansza.shape[0]):
  41.         if(plansza[i+1][j] == 1):
  42.             suma += 1
  43.     return suma
  44.  
  45. def krok(plansza):
  46.     for i in range(plansza.shape[0]):
  47.         for j in range(plansza.shape[1]):
  48.             if(plansza[i][j] == 1):
  49.                 if(sasiedzi(plansza,i,j)<2)or(sasiedzi(plansza,i,j)>3):
  50.                     plansza[i][j] = 0
  51.             elif(plansza[i][j] == 0):
  52.                 if(sasiedzi(plansza,i,j) == 3):
  53.                     plansza[i][j] = 1
  54.     return plansza
  55.  
  56. def symulacja(plansza, n):
  57.     L = []
  58.     L.append(plansza)
  59.     for i in range(n+1):
  60.         M = krok(L[i])
  61.         L.append(M)
  62.        
  63.     return L
  64.  
  65. szybowiec = nowa_plansza(50, 50, [(25, 30), (25, 31), (25, 32), (26, 30), (27, 31)])
  66. leci = symulacja(szybowiec, 100)
  67.  
  68. @interact(n=(0, 100))
  69. def animuj(n=0):
  70.     plt.matshow(leci[n])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top