Loading...
Logo
Processing Forum
I'm trying to edit this program so that the changing pixels are larger, but I'm not sure how

Copy code
  1. int sx, sy; 
  2. float density = 0.5; 
  3. int[][][] world; //triple array
  4.  
  5. void setup() 
  6.   size(640, 500, P2D);
  7.   frameRate(12); //set a framerate variable--1000/framerate = new variable called frameDuration
  8.   sx = width;
  9.   sy = height;
  10.   world = new int[sx][sy][2]; 
  11.    
  12.   // Set random cells to 'on' 
  13.   for (int i = 0; i < sx * sy * density; i++) { 
  14.     world[(int)random(sx)][(int)random(sy)][1] = 1; 
  15.   } 
  16.  
  17. void draw() 
  18.   background(0); 
  19.   
  20.   // Drawing and update cycle 
  21.   for (int x = 0; x < sx; x=x+1) { 
  22.     for (int y = 0; y < sy; y=y+1) { 
  23.       //if (world[x][y][1] == 1) 
  24.       // Change recommended by The.Lucky.Mutt
  25.       if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1)) 
  26.       { 
  27.         world[x][y][0] = 1; 
  28.         set(x, y, #FFFFFF); 
  29.       } 
  30.       if (world[x][y][1] == -1) 
  31.       { 
  32.         world[x][y][0] = 0; 
  33.       } 
  34.       world[x][y][1] = 0; 
  35.     } 
  36.   } 
  37.   // Birth and death cycle 
  38.   for (int x = 0; x < sx; x=x+1) { 
  39.     for (int y = 0; y < sy; y=y+1) { 
  40.       int count = neighbors(x, y); 
  41.       if (count == 3 && world[x][y][0] == 0) // if the cell is black and has three white neighbors 
  42.       { 
  43.         world[x][y][1] = 1;
  44.        // audio here 
  45.       } 
  46.       if ((count < 2 || count > 3) && world[x][y][0] == 1) // if the cell is white and has less than 2 or more than 3 white neighbors
  47.      { 
  48.         world[x][y][1] = -1;
  49.        //audio here 
  50.       } 
  51.     } 
  52.   } 

  53. // differentiate between different coordinates on the grid
  54.  
  55. // Count the number of adjacent cells 'on' 
  56. int neighbors(int x, int y) 
  57.   return world[(x + 1) % sx][y][0] + 
  58.          world[x][(y + 1) % sy][0] + 
  59.          world[(x + sx - 1) % sx][y][0] + 
  60.          world[x][(y + sy - 1) % sy][0] + 
  61.          world[(x + 1) % sx][(y + 1) % sy][0] + 
  62.          world[(x + sx - 1) % sx][(y + 1) % sy][0] + 
  63.          world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] + 
  64.          world[(x + 1) % sx][(y + sy - 1) % sy][0]; 

Replies(2)

I dont think thats easily done with still using the "set" command to make a pixel black/white, cause it only effects one single pixel. Better draw a rectangle then for which you can specify width and height. If you then have a variable like "zoom" you can set that to a value of e.g. 5 and add it in every size, cell position and dimension. So you'd have to say size(640*zoom, 480*zoom); and e.g. for the cell drawing rect(cellPosX*zoom, cellPosY*zoom, yourWidth*zoom, yourHeight*zoom);
You can do this, via scaling, using 3 modifications.

Change this

  sx = width;
  sy = height;

to this

  sx = width/4;
  sy = height/4;

Change this

  background(0); 

to this

  background(0); 
  scale(4);
  strokeWeight(3); // this leaves a little border between the cells, use 4 if you want it solid.
  stroke(255);

Change this

      set(x, y, #FFFFFF);

to this

      point(x,y);