Log In  

When I started working in PICO, I was aware of a command called FILLP() and thought surely that was the method for painting on a screen to fill in polygonal shapes or other areas.

Now I am understanding, no, there -IS- no fill routine or PAINT as it might be called in BASIC for PICO.

FILLP() just creates a fill pattern for the standard draw routines.

Hands down, was there some competition or other where the fastest paint routine with the smallest code ever - was there one for PICO ?

And if so who did it and what was their code and method of achievement ? Or do you know of one yourself ?

P#55792 2018-08-27 20:38 ( Edited 2018-10-13 11:21)

Hey, my first serious RECURSION code, works pretty good.

-- good fill routine
function main()

cls()
rect(0,0,127,127)
circ(44,44,32)
circ(45,44,32)
circ(84,84,32)
circ(85,84,32)

watchspeed=0
--watchspeed=32 -- see actions
fill(1,1,13)

end

function fill(x,y,c,b)
  if (b==null) b=pget(x,y)
  if pget(x,y)==b then
    pset(x,y,c)
    for i=y-1,y+1 do
      for j=x-1,x+1 do
        if (pget(j,i)==b) fill(j,i,c,b)
      end
      if (watchspeed>0 and (x+y)%watchspeed==0) flip()
    end
  end
end

main()

Still ... I wonder if there's a faster or better fill method than this.
Likely this code would be useful for others too.

P#55793 2018-08-27 21:17 ( Edited 2018-08-28 01:38)

Recursion sure is a cool thing, but yep, just found out that I can use up all the stack in PICO quite easily with it !

Here is the routine listed for QBasic, standard with the language:

https://www.qbasic.net/en/reference/qb11/Statement/PAINT.htm

Despite it being available for the language I was programming in back then, I wanted to write my own, just for the challenge of it.

Here is the PAINT I wrote for QBasic years ago, converted to PICO. It's supposed to be pretty slow but seems to be going a nice clip in here:

-- good fill routine
function main()

cls()
rect(0,0,127,127) -- necessary!
circ(44,44,32)
circ(45,44,32)
circ(84,84,32)
circ(85,84,32)

watchspeed=0
--watchspeed=32 -- see actions
slowfill(1,1,13)

end

function slowfill(x,y,c)
local ok,p=0,pget(x,y)
  pset(x,y,c)
  repeat
    ok=1
    for i=0,127 do
      for j=0,127 do
        if pget(j,i)==c then
          for k=i-1,i+1 do
            for l=j-1,j+1 do
              if (pget(l,k)==p) pset(l,k,c) ok=0
            end
          end
        end
        if (watchspeed>0 and (j+i)%watchspeed==0) flip()
      end
    end  
  until ok==1
end

function fill(x,y,c,b)
  if (b==null) b=pget(x,y)
  if pget(x,y)==b then
    pset(x,y,c)
    for i=y-1,y+1 do
      for j=x-1,x+1 do
        if (pget(j,i)==b) fill(j,i,c,b)
      end
      if (watchspeed>0 and (x+y)%watchspeed==0) flip()
    end
  end
end

main()

It's interesting that I can't seem to make a fast fill that would dodge diagonal connections. As you see the two circles are double-printed to ensure the paint does not get in.

Found out how to do it. It cuts down the fill routine speed by half, but at least you don't have dangling diagonals. Also this fill routine DOES get slow with a lot of walls in the way. Resorting back to my recursion method which is quite speedy.

P#55794 2018-08-27 21:47 ( Edited 2018-08-28 03:45)

The term you are looking for is "flood fill". The scanline flood fill is the typical "fast enough and correct" way of doing it.

Here is a repository that has Lua implementations of flood fills. Note that it's standard Lua 5.x, not PICO-8's Lua, so it still has to be ported.

I would also suggest looking at some of the available 3D engines for PICO which contain optimized triangle rasterizers. This code is mostly interesting if you specifically need fast shape drawing that is not rectangles or circles, but the actual drawing will use tricks to plot more than one pixel at a time.

P#55905 2018-08-30 20:55 ( Edited 2018-08-31 00:55)

I appreciate the comment, TF, but I'm well satisfied with the recursion one I wrote. Fastest fill I've ever seen. :)

And no, I cannot guarantee the shapes will be triangles or circles - so, it must be done a pixel at a time.

Looking at the code specifically, "Flood Scanline," I do not see it any improvement over what I wrote. If anything it would be slower since it only draws horizontal lines (which must be scanned first) instead of a straight outward "virus" filling method.

In any case, the only other reason I would use my fill is for light displacement, a tutorial I hadn't written yet, or an image vector generator. Really not needed since PICO's resolution is so small - but it might appeal to some who want to make vector-based text-parser adventure games.

P#55913 2018-08-30 21:44 ( Edited 2018-08-31 01:44)

shapes that aren't circles or round are made of triangles

a rectangle is 2 triangles put together

P#56074 2018-09-02 17:31 ( Edited 2018-09-02 21:31)

Not all shapes would work with just triangles. Consider the complexity of rounded edges in addition to sharp ones.

[16x16]

P#56088 2018-09-02 23:02 ( Edited 2018-09-03 03:02)

Good discussion!

A curve is just a bunch of infinitely small triangles.; - )

P#57907 2018-10-13 07:21 ( Edited 2018-10-13 11:21)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 04:42:12 | 0.009s | Q:19