Advertisement
FallenEagle

The Infinite Fight of Pixels [Complete]

Nov 26th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. <script type="text/javascript">
  2.  
  3. var _gaq = _gaq || [];
  4. _gaq.push(['_setAccount', 'UA-36545784-1']);
  5. _gaq.push(['_trackPageview']);
  6.  
  7. (function() {
  8. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  9. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  10. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  11. })();
  12.  
  13. </script>
  14.  
  15. <style>
  16. footer {
  17. position: absolute;
  18. bottom: 10;
  19. }
  20. </style>
  21. <script type="application/javascript">
  22.  
  23. var canvas, ctx;
  24. var interval;
  25. var width, height;
  26. var size,step;
  27. var Sum_1;
  28. color1 = Math.floor(Math.random()*16777215).toString(16)
  29. color1 = "#"+("000000" + color1).slice(-6)
  30. color2 = Math.floor(Math.random()*16777215).toString(16)
  31. color2 = "#"+("000000" + color2).slice(-6)
  32.  
  33. function setup() {
  34. width = 500;
  35. height = 500;
  36. canvas = document.getElementById("scrawl");
  37. ctx = canvas.getContext("2d");
  38. size=125;
  39. step=500/size;
  40. Sum_1=0;
  41.  
  42.  
  43. initialize();
  44. interval = setInterval(run,2);
  45.  
  46. }
  47.  
  48.  
  49.  
  50.  
  51. // Intiiales Setzen der Boards
  52. function initialize() {
  53. Old = new Array (size);
  54. New = new Array (size);
  55. //Anzahl der Nachbarn
  56. Neigh = new Array (size);
  57. Ratio1 = new Array (size);
  58.  
  59. for (i = 0; i < Old . length; ++ i){
  60. Old [i] = new Array (size);
  61. New [i] = new Array (size);
  62. Neigh [i] = new Array (size);
  63. Ratio1 [i] = new Array (size);
  64.  
  65. }
  66. for (i = 0; i < size; ++ i){
  67. for (j = 0; j < size; ++ j){
  68. Ratio1 [i][j]=0;
  69. Neigh[i][j]=8;
  70. if (i===0 || i===size-1) {
  71. Neigh[i][j]=5;
  72. if(j===0 || j===size-1) {
  73. Neigh[i][j]=3
  74. }
  75. }
  76. if (j===0 || j===size-1) {
  77. Neigh[i][j]=5;
  78. if(i===0 || i===size-1) {
  79. Neigh[i][j]=3
  80. }
  81. }
  82.  
  83.  
  84. if (i<size/2){
  85. Old [i][j] = 1;
  86. Sum_1+=1
  87. }
  88. else {
  89. Old [i][j] = 0;
  90. }
  91. New [i][j] = Old [i][j];
  92. }
  93. }
  94. Sum_1=Sum_1/(size*size);
  95. }
  96.  
  97.  
  98. function ratio() {
  99. for (i = 0; i < size; ++ i){
  100. for (j = 0; j < size; ++ j){
  101. Ratio1[i][j]=0;
  102. if(i>0){
  103. if(j>0){ Ratio1[i][j]+=Old[i-1][j-1];}
  104. Ratio1[i][j]+=Old[i-1][j];
  105. if(j<size-1){ Ratio1[i][j]+=Old[i-1][j+1];}
  106. }
  107.  
  108. if(j>0){ Ratio1[i][j]+=Old[i][j-1];}
  109. if(j<size-1){ Ratio1[i][j]+=Old[i][j+1];}
  110.  
  111. if(i<size-1){
  112. if(j>0){ Ratio1[i][j]+=Old[i+1][j-1];}
  113. Ratio1[i][j]+=Old[i+1][j];
  114. if(j<size-1){ Ratio1[i][j]+=Old[i+1][j+1];}
  115. }
  116.  
  117. Ratio1[i][j]=Ratio1[i][j]/Neigh[i][j];
  118. }
  119. }
  120. console.log(Neigh[1][1]);
  121. console.log(Ratio1[1][1]);
  122. console.log(Sum_1);
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. function draw() {
  131.  
  132.  
  133. for (i = 0; i < size; ++ i){
  134. for (j = 0; j < size; ++ j){
  135. ctx.fillStyle = color1;
  136. if(Old[i][j]===1){ctx.fillStyle = color2;}
  137. ctx.fillRect (i*step, j*step, step, step);
  138. }
  139. }
  140. }
  141.  
  142. function calculate() {
  143.  
  144. for (i = 0; i < size; ++ i){
  145. for (j = 0; j < size; ++ j){
  146. help=Math.random();
  147.  
  148. if ((Ratio1[i][j])>help){
  149. Old[i][j]=1;
  150. } else{Old[i][j]=0;
  151. }
  152. }
  153. }
  154.  
  155.  
  156. Sum_1=0;
  157. for (i = 0; i < size; ++ i){
  158. for (j = 0; j < size; ++ j){
  159. if (Old[i][j]==1) Sum_1+=1;
  160. }
  161. }
  162. Sum_1=Sum_1/(size*size);
  163.  
  164.  
  165. }
  166.  
  167.  
  168. function run() {
  169. ratio();
  170. draw();
  171. calculate();
  172.  
  173.  
  174. }
  175.  
  176. </script>
  177. </head>
  178. <style>
  179. body
  180. {
  181.  
  182. }
  183. </style>
  184.  
  185. <body onload="setup()">
  186.  
  187. <center>
  188. <br>
  189. <br>
  190.  
  191. <canvas id="scrawl" width="500" height="500"></canvas>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement