Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- def nowa_plansza(H, W, pola=None):
- L = numpy.arange(H*W).reshape((H,W)) * 0
- for i,j in pola:
- L[i][j] = 1
- return L
- def sasiedzi(plansza, i ,j):
- suma = 0
- if((i-1 >= 0)and(j-1 >= 0)):
- if(plansza[i-1][j-1] == 1):
- suma += 1
- if(j-1 >= 0):
- if(plansza[i][j-1] == 1):
- suma += 1
- if((i+1 < plansza.shape[0])and(j-1 >= 0)):
- if(plansza[i+1][j-1] == 1):
- suma += 1
- if(i-1 >= 0):
- if(plansza[i-1][j] == 1):
- suma += 1
- if((i-1 >= 0)and(j+1 < plansza.shape[1])):
- if(plansza[i-1][j+1] == 1):
- suma += 1
- if(j+1 < plansza.shape[1]):
- if(plansza[i][j+1] == 1):
- suma += 1
- if((i+1 < plansza.shape[0])and(j+1 < plansza.shape[1])):
- if(plansza[i+1][j+1] == 1):
- suma += 1
- if(i+1 < plansza.shape[0]):
- if(plansza[i+1][j] == 1):
- suma += 1
- return suma
- def krok(plansza):
- plansza2 = plansza.copy()
- for i in range(plansza.shape[0]):
- for j in range(plansza.shape[1]):
- if(plansza[i][j] == 1):
- if(sasiedzi(plansza,i,j)<2)or(sasiedzi(plansza,i,j)>3):
- plansza2[i][j] = 0
- elif(plansza[i][j] == 0):
- if(sasiedzi(plansza,i,j) == 3):
- plansza2[i][j] = 1
- return plansza2
- def symulacja(plansza, n):
- L = []
- L.append(plansza)
- for i in range(n+1):
- M = krok(L[i])
- L.append(M)
- return L
- szybowiec = nowa_plansza(50, 50, [(25, 30), (25, 31), (25, 32), (26, 30), (27, 31)])
- leci = symulacja(szybowiec, 100)
- from ipywidgets import interact
- import matplotlib.pyplot as plt
- @interact(n=(0, 100))
- def animuj(n=0):
- plt.matshow(leci[n])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement