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

0 Members and 1 Guest are viewing this topic.

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #15 on: February 13, 2012, 04:01:48 pm »
In those screenshots, the screen is being updated 4-5 times per frame, so the program is spending most of the time updating the screen. Also, I haven't even started optimizing the code, and I have some ideas for how to optimize the algorithm. In fact, filling a blank screen from the center without updating the screen currently takes only 10 seconds (That's 24x faster than the screenshot ;D).

Edit: Updating every 256'th frame looks like this:
« Last Edit: February 13, 2012, 04:15:04 pm by jacobly »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #16 on: February 13, 2012, 04:14:34 pm »
Hmm, so your flood fill algorithm just traces around the edge of the region until it is all filled? Nice...
I am curious, will it work properly if you have a setup like an hourglass but at the middle (where it is narrow) you have 1 empty pixel with a black pixel on either side?

EDIT: Ah, I see the screenies, now >.>

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #17 on: February 13, 2012, 05:19:06 pm »
This is a very very clever algorithm!  Kudos to its creation, it sounds like a powerful utility for anybody needing a flood fill with no memory overhead associated with it.
« Last Edit: February 13, 2012, 06:04:13 pm by Builderboy »

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #18 on: February 13, 2012, 07:24:39 pm »
Hmm, so your flood fill algorithm just traces around the edge of the region until it is all filled? Nice...
I am curious, will it work properly if you have a setup like an hourglass but at the middle (where it is narrow) you have 1 empty pixel with a black pixel on either side?

EDIT: Ah, I see the screenies, now >.>

Yeah, the algorithm traces the edges and only fills in a pixel if it is positive that filling that pixel will not block off another area to fill. If it is unsure, it places a markat that "questionable" pixel indicating the direction it was heading and continues to trace the edges until it comes back to the mark.

If it reaches the mark and the mark is pointing in the direction that it was already heading, then it knows filling that pixel will still allow it to get to the other side.

However, if it reaches the mark and the mark is pointing in a different direction, then there must be some sort of a loop that turns it back onto itself, meaning it wouldn't be able to reach the other side if it filled the pixel.

To eliminate this loop, it turns around and starts backtracking around the loop. Once it reaches another "questionable" pixel in that loop, a second mark is placed on that pixel. It keeps backtracking until it runs into one of the two marks.

If it runs into the second mark before the first mark, it knows the place where it gets turned around on itself must be further into the loop, so it turns around and backtracks again, finding the NEXT "questionable" pixel.

But if it reaches the first mark before the second it knows that filling the pixel at the second mark will get rid of the loop without blocking off the rest of the map. So it fills in the pixel at the second mark, removes both marks, and continues as usual from where the second mark was.


Hope that explanation helps clarify how the algorithm works. It's certainly easier than trying to interpret that pseudocode.


Edit:
In those screenshots, the screen is being updated 4-5 times per frame, so the program is spending most of the time updating the screen. Also, I haven't even started optimizing the code, and I have some ideas for how to optimize the algorithm. In fact, filling a blank screen from the center without updating the screen currently takes only 10 seconds (That's 24x faster than the screenshot ;D).

Edit: Updating every 256'th frame looks like this:
What about updating every Xth time it paints, not every Xth frame? That might go even faster.
« Last Edit: February 13, 2012, 07:28:13 pm 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 #19 on: February 14, 2012, 10:39:54 pm »
Wow that's fast Jacobly. To think in Paint Shop Pro it took two seconds to fill an entire 200x200 image on a Pentium 100 MHz...

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #20 on: February 16, 2012, 02:20:57 pm »
So, uh, I came up with my own algorithm and implemented it in Grammer code. Needless to say, even when updating every frame, it was about as fast as Jacobly's. My calc has crashed so it'll take me a bit to reprogram it, but I wrote down the pseudo code. Before I post it, I want to double check on a few things:
It uses 2 fixed buffers (the graph screen and another 768-byte buffer)
It does not trace around the edge like your code

The code is very simple and you will see why it is fast. I didn't know if using another buffer was okay or not for your implementation.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #21 on: February 16, 2012, 02:43:38 pm »
If you are using another 768 byte buffer, you might as well use a queue method at that point XD

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #22 on: February 16, 2012, 02:46:37 pm »
If you ever wanted to add grayscale support then you'd need another buffer, meaning 3 buffers...that's a lot of memory to keep track of... The whole point is that this algorithm uses no extra memory other than what is in the routine itself.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #23 on: February 16, 2012, 03:00:40 pm »
Oh, that is cool o.o I am still not seeing how it can mark paths though (or is it one at a time?)

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #24 on: February 16, 2012, 03:01:25 pm »
only one mark is ever placed at a time. yeah.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #25 on: February 16, 2012, 03:02:30 pm »
Wow, very nice O.O That is indeed an excellent algorithm, then O.O

Offline MGOS

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +95/-0
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #26 on: April 03, 2012, 11:22:22 am »
I'm not sure if this counts to necroposting, but because I need something like that atm I'm now curious to see Xeda's algorithm.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #27 on: April 03, 2012, 02:30:25 pm »
Hmm, I actually used my algorithm in my TI-Concours entry :) How it works is this:

Setup: I clear a buffer the size of the screen (in Axe, I used L3, originally)

1) Start going in one direction until you hit a barrier, turning pixels ON
     While doing this, check either side of the pixel. If either side is OFF, plot the pixel on the other buffer as well.
2) When you hit a barrier, if you have no other directions, search the buffer for an ON pixel. If your draw buffer has 1 or 0 directions it can go in, pixel-off the back buffer. If it was zero directions, keep searching. If it was one or more, start drawing from there

So, it uses Fixed-Memory, but instead of no extra memory required, it uses a 768-byte buffer as a scrap buffer.

EDIT: I removed a bunch of stuff from my TI-Concour entry so that it just had the flood fill, so I added a screenie. I have a button ([VARS]) that when you are holding it, it will update the LCD to show you what it is doing, so that is why sometimes you see it filling in my screenie and sometimes you don't :)

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 #28 on: April 03, 2012, 02:59:05 pm »
Looks nice Xeda. I think someone should do a drawing program with that kind of algorithm. :)

Offline MGOS

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +95/-0
    • View Profile
Re: Fixed-Memory Flood Fill Pseudocode
« Reply #29 on: April 03, 2012, 04:19:26 pm »
Wow, Xeda, that's awesome! Thanks, that helped me a lot.  :)