//One dimensional automata based on: //http://processing.org/learning/examples/wolframca.html //detailed in A New Kind of Science (Wolfram Stephen, 2002) Automata auto; Buttons [] button; AutoButtonTemplate [] template; PFont font; boolean [] rules = { false, false, false, true, true, true, true, false}; boolean [][] ruleSets = { { true, true, true } , { true, true, false } , { true, false, true } , { true, false, false } , { false, true, true } , { false, true, false } , { false, false, true } , { false, false, false } }; void setup(){ size(500,520); font = loadFont("Kartika-48.vlw"); textFont(font, 28); auto = new Automata(400,500); button = new Buttons[9]; template = new AutoButtonTemplate[8]; for (int i = 0; i < 8; i++){ int col = color(255); if(rules[i]) col = color(0); button[i] = new Buttons(width - 60, 30 + i * 60, 27, 27, col, null); template[i] = new AutoButtonTemplate(width - 90, i * 60, ruleSets[i]); } button[8] = new Buttons(width - 90, 480, 90, 30, color(240), "Propagate"); background(255); } void draw(){ //draw gui fill(200); stroke(0); rect(width - 92, 0, 92, height); noStroke(); fill(220); for(int i = 0; i < 4; i++) rect(width - 90, i * 120, 90, 60); fill(220,220,255); for(int i = 0; i < 8; i++) rect(width - 80, 40 + i * 60, 70, 10); for(int i = 0; i < template.length; i++) template[i].draw(); for(int i = 0; i < button.length; i++) button[i].draw(); fill(0); text("hit propagate to generate pattern", 20, height - 10); } void mousePressed(){ int choice = -1; for(int i = 0; i < button.length; i++) if(button[i].over()) choice = i; switch(choice){ case 8: auto.propagate(rules); auto.draw(0,0); break; default: if(choice > -1){ rules[choice] = !rules[choice]; if(rules[choice]) button[choice].col = color(0); else button[choice].col = color(255); } break; } } class Automata{ boolean [] cell; int w,h; Automata(int w, int h){ this.w = w; this.h = h; cell = new boolean[w * h]; for(int i = 0; i < cell.length; i++) cell[i]= false; cell[w / 2] = true; } void propagate (boolean [] rules){ //clear cells for(int i = 0; i < cell.length; i++) cell[i] = false; cell[w / 2] = true; //propagate rules for(int i = 0; i < h - 1; i++) for(int j = 1; j < w - 1; j++) for(int k = 0; k < ruleSets.length; k++){ if(cell[(j - 1) + i * w] == ruleSets[k][0] && cell[j + i * w] == ruleSets[k][1] && cell[(j + 1) + i * w] == ruleSets[k][2]) cell[j + (i + 1) * w] = rules[k]; } } void draw(int x, int y){ for(int i = 0; i < cell.length; i++){ if(cell[i]) stroke(0); else stroke(255); point(x + (i % w), y + (i / w)); } } } class AutoButtonTemplate{ boolean [] cell; int x,y; AutoButtonTemplate(int x, int y, boolean [] cell){ this.x = x; this.y = y; this.cell = cell; } void draw(){ for(int i = 0; i < cell.length; i++){ stroke(0); if(cell[i]) fill(0); else fill(255); rect(x + i * 30, y, 27, 27); } } }