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