// Gliders // By Brian Prentice // December 2008 int Width = 1500; int Height = 1000; int ControlHeight = 50; int ScrollerWidth = 160; int ScrollerHeight = 20; int noGliders = 25; int noCells = 15; int dnaLength = 100; int noRows; int noColumns; int gridSize = 10; int xOffset; int yOffset; boolean showGrid = false; Glider[] gliders; boolean bidirectional = false; boolean running = true; boolean tracing = false; boolean randomNoCells = false; boolean randomDnaLength = false; PFont font; IntScroller noGlidersInput; IntScroller noCellsInput; IntScroller dnaLengthInput; void setup() { size(1500, 1050, P2D); // Width, Height + ControlHeight font = loadFont("SansSerif-14.vlw"); textFont(font, 14); textAlign(LEFT, CENTER); setupControls(); setSize(); makeGliders(); } void draw() { if (( ! tracing) || (gliders == null)) background(0); setSize(); if (showGrid) drawGrid(); checkScrollers(); if (gliders != null) { for (int n = 0; n < noGliders; n++) { gliders[n].draw(); if (running) gliders[n].update(); } } noGlidersInput.draw(); noCellsInput.draw(); dnaLengthInput.draw(); } void keyPressed() { noGlidersInput.keyPressed(); noCellsInput.keyPressed(); dnaLengthInput.keyPressed(); if (key == ENTER) running = ! running; else if ((key == ' ') && (gliders != null)) for (int n = 0; n < noGliders; n++) gliders[n].update(); else if (key == 'c') gliders = null; else if ((key == 'g') && (! tracing)) showGrid = ! showGrid; else if (key == 'r') { bidirectional = false; makeGliders(); } else if (key == 'b') { bidirectional = true; makeGliders(); } else if (key == 't') tracing = ! tracing; else if (key == 'n') { if (randomNoCells) noCellsInput.setName("No Cells: "); else noCellsInput.setName("Max No Cells: "); randomNoCells = ! randomNoCells; } else if (key == 'l') { if (randomDnaLength) dnaLengthInput.setName("DNA Length: "); else dnaLengthInput.setName("Max DNA Length: "); randomDnaLength = ! randomDnaLength; } else if (key == 'o') saveFrame(); else if ((key == '+') && (gridSize < (width - 2)) && (gridSize < (height - 2))) { gridSize++; showGrid = true; gliders = null; } else if ((key == '-') && (gridSize > 1)) { gridSize--; showGrid = true; gliders = null; } } void setupControls() { int spacing = (Width - 3 * ScrollerWidth) / 4; int x = (Width - 2 * spacing - 3 * ScrollerWidth) / 2; int y = Height + (ControlHeight - ScrollerHeight) / 2; noGlidersInput = new IntScroller("No Gliders: ", noGliders, 1, 100, x, y); x += ScrollerWidth + spacing; noCellsInput = new IntScroller("No Cells: ", noCells, 1, 200, x, y); x += ScrollerWidth + spacing; dnaLengthInput = new IntScroller("DNA Length: ", dnaLength, 1, 200, x, y); } void setSize() { noColumns = (width - 2) / gridSize; noRows = (height - ControlHeight - 2) / gridSize; xOffset = (width - noColumns * gridSize) / 2; yOffset = (height - ControlHeight - noRows * gridSize) / 2; } void drawGrid() { if (gridSize > 3) { stroke(255); for (int r = yOffset; r < (noRows + 1) * gridSize; r += gridSize) line(xOffset, r, xOffset + noColumns * gridSize, r); for(int c = xOffset; c < (noColumns + 1) * gridSize; c += gridSize) line(c, yOffset, c, yOffset + noRows * gridSize); } else noStroke(); } int checkInput(IntScroller scroller, int oldValue) { int newValue = scroller.getValue(); if (newValue != oldValue) gliders = null; return newValue; } void checkScrollers() { noGliders = checkInput(noGlidersInput, noGliders); noCells = checkInput(noCellsInput, noCells); dnaLength = checkInput(dnaLengthInput, dnaLength); } void makeGliders() { gliders = new Glider[noGliders]; for (int g = 0; g < noGliders; g++) { color gliderColor = makeColor(); int l = dnaLength; if (randomDnaLength) l = int(random(l)) + 1; int n = noCells; if (randomNoCells) n = int(random(n)) + 1; gliders[g] = new Glider(noColumns / 2, noRows / 2, gliderColor, l, n, bidirectional); } } int makeColor() { int[] rgb = new int[3]; int i = int(random(3)); rgb[i] = int(random(56)) + 200; rgb[(i + 1) % 3] = int(random(200)); rgb[(i + 2) % 3] = int(random(100)); return color(rgb[0], rgb[1], rgb[2]); }