Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas, ctx;
  2. var interval;
  3. var width, height;
  4. var size,step;
  5. var Sum_1;
  6.       color1 = Math.floor(Math.random()*16777215).toString(16)
  7.       color1 = "#"+("000000" + color1).slice(-6)
  8.       color2 = Math.floor(Math.random()*16777215).toString(16)
  9.       color2 = "#"+("000000" + color2).slice(-6)
  10.  
  11. function setup() {
  12.     width = 500;
  13.     height = 500;
  14.     canvas = document.getElementById("scrawl");
  15.     ctx = canvas.getContext("2d");
  16.     size=125;
  17.     step=500/size;
  18.     Sum_1=0;
  19.  
  20.  
  21.     initialize();
  22.     interval = setInterval(run,2);
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29. // Intiiales Setzen der Boards
  30. function initialize() {
  31.     Old = new Array (size);
  32.     New = new Array (size);
  33.     //Anzahl der Nachbarn
  34.     Neigh = new Array (size);
  35.     Ratio1 = new Array (size);
  36.  
  37.     for (i = 0; i < Old . length; ++ i){
  38.         Old [i] = new Array (size);
  39.         New [i] = new Array (size);
  40.         Neigh [i] = new Array (size);
  41.         Ratio1 [i] = new Array (size);
  42.  
  43.     }
  44.     for (i = 0; i < size; ++ i){
  45.         for (j = 0; j < size; ++ j){
  46.             Ratio1 [i][j]=0;
  47.             Neigh[i][j]=8;
  48.             if (i===0 || i===size-1) {
  49.                 Neigh[i][j]=5;
  50.                 if(j===0 || j===size-1) {
  51.                     Neigh[i][j]=3
  52.                 }
  53.             }
  54.             if (j===0 || j===size-1) {
  55.                 Neigh[i][j]=5;
  56.                 if(i===0 || i===size-1) {
  57.                     Neigh[i][j]=3
  58.                 }
  59.             }
  60.  
  61.  
  62.             if (i<size/2){
  63.                 Old [i][j] = 1;
  64.                 Sum_1+=1
  65.             }
  66.             else {
  67.                 Old [i][j] = 0;
  68.             }
  69.             New [i][j] = Old [i][j];
  70.         }
  71.     }
  72.     Sum_1=Sum_1/(size*size);
  73. }
  74.  
  75.  
  76. function ratio() {
  77.     for (i = 0; i < size; ++ i){
  78.         for (j = 0; j < size; ++ j){
  79.             Ratio1[i][j]=0;
  80.             if(i>0){
  81.                 if(j>0){        Ratio1[i][j]+=Old[i-1][j-1];}
  82.                                 Ratio1[i][j]+=Old[i-1][j];
  83.                 if(j<size-1){   Ratio1[i][j]+=Old[i-1][j+1];}
  84.             }
  85.  
  86.             if(j>0){        Ratio1[i][j]+=Old[i][j-1];}
  87.             if(j<size-1){   Ratio1[i][j]+=Old[i][j+1];}
  88.  
  89.             if(i<size-1){
  90.                 if(j>0){        Ratio1[i][j]+=Old[i+1][j-1];}
  91.                                 Ratio1[i][j]+=Old[i+1][j];
  92.                 if(j<size-1){   Ratio1[i][j]+=Old[i+1][j+1];}
  93.             }
  94.  
  95.             Ratio1[i][j]=Ratio1[i][j]/Neigh[i][j];
  96.         }
  97.     }
  98.         console.log(Neigh[1][1]);
  99.     console.log(Ratio1[1][1]);
  100.             console.log(Sum_1);
  101.  
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. function draw() {
  109.    
  110.  
  111.     for (i = 0; i < size; ++ i){
  112.         for (j = 0; j < size; ++ j){
  113.                     ctx.fillStyle = color1;
  114.                     if(Old[i][j]===1){ctx.fillStyle = color2;}                     
  115.                 ctx.fillRect (i*step, j*step, step, step);
  116.             }
  117.         }
  118. }
  119.  
  120. function calculate() {
  121.  
  122.     for (i = 0; i < size; ++ i){
  123.         for (j = 0; j < size; ++ j){
  124.             help=Math.random();
  125.  
  126.             if ((Ratio1[i][j])>help){
  127.                 Old[i][j]=1;
  128.             } else{Old[i][j]=0;
  129.             }
  130.         }
  131.     }
  132.    
  133.  
  134.     Sum_1=0;
  135.     for (i = 0; i < size; ++ i){
  136.         for (j = 0; j < size; ++ j){
  137.             if (Old[i][j]==1) Sum_1+=1;
  138.         }
  139.     }
  140.     Sum_1=Sum_1/(size*size);
  141.  
  142.  
  143. }
  144.  
  145.  
  146. function run() {
  147.     ratio();
  148.     draw();
  149.     calculate();
  150.      
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement