Author Topic: Fixed-Memory Flood Fill Pseudocode  (Read 18116 times)

0 Members and 2 Guests are viewing this topic.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Fixed-Memory Flood Fill Pseudocode
« on: February 10, 2012, 02:56:04 am »
So, I was trying to write an assembly program to execute a fixed-memory flood fill routine as described by the "flood fill" Wikipedia article here, but I realized that the algorithm written there is poorly explained, and also missing a lot of crucial conditions.

After realizing this, I opened up Adobe Flash to try to write an implementation so that I could easily see what was going on at all times, and could therefore debug it a lot more easily.

After hours of debugging and modifications, I finally was able to write an algorithm that actually worked in all situations. Here is the pseudocode I have written out for it:

Code: [Select]
RULE is "right"
FINDLOOP is false
BOTTLENECK is false

CUR is the current pixel
MARK is the first mark, and is not set
MARK2 is the second mark, and is not set

main loop:
if CUR is bound by 4 edge pixels
paint CUR
all possible pixels have been filled, so exit the routine
 
if CUR is bound by 3 edge pixels
paint CUR
move based on RULE
continue main loop

if CUR is bound by 2 edge pixels
if the two bounding edge pixels are not across from each other, and the corner opposite both edge pixels is not filled
paint CUR
move according to RULE
continue main loop
else
if RULE is "right" and  MARK is not set
set MARK to CUR's location and direction
set FINDLOOP to false
else, if MARK is set
if MARK2 is not set
if CUR is at MARK's location
if CUR's direction is the same as MARK's
remove MARK
set RULE to "right"
else
set RULE to "left"
set CUR to MARK's direction
set FINDLOOP to false
else, if FINDLOOP is true
set MARK2 to CUR's location and direction
else
if CUR is at MARK's location
set CUR to MARK2's direction and location
remove MARK
remove MARK2
set RULE to "right"
else if CUR is at MARK2's location
set MARK to MARK2's direction and location
set CUR's direction to MARK2's direction
remove MARK2
if RULE is "right" and MARK is not set
if BOTTLENECK is false
paint CUR
set BOTTLENECK to false
move according to RULE
continue main loop

if CUR is bound by 1 edge pixel
if RULE is "left" and FINDLOOP is false
set FINDLOOP to true
else, if neither  of the corners opposite the bounding pixel are filled
paint CUR
move according to RULE
continue main loop

if CUR is bound by 0 edge pixels
if RULE is "left" and FINDLOOP is false
set FINDLOOP to true
set BOTTLENECK to false
move any direction (use a default direction, not a random one)
continue main loop


##############################
how to move according to RULE:
if CUR is filled or the rule-side-corner is filled
move CUR one step forward
else
if MARK is at the pixel one step forward
set BOTTLENECK to true
else, if MARK2 is at the pixel one step forward
set MARK to MARK2's direction and location
set CUR's direction to MARK2's direction
remove MARK2
if RULE is "left" and FINDLOOP is false
if either the pixel two steps forward is filled or the non-rule-side pixel is filled
set FINDLOOP to true
move CUR one step forward
move CUR one step to the RULE side



##############################
every time you paint:
remove MARK, if set
remove MARK2, if set
set FINDLOOP to false
set BOTTLENECK to false
set RULE to "right"

I'd like to put this into the Wikipedia article so that people can have a much better example to base their code on. Is there any way I should modify this pseudocode to make it look better?

I am also going to create some images explaining what some things are (like what an "open corner" is, as opposed to a "closed corner").

Also, if someone wants to write an ASM implementation of this, please go ahead. I bet your implementation would be better than mine >.<
« Last Edit: February 10, 2012, 05:18:02 am by ZippyDee »
There's something about Tuesday...


Pushpins 'n' stuff...


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #1 on: February 10, 2012, 03:20:50 am »
This seems interesting. When you get an ASM or other language version done, it would be nice to see it in action. :)
« Last Edit: February 10, 2012, 03:21:17 am by DJ_O »

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #2 on: February 10, 2012, 03:45:22 am »
Here's a link to the flash file I made to debug this algorithm.

http://megaswf.com/file/2130949

The red arrow is the curent location, the blue filled arrow is the first marker, and the blue filed arrow with a green outline is the second marker used for removing unwanted loops.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #3 on: February 10, 2012, 03:45:24 am »
To be consistent, each case in the main loop should probably end with continue main loop. Also, I don't see any code that sets MARK2.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #4 on: February 10, 2012, 03:48:55 am »
To be consistent, each case in the main loop should probably end with continue main loop. Also, I don't see any code that sets MARK2.
Oops! I accidentally put MARK instead of MARK2 at the place it should be set. Fixed that now. And I added the "continue main loop" as well.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #5 on: February 10, 2012, 03:58:19 am »
Interesting, thanks for the link. I am curious if in some occasions it can loop forever, though? Because once it seemed to go through the same path 5 times until it finally started filling more stuff, and as an ASM program, if it can end up into an endless loop, then that means a battery pull/RAM Clear :P

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #6 on: February 10, 2012, 03:58:54 am »
Hmm, maybe I should rewrite my JavaScript minesweeper to use a better algorithm.
I just made mine form scratch, so its probably not efficient.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #7 on: February 10, 2012, 04:00:01 am »
Interesting, thanks for the link. I am curious if in some occasions it can loop forever, though? Because once it seemed to go through the same path 5 times until it finally started filling more stuff, and as an ASM program, if it can end up into an endless loop, then that means a battery pull/RAM Clear :P
No, it will not loop forever. Each time it went through the path, it changed something. It either filled at least one pixel, or it moved the marker to a new location.

Hmm, maybe I should rewrite my JavaScript minesweeper to use a better algorithm.
I just made mine form scratch, so its probably not efficient.
For javascript, you shouldn't have to worry about stack or memory issues, so a simple recursive flood-fill would be best. This one is much too slow to be worthwhile for any system that can handle recursive/stack-based flood-fills.
« Last Edit: February 10, 2012, 04:02:03 am by ZippyDee »
There's something about Tuesday...


Pushpins 'n' stuff...


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #8 on: February 10, 2012, 04:03:13 am »
Ah maybe I didn't notice, since it was going on pretty fast.

Also I'm curious how fast this would be in z80 ASM, considering you don't have to update the LCD every frame but only at the end.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #9 on: February 10, 2012, 04:07:29 am »
Ah maybe I didn't notice, since it was going on pretty fast.

Also I'm curious how fast this would be in z80 ASM, considering you don't have to update the LCD every frame but only at the end.
Yeah, I'm wondering that, too. Seeing as it's such an obnoxiously slow routine, a mostly-speed-optimized ASM version would probably be best. I personally don't feel like I'd be able to make a very good routine in ASM. I'd rather that someone else give it a go...

Maybe someone could even write an Axe implementation just so something exists to base some of the ASM off of. This pseudocode is written in structured English, so it's not really written for easy translation directly into ASM.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #10 on: February 10, 2012, 04:10:41 am »
Yeah I agree that translating high level language stuff into such low level language would be a huge challenge. This is why there are so few BASIC games that got converted straight to ASM.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #11 on: February 10, 2012, 04:58:48 am »
For javascript, you shouldn't have to worry about stack or memory issues, so a simple recursive flood-fill would be best. This one is much too slow to be worthwhile for any system that can handle recursive/stack-based flood-fills.
Ah, mine is recursive. Thanks for the information anyway :)

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #12 on: February 13, 2012, 05:24:05 am »
I am working on converting this algorithm to z80 assembly, so I thought I would post some screenshots.

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #13 on: February 13, 2012, 12:31:44 pm »
How fast is this when it only draws to the screen when it finishes rather than plot every pixel individually?

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #14 on: February 13, 2012, 01:15:39 pm »
That looks awesome, jacobly!

You know, if the finished version is still quite slow, it may be good to periodically draw to the screen as it fills rather than only at the end, just so people know it's still working and hasn't totally crashed or something.

How fast is this when it only draws to the screen when it finishes rather than plot every pixel individually?
Well, the algorithm itself is going to be slow. There's no way around that. It has to do a lot of tracing of boundaries in order to validate the pixels for drawing. The only question is about how slow it will be. This floodfill will certainly not be anything you'd want to use for dynamic drawing in games or anything. I'd say it would be used solely in on-calc graphics programs. Yes, it will be slow, but the trade off is worth it to have a floodfill that doesn't threaten a stack overflow no matter how large or complex of a shape you are filling.
There's something about Tuesday...


Pushpins 'n' stuff...