'=========================================================================== ' Subject: FLOOD FILL ROUTINE Date: 12-23-97 (00:57) ' Author: Petter Holmberg Code: QB, QBasic, PDS ' Origin: petter.holmberg@usa.net Packet: GRAPHICS.ABC '=========================================================================== ' SCREEN 13 flood fill routine by Petter Holmberg, -97: '$DYNAMIC TYPE pixtype x AS INTEGER y AS INTEGER END TYPE DECLARE SUB FloodFill (x%, y%, col%) CLS SCREEN 0 WIDTH 80, 25 PRINT "SCREEN 13 flood fill routine by Petter Holmberg, -97:" PRINT PRINT "Finally! The routine everyone have been waiting for: A fully functional non-" PRINT "recursive flood fill routine for SCREEN 13!" PRINT PRINT "This routine is similar to the PAINT statement, it will fill an arbitraty area" PRINT "with any color. but unlike PAINT, who fills the area until it hits a certain" PRINT "bordercolor, this routine will stop filling when it hits any other color than" PRINT "the one being filled. This is the kind of flood fill routine you would like" PRINT "to have in a paint program. I'm sure many of you know what I'm talking about." PRINT "The routine is a little slower than PAINT in large areas, but in complex" PRINT "images it will actually be faster than PAINT. This routine is small, doesn't" PRINT "need much memory and works for all images. You'll never use PAINT again! :-)" PRINT PRINT "You may use this routine in any way you like, as long as you give me credits!" PRINT "If you use it in a program of yours, I would be glad if you sent me a mail and" PRINT "told me about it. my email address is: petter.holmberg@usa.net" PRINT PRINT "Now enjoy the demonstration of this routine..." PRINT PRINT "Special thanks to Thomas Nyberg for advice and testing!" DO: LOOP WHILE INKEY$ = "" SCREEN 13 LOCATE 1, 1 PRINT "In the beginning, there was only PAINT! " PRINT " Good enough for most images... " CIRCLE (159, 99), 100, 15 DO: LOOP WHILE INKEY$ = "" PAINT (159, 99), 14, 15 DO: LOOP WHILE INKEY$ = "" LOCATE 24, 1 PRINT " ...but far from all!!! "; DO: LOOP WHILE INKEY$ = "" PAINT (159, 99), 0, 15 LINE (65, 23)-(253, 175), 12, B DO: LOOP WHILE INKEY$ = "" PAINT (159, 99), 14, 15 DO: LOOP WHILE INKEY$ = "" CLS LOCATE 1, 1 PRINT " Then came this flood fill routine... " CIRCLE (159, 99), 100, 15 LINE (65, 23)-(253, 175), 12, B DO: LOOP WHILE INKEY$ = "" FloodFill 159, 99, 14 LOCATE 24, 1 PRINT "...and there was never more a problem!!!"; DO: LOOP WHILE INKEY$ = "" CLS FOR y% = 0 TO 199 STEP 2 FOR x% = 0 TO 319 STEP 2 PSET (x%, y%) NEXT x% NEXT y% LOCATE 1, 1 PRINT "With the usual PAINT..." PAINT (159, 99), 6, 15 DO: LOOP WHILE INKEY$ = "" CLS FOR y% = 0 TO 199 STEP 2 FOR x% = 0 TO 319 STEP 2 PSET (x%, y%) NEXT x% NEXT y% LOCATE 1, 1 PRINT "With this flood fill routine..." FloodFill 159, 99, 9 DO: LOOP WHILE INKEY$ = "" CLS x1% = 4 x2% = 319 y1% = 0 y2% = 199 FOR i% = 1 TO 24 LINE (x1% - 4, y1%)-(x2%, y1%), 7 LINE (x2%, y1%)-(x2%, y2%), 7 LINE (x2%, y2%)-(x1%, y2%), 8 LINE (x1%, y2%)-(x1%, y1% + 4), 8 x1% = x1% + 4 x2% = x2% - 4 y1% = y1% + 4 y2% = y2% - 4 NEXT i% LOCATE 13, 16 PRINT "Watch this!"; LINE (130, 101)-(131, 101), 12 FloodFill 199, 99, 12 DO: LOOP WHILE INKEY$ = "" CLS PRINT "The final challange! The following image" PRINT "is too complex for PAINT to fill. With" PRINT "PAINT, you will run out of memory when" PRINT "trying to fill it." DO: LOOP WHILE INKEY$ = "" CLS FOR y% = 0 TO 199 IF y% AND 1 THEN FOR x% = 2 TO 319 STEP 4 IF POINT(x%, y%) = 0 THEN PSET (x%, y%), 7 NEXT x% ELSE FOR x% = 0 TO 319 STEP 4 IF POINT(x%, y%) = 0 THEN PSET (x%, y%), 7 NEXT x% END IF NEXT y% LOCATE 1, 1 DO: LOOP WHILE INKEY$ = "" FloodFill 159, 99, 15 LOCATE 12, 13 PRINT " " LOCATE 13, 13 PRINT " Satisfied??? " LOCATE 14, 13 PRINT " " REM $STATIC SUB FloodFill (x%, y%, col%) DIM pixdata(1278) AS pixtype bcol% = POINT(x%, y%) firstentry% = 0 lastentry% = 1 DO fx% = pixdata(firstentry%).x fy% = pixdata(firstentry%).y DO IF POINT(x% + fx%, y% + fy%) = bcol% THEN PSET (x% + fx%, y% + fy%), col% IF POINT(x% + fx%, y% + fy% - 1) = bcol% THEN pixdata(lastentry%).x = fx% pixdata(lastentry%).y = fy% - 1 lastentry% = lastentry% + 1 IF lastentry% = 1279 THEN lastentry% = 0 END IF IF POINT(x% + fx%, y% + fy% + 1) = bcol% THEN pixdata(lastentry%).x = fx% pixdata(lastentry%).y = fy% + 1 lastentry% = lastentry% + 1 IF lastentry% = 1279 THEN lastentry% = 0 END IF ELSE EXIT DO END IF fx% = fx% + 1 LOOP fx% = pixdata(firstentry%).x - 1 fy% = pixdata(firstentry%).y DO IF POINT(x% + fx%, y% + fy%) = bcol% THEN PSET (x% + fx%, y% + fy%), col% IF POINT(x% + fx%, y% + fy% - 1) = bcol% THEN pixdata(lastentry%).x = fx% pixdata(lastentry%).y = fy% - 1 lastentry% = lastentry% + 1 IF lastentry% = 1279 THEN lastentry% = 0 END IF IF POINT(x% + fx%, y% + fy% + 1) = bcol% THEN pixdata(lastentry%).x = fx% pixdata(lastentry%).y = fy% + 1 lastentry% = lastentry% + 1 IF lastentry% = 1279 THEN lastentry% = 0 END IF ELSE EXIT DO END IF fx% = fx% - 1 LOOP firstentry% = firstentry% + 1 IF firstentry% = 1279 THEN firstentry% = 0 LOOP UNTIL firstentry% = lastentry% END SUB