|
Author |
Topic: Region filling sourcecode (Read 614 times) |
|
gilazilla
|
Region filling sourcecode
« on: Dec 14th, 2004, 10:20am » |
|
anyone with simple region filling algorithm? i have multiple objects with boundary that needs to be filled with color white.It is a black and white image? Sourcecode would be appreciated.
|
|
|
|
arielm
|
Re: Region filling sourcecode
« Reply #1 on: Dec 14th, 2004, 1:30pm » |
|
here's a snippet i developed a long ago which does the job: Code:void setup() { size(400, 200); noBackground(); noStroke(); fill(255, 0, 0); } void loop() { if (mousePressed) { if (!keyPressed) { rectMode(CENTER_DIAMETER); rect(mouseX, mouseY, 20, 20); } else { fillAt(mouseX, mouseY, 0); } } } // homemade fifo queue based on a "circular buffer"... int QUEUE_CAPACITY = 200 * 8; // min(width, height) * 8 int queue_buf_x[] = new int[QUEUE_CAPACITY]; int queue_buf_y[] = new int[QUEUE_CAPACITY]; int queue_head, queue_tail; int queue_size; void enqueue(int x, int y) { queue_buf_x[queue_tail] = x; queue_buf_y[queue_tail] = y; queue_tail = (queue_tail + 1) % QUEUE_CAPACITY; queue_size++; } void fillAt(int x, int y, int col_dst) { int col_src = getPixel(x, y); if (col_src == col_dst) { return; } // reseting queue queue_head = queue_tail = queue_size = 0; enqueue(x, y); while (queue_size > 0) { // dequeue-ing x = queue_buf_x[queue_head]; y = queue_buf_y[queue_head]; queue_head = (queue_head + 1) % QUEUE_CAPACITY; queue_size--; if (getPixel(x, y) == col_src) { setPixel(x, y, col_dst); if (y + 1 < height) { enqueue(x, y + 1); } if (y > 0) { enqueue(x, y - 1); } if (x + 1 < width) { enqueue(x + 1, y); } if (x > 0) { enqueue(x - 1, y); } } } } |
| notes: - press the mouse to draw (red) shapes / press while holding a key to paint (in black...) - it's based on http://research.microsoft.com/~hollasch/cgindex/polygons/floodfill.html (seems to be a dead link nowadays...) - it's been done with P5 version 056 or so, so you may need to tweak the code a bit, e.g.: removing "noBackground()", changing "setPixel()" to "set()" and "getPixel()" to "get()", etc...
|
Ariel Malka | www.chronotext.org
|
|
|
|