// Implments a 1D cellular automaton // Two colors are allowed. int myPixels[][]; void setup() { size(400, 300); // create the array myPixels = new int[height][width]; firstRow(); calcValues(); drawResult(); } // randomly set the first row void firstRow() { for (int col = 0; col < width; col++) { if (random(1) < .5) myPixels[0][col] = 0; else myPixels[0][col] = 1; } } // Calculate the current row based on the previous row void calcValues() { for (int row = 1; row < height; row++) { for (int col = 1; col < width-1; col++) { int sum = myPixels[row-1][col-1] + 2*myPixels[row-1][col] + 4*myPixels[row-1][col+1]; // This sets the rule to use: if (sum ==0 || sum == 3 || sum == 5 || sum == 2) { myPixels[row][col] = 0; } else { myPixels[row][col] = 1; } } } } // Draw the result void drawResult() { for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { if (myPixels[row][col] ==0) { stroke(255, 0, 0); } else { stroke(255, 255, 0); } point(col,row); } } }