Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  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. plansza2 = plansza.copy()
  47. for i in range(plansza.shape[0]):
  48. for j in range(plansza.shape[1]):
  49. if(plansza[i][j] == 1):
  50. if(sasiedzi(plansza,i,j)<2)or(sasiedzi(plansza,i,j)>3):
  51. plansza2[i][j] = 0
  52. elif(plansza[i][j] == 0):
  53. if(sasiedzi(plansza,i,j) == 3):
  54. plansza2[i][j] = 1
  55. return plansza2
  56.  
  57. def symulacja(plansza, n):
  58. L = []
  59. L.append(plansza)
  60. for i in range(n+1):
  61. M = krok(L[i])
  62. L.append(M)
  63.  
  64. return L
  65.  
  66. szybowiec = nowa_plansza(50, 50, [(25, 30), (25, 31), (25, 32), (26, 30), (27, 31)])
  67. leci = symulacja(szybowiec, 100)
  68.  
  69. from ipywidgets import interact
  70. import matplotlib.pyplot as plt
  71.  
  72. @interact(n=(0, 100))
  73. def animuj(n=0):
  74. plt.matshow(leci[n])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement