/* Slow version of tuesday's floodfill example * This will take a break and display the image once every 100 pixels processed * Note that since a pixel is always taken from the end of tocheck, it will proceed * rightward first, since the rightmost neighbor is added to tocheck last. * tocheck is operating as a stack, where the first item processed is the last inserted. * This is LIFO - last in, first out. If it was FIFI - first in, first out, then the * fill would proceed outward from a central point. */ PImage img; void setup() { img = loadImage("spiralthing.png"); image(img, 0, 0); size(1000, 1000); } void draw() { image(img, 0, 0); decount = 0; ff_continue(); } int qsize = 0; int qbegin = 0; void qin(int newp){ tocheck[qbegin + qsize++] = newp; } int qout(){ qsize--; return tocheck[qbegin++]; } int tocheck[] = new int[100000]; // This is 400 KB of space. That used to be a lot! int decount = 0; void ff_continue() { while (tcsize > 0) { if (decount > 100) { break; } decount++; // Check to see if the last pixel in tocheck needs to be filled // If not, move on to the next pixel // If so, add the neighbors to tocheck if they need to be filled tcsize--; int curr = tocheck[tcsize]; if (red(img.pixels[curr]) < 128) { img.pixels[curr] = color(255, 100, 100); img.updatePixels(); if (curr - width > 0 && red(img.pixels[curr - width]) < 128 ) { tocheck[tcsize] = curr - width; tcsize++; } if (curr + width < (height * width) && red(img.pixels[curr + width]) < 128 ) { tocheck[tcsize] = curr + width; tcsize++; } if (curr - 1 > 0 && red(img.pixels[curr - 1]) < 128 ) { tocheck[tcsize] = curr - 1; tcsize++; } if (curr + 1 < width*height && red(img.pixels[curr + 1]) < 128 ) { tocheck[tcsize] = curr + 1; tcsize++; } } } img.updatePixels(); } void floodFill(int x, int y) { img.loadPixels(); int startpoint = x + y*width; tocheck[0] = startpoint; tcsize = 1; } void mouseClicked() { floodFill(mouseX, mouseY); }