Guest User

Untitled

a guest
Sep 20th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5.  
  6.  
  7.  
  8. function init(size, polje)
  9. {
  10. var i, j;
  11. for (j = 0; j< size; j++)
  12. {
  13. var redak = new Array();
  14. for (i = 0; i < size; i++)
  15. {
  16. redak[i] = 0;
  17. }
  18. polje[j] = redak;
  19. }
  20. }
  21.  
  22. function initiate(polje, koordinate)
  23. {
  24. var i;
  25. for (i = 0; i < koordinate.length; i++)
  26. {
  27. var x = koordinate[i][0];
  28. var y = koordinate[i][1];
  29. polje[x][y] += 1;
  30. }
  31. }
  32.  
  33. function broj_zivih_susjeda(polje, x, y)
  34. {
  35. var count = 0;
  36. s = polje.length;
  37. var x1 = x + 1;
  38.  
  39. if (x1 >= s){
  40. x1 = x1 - s;
  41. }
  42.  
  43. var x2 = x - 1;
  44. if (x2 < 0){
  45. x2 = x2 + s;
  46. }
  47. var y1 = y + 1;
  48.  
  49. if (y1 >= s){
  50. y1 = y1 - s;
  51. }
  52. var y2 = y - 1;
  53. if (y2 < 0){
  54. y2 = y2 + s;
  55. }
  56. count += polje[x1][y];
  57. count += polje[x2][y];
  58. count += polje[x][y1];
  59. count += polje[x][y2];
  60. count += polje[x1][y2];
  61. count += polje[x1][y1];
  62. count += polje[x2][y1];
  63. count += polje[x2][y2];
  64.  
  65. return count;
  66. }
  67.  
  68. function step(polje)
  69. {
  70. var polje_tmp = new Array();
  71. var i, j;
  72. for (i = 0; i < polje.length; i++)
  73. {
  74. var red_tmp = new Array();
  75. for (j = 0; j < polje.length; j++)
  76. {
  77. //document.write('xxx' + i + ' ' + j);
  78. var za_append = polje[i][j];
  79. if (polje[i][j] == 1 && broj_zivih_susjeda(polje, i, j) < 2)
  80. {
  81. za_append = 0;
  82. }
  83. if (polje[i][j] == 1 && (broj_zivih_susjeda(polje, i, j) == 2 || broj_zivih_susjeda(polje, i, j) == 3))
  84. {
  85. za_append = polje[i][j];
  86.  
  87. }
  88. if (polje[i][j] == 1 && (broj_zivih_susjeda(polje, i, j) > 3))
  89. {
  90. za_append = 0;
  91. }
  92. if (polje[i][j] == 0 && (broj_zivih_susjeda(polje, i, j) == 3))
  93. {
  94. za_append = 1;
  95. }
  96. red_tmp[j] = za_append;
  97. }
  98. polje_tmp[i] = red_tmp;
  99. //document.write('xxx' + red_tmp);
  100. }
  101.  
  102. for (i = 0; i < polje.length; i++)
  103. {
  104. for (j = 0; j< polje.length; j++)
  105. {
  106. polje[i][j] = polje_tmp[i][j];
  107. }
  108. }
  109.  
  110. //polje = polje_tmp;
  111. }
  112.  
  113. function sleep(milliseconds) {
  114. var start = new Date().getTime();
  115. for (var i = 0; i < 1e7; i++) {
  116. if ((new Date().getTime() - start) > milliseconds){
  117. break;
  118. }
  119. }
  120. }
  121.  
  122. window.requestAnimFrame = (function(callback){
  123. return window.requestAnimationFrame ||
  124. window.webkitRequestAnimationFrame ||
  125. window.mozRequestAnimationFrame ||
  126. window.oRequestAnimationFrame ||
  127. window.msRequestAnimationFrame ||
  128. function(callback){
  129. window.setTimeout(callback, 1000 / 60);
  130. };
  131. })();
  132.  
  133.  
  134.  
  135. function animate(polje){
  136. var canvas = document.getElementById("myCanvas");
  137. var context = canvas.getContext("2d");
  138. context.clearRect(0, 0, canvas.width, canvas.height);
  139. var i, j;
  140. var velicina = polje.length;
  141.  
  142. var korak = 500 / velicina;
  143.  
  144. //var polje = new Array();
  145.  
  146. //init(velicina, polje);
  147.  
  148. //blinker = [[2, 1], [2, 2], [2, 3]]
  149.  
  150. //initiate(polje, blinker);
  151. //
  152. //var k;
  153. //for (k = 0; k < 100; k++) {
  154. step(polje);
  155. for (i = 0; i < velicina; i++)
  156. {
  157. for (j = 0; j < velicina; j++)
  158. {
  159.  
  160.  
  161. if (polje[i][j] == 1) {
  162. var topLeftCornerX = i*korak;
  163. var topLeftCornerY = j*korak;
  164. var width = korak-0;
  165. var height = korak-0;
  166.  
  167. context.beginPath();
  168. context.rect(topLeftCornerX, topLeftCornerY, width, height);
  169. context.fillStyle = "red";
  170. context.fill();
  171. context.lineWidth = 1;
  172. context.strokeStyle = "lightgray";
  173. context.stroke();
  174. }
  175.  
  176.  
  177.  
  178. /* context.moveTo(0, i*korak);
  179. context.lineTo(500, i*korak);
  180. context.strokeStyle = "gray";
  181. context.stroke();
  182.  
  183. context.moveTo(i*korak, 0);
  184. context.lineTo(i*korak, 500);
  185. context.stroke();*/
  186.  
  187. }
  188. }
  189.  
  190.  
  191. requestAnimFrame(function(){
  192. animate(polje);
  193. });
  194.  
  195.  
  196.  
  197. };
  198.  
  199. window.onload = function(){
  200. var canvas = document.getElementById("myCanvas");
  201. var context = canvas.getContext("2d");
  202.  
  203. var i, j;
  204. var velicina = 100;
  205.  
  206. var korak = 500 / velicina;
  207.  
  208. var polje = new Array();
  209.  
  210. init(velicina, polje);
  211.  
  212. blinker = [[2, 1], [2, 2], [2, 3]]
  213.  
  214. pulsar = [[1, 5], [1, 11],
  215. [2, 5], [2, 11],
  216. [3, 5], [3, 6], [3, 10], [3, 11],
  217. [5, 1], [5, 2], [5, 3], [5, 6], [5, 7], [5, 9], [5, 10], [5, 13], [5, 14], [5, 15],
  218. [11, 1], [11, 2], [11, 3], [11, 6], [11, 7], [11, 9], [11, 10], [11, 13], [11, 14], [11, 15],
  219. [6, 3], [6, 5], [6, 7], [6, 9], [6, 11], [6, 13],
  220. [10, 3], [10, 5], [10, 7], [10, 9], [10, 11], [10, 13],
  221. [7, 5], [7, 6], [7, 10], [7, 11],
  222. [9, 5], [9, 6], [9, 10], [9, 11],
  223. [13, 5], [13, 6], [13, 10], [13, 11],
  224. [14, 5], [14, 11],
  225. [15, 5], [15, 11],
  226. ]
  227.  
  228. gosperGliderGun = [[5, 1], [5, 2], [6, 1], [6, 2],
  229. [3, 13], [3, 14], [4, 12], [5, 11], [6, 11], [7, 11], [8, 12], [9, 13], [9, 14], [8, 16],
  230. [7, 17], [6, 17], [5, 17], [4, 16], [6, 15], [6, 18],
  231. [3, 21], [3, 22], [4, 21], [4, 22], [5, 21], [5, 22], [6, 23], [2, 23], [2, 25], [1, 25],
  232. [6, 25], [7, 25], [3, 35], [4, 35], [3, 36], [4, 36] ]
  233.  
  234. initiate(polje, gosperGliderGun);
  235.  
  236.  
  237. animate(polje);
  238. };
  239.  
  240.  
  241.  
  242. </script>
  243.  
  244. <canvas id="myCanvas" width="500" height="500"></canvas>
  245.  
  246. </body>
  247. </html>
Add Comment
Please, Sign In to add comment