import org.seltar.Bytes2Web.*; final int SIZE = 200; final int WIDTH = 2; final int COUNT = 4000; class Cell { int row, col; boolean fixed; } Cell[][] grid; Cell[] cells; boolean seeded; Capture capture; void setup() { size(SIZE * WIDTH, SIZE * WIDTH); cursor(CROSS); String host = param("host"); if (host == null) { host = "localhost"; } capture = new Capture(this, "diffuse", "http://" + host + "/processing/"); capture.x = SIZE * WIDTH - capture.getWidth() - 4; capture.y = SIZE * WIDTH - capture.getHeight() - 4; PFont font = loadFont("Georgia-18.vlw"); textFont(font); noLoop(); } void mousePressed() { if (!seeded) { cells = new Cell[COUNT]; grid = new Cell[SIZE][SIZE]; Cell seed = new Cell(); seed.fixed = true; grid[mouseY / WIDTH][mouseX / WIDTH] = seed; for (int i = COUNT - 1; i >= 0; i--) { int row, col; do { row = floor(random(0, SIZE)); col = floor(random(0, SIZE)); } while (grid[row][col] != null); cells[i] = new Cell(); cells[i].row = row; cells[i].col = col; grid[row][col] = cells[i]; } seeded = true; loop(); } } void draw() { background(255); if (!seeded) { textAlign(CENTER); fill(#333333); text("Please CLICK to set the seed particle and begin.", width >> 1, height >> 1); } else { fill(0); noStroke(); for (int row = SIZE - 1; row >= 0; row--) { for (int col = SIZE - 1; col >= 0; col--) { if (grid[row][col] != null) { rect(col * WIDTH, row * WIDTH, WIDTH, WIDTH); if (!grid[row][col].fixed) { for (int r = Math.max(0, row - 1), rend = Math.min(row + 1, SIZE - 1); r <= rend; r++) { for (int c = Math.max(0, col - 1), cend = Math.min(col + 1, SIZE - 1); c <= cend; c++) { if (grid[r][c] != null) { grid[row][col].fixed |= grid[r][c].fixed; if (grid[row][col].fixed) { break; } } } } } } } } boolean done = true; for (int i = COUNT - 1; i >= 0; i--) { if (!cells[i].fixed) { int row = cells[i].row; int col = cells[i].col; boolean canmove = false; for (int r = Math.max(0, row - 1), rend = Math.min(row + 1, SIZE - 1); r <= rend; r++) { for (int c = Math.max(0, col - 1), cend = Math.min(col + 1, SIZE - 1); c <= cend; c++) { if (grid[r][c] == null) { canmove = true; break; } } } if (canmove) { int dr, dc; do { dr = round(random(-1, 1)); dc = round(random(-1, 1)); } while (((row + dr) < 0) || ((row + dr) > (SIZE - 1)) || ((col + dc) < 0) || ((col + dc) > (SIZE - 1)) || (grid[row + dr][col + dc] != null)); grid[row + dr][col + dc] = grid[row][col]; grid[row][col] = null; cells[i].row = row + dr; cells[i].col = col + dc; done = false; } } } if (done) { capture.draw(); } } }