|
Author |
Topic: Recursion? (Read 510 times) |
|
v3ga
|
Recursion?
« on: Mar 1st, 2003, 11:14am » |
|
Hello, I'm having troubles with this little program. It seems that the recursion stops at a certain level. Filling the box works fine , while filling the space around the box doesn't seem to work : the drawing stops in the middle of the screen. //************************************** int W = 500; int H = 250; void setup() { size(W, H); noFill(); colorMode(RGB, 1); background(1.0,1.0,1.0); rectMode(CORNER); } void loop() { stroke(255,0,0); rect(10,10,50,50); floodFill(5,5); // floodFill(15,15) works well } void floodFill(int x,int y) { if ( x<0 || x>W-1) return; if ( y<0 || y>H-1) return; if (pixels[y*W + x] == 0xffff0000) return; pixels[y*W + x] = 0xffff0000; floodFill(x ,y+1); floodFill(x+1,y ); floodFill(x-1,y ); floodFill(x ,y-1); }
|
http://v3ga.net
|
|
|
Martin
|
Re: Recursion?
« Reply #1 on: Mar 1st, 2003, 2:46pm » |
|
I don't know if this would make sense but... Code:int W = 500; int H = 250; void setup() { size(W, H); noFill(); colorMode(RGB, 1); background(1,1,1); rectMode(CORNER); } void draw() { stroke(255,0,0); rect(10,10,50,50); // floodFill(10,10,50,50); // :D // floodFill(15,15) works well floodFill(15,15,40,40); } void floodFill(int x,int y, int x1, int y1) { // if ( x<0 || x>W-1 ) return; // if ( y<0 || y>H-1) return; // if (pixels[y*W + x] == 0xffff0000) return; // pixels[y*W + x] = 0xffff0000; // floodFill(x ,y+1); // floodFill(x+1,y ); // floodFill(x-1,y ); // floodFill(x ,y-1); if( !(x<0 || x>W-1) && !(y<0 || y>H-1) ) // && // !(pixels[y*W + x] == 0xffff0000) ) { for(int a = x; a <= (x+x1); a++) { for(int b = y; b <= (y+y1); b++ ) { /**/ setPixel(a,b,0xffff0000); } } } } |
| ... hmm... what exactly are you trying to do? hehe.
|
|
|
|
v3ga
|
Re: Recursion?
« Reply #2 on: Mar 1st, 2003, 5:31pm » |
|
Thanks for answering, but that's not exactly what i am trying to do I just want to fill an arbitrary shape. Take a pixel inside a shape (which has no holes) and then let the resursive stuff do the thing. With the same setup(), let's try : void loop() { stroke(255,0,0); ellipse(150, 60, 55, 70); floodFill(150,60); } void floodFill(int x,int y) { if ( x<0 || x>W-1) return; if ( y<0 || y>H-1) return; if (pixels[y*W + x] == 0xffff0000) return; pixels[y*W + x] = 0xffff0000; floodFill(x ,y+1); floodFill(x+1,y ); floodFill(x-1,y ); floodFill(x ,y-1); } This works fine. But It you try floodFill(0,0) (ie you want to fill the screen except the ellipse) the recursion stops. I'm guessing there's some internal limitation concerning the depth of recursion. Or my algo is perhaps wrong?
|
http://v3ga.net
|
|
|
Martin
|
Re: Recursion?
« Reply #3 on: Mar 1st, 2003, 6:53pm » |
|
Ah yes, should've noticed that. Anyway, it's a stacking problem since recursion is expensive, that's why there an overflow. A way around this would be to make a queue of pixels that it still has to fill then take a pixel off the head of the queue and color the pixels around it, then adds those pixels to the queue. Good to use push and pop. Hmm... I'll post something a little later. ps. btw, floodFill(150,60) doesn't work. however, floodFill(155,85) works.
|
« Last Edit: Mar 1st, 2003, 7:02pm by Martin » |
|
|
|
|
v3ga
|
Re: Recursion?
« Reply #4 on: Mar 2nd, 2003, 11:26pm » |
|
Thank you for having looked at the problem.Your idea about queueing pixels seems nice
|
http://v3ga.net
|
|
|
Martin
|
Re: Recursion?
« Reply #5 on: Mar 3rd, 2003, 3:54pm » |
|
um... try iterating instead of recursing. ... too many things to do right now to post code. too tired. too many things to do...
|
« Last Edit: Mar 3rd, 2003, 3:58pm by Martin » |
|
|
|
|
|