Weather / Spam / www |
Lyon Weather
aldweb against spam
|
|
|
|
|
|
|
- Forum - iziBasic
Warning: A non-numeric value encountered in /web5/aldweb/www/aldweb_com/www/thread.php on line 257
Topic # 1421 |
Flood Fill Routine |
29/05/2006 @ 13:42 by Garfield
|
Hi Guys
I've written a flood fill routine for anyone who is interested. The subroutine is below.
When called, the X and Y variables need to hold the coordinate of where the flood fill should begin, and the subroutine will take the colour of this point and fill the area that is in this colour. This means that if this area is bordered by other colours it will respect them and will not spill into them.
The routine can be easily modified to fill up to a specific colour instead, and therefore fill in an area outlined by one colour regardless of the various colours that may exist within the area to be filled.
Variables V and W should hold the resolution of the screen, so 160 for low res and 320 for high res. I know that using two variables is not necessary, but thinking for the future there MAY be a possibility that at some point we may be able to use a 320x480 screen, and this routine is therefore future-proof!
My biggest problem was finding a big enough stack. I couldn't reserve enough variables using DIM, so what I've done is I've used the MegaString as my stack and it works very well. I use two bytes per number which means I use four bytes per coordinate, and that gives me a total of 1024 coordinates that I can stack.
I've written flood fills before using recursive subroutines which effectively use a LIFO stack. However, that requires a lot of stack space, so I've emulated a FIFO stack with the MegaString which gives more than enough space.
One thing I've now noticed (which may also explain why my filled-in triangles routine may have stuck out of an outline) is that, in low resolution, even though the LINE command draws lines that maintain the thickness of that low resolution, if your Palm supports high resolution the line will be drawn using that high resolution to make it look smoother. This means that, in low resolution, my fill routine will appear to leave gaps where the fill meets the line. This is because of the Palm using the higher resolution to draw the line and it is the line that has left that gap and not my fill routine. If you fill using high resolution, no gaps will remain.
Enough talk - here's the subroutine:
_FloodFill: Z=PGET(X,Y) ' Get colour to fill V=160 : W=160 ' Set to resolution of screen S=0 : T=0 GOSUB _AddToStack
REPEAT GOSUB _GetFromStack
INC X : IF X<V GOSUB _AddToStack DEC X : INC Y : IF Y<W GOSUB _AddToStack DEC Y : DEC X : IF X>=0 GOSUB _AddToStack INC X : DEC Y : IF Y>=0 GOSUB _AddToStack
UNTIL S=T RETURN
_AddToStack: IF S=-1 RETURN U=PGET(X,Y) ' Get colour of point IF U<>Z RETURN ' Exit if not the correct colour to fill PSET X,Y ' Otherwise fill it
' Increase pointer and add coordinate to stack INC S : IF S=1024 S=0 IF S=T S=-1 : T=-1 : RETURN
U=X/256 : U=INT(U) : Z$=CHR$(U) U=S*4+1 : PUTCHAR$$ Z$,U U=X MOD 256 : Z$=CHR$(U) U=S*4+2 : PUTCHAR$$ Z$,U
U=Y/256 : U=INT(U) : Z$=CHR$(U) U=S*4+3 : PUTCHAR$$ Z$,U U=Y MOD 256 : Z$=CHR$(U) U=S*4+4 : PUTCHAR$$ Z$,U
RETURN
_GetFromStack: ' Increase pointer and get coordinate from stack INC T : IF T=1024 T=0 U=T*4+1 : Z$=GETCHAR$$(U) U=ASC(Z$) : X=U*256 U=T*4+2 : Z$=GETCHAR$$(U) U=ASC(Z$) : X=X+U
U=T*4+3 : Z$=GETCHAR$$(U) U=ASC(Z$) : Y=U*256 U=T*4+4 : Z$=GETCHAR$$(U) U=ASC(Z$) : Y=Y+U
RETURN
|
|
|
Warning: A non-numeric value encountered in /web5/aldweb/www/aldweb_com/www/thread.php on line 497
Answer n° 1 -------- 29/05/2006 @ 14:11 by Garfield
visitor |
Hi
Here's the body of a program to enable you to try my flood fill routine. It allows you to draw whatever you want on the screen, but it will close the object when you lift your pen off the screen. This is to ensure the fill can't escape through any holes.
Once you've drawn enough, click on the 'Home' icon (as if you want to exit the program) and this will tell the program you've finished drawing. Then tap anywhere on the screen where you want the flood fill to begin, and watch it go.
BEGIN S=0 REPEAT Z=DOEVENTS IF Z=1000 THEN Z=PENDOWN IF Z=1 THEN X=PENX : Y=PENY IF S=0 V=X : W=Y : PSET X,Y LINETO X,Y S=1 ELSE S=0 : LINETO V,W ENDIF ENDIF UNTIL Z=-1
REPEAT Z=DOEVENTS Z=PENDOWN UNTIL Z=1
X=PENX Y=PENY
GOSUB _FloodFill
REPEAT Z=WAITEVENT UNTIL Z=-1
END
|
|
|
Warning: A non-numeric value encountered in /web5/aldweb/www/aldweb_com/www/thread.php on line 497
Answer n° 2 -------- 30/05/2006 @ 22:31 by aldweb
visitor |
Hello Garfield,
I love your trick of using the MegaString to develop your flood routine. It is true that iziBasic's data stacks are a little bit too tiny for this kind of doing but smart people always find smart ways of doing great things with limited capabilities.
This is why I like the challenge of programming on a Palm device which already offers a so huge RAM when compared to my very first 1 KByte Sharp Pocket Computer back in 1982... (yeah, I'm getting old ).
Cheers, aldweb
|
|
|
Warning: A non-numeric value encountered in /web5/aldweb/www/aldweb_com/www/thread.php on line 497
Answer n° 3 -------- 01/06/2006 @ 20:02 by Garfield
visitor |
Hi Aldweb
I know what you mean. This type of programming is taking me back to the days I had my BBC Computer with 32K. Quite an improvement over your Sharp with 1K, but the challenges were there nevertheless!
Am I also showing my age?
|
|
|
topic active
topic closed
Sticky
New message -
Correct message
Close topic
Make sticky
|
|
|
|
|
|