Post by johnno56 on Apr 17, 2021 19:27:34 GMT -6
I wasn't sure whether to use "Tips and Tricks" or "Projects" for a small challenge.
If you can set you way-back machines to the mid to late 80's, either recall or have read about, flood filling shapes. Before you say, "But we use Polygon() to fill shapes", I thought it might fun to try and modify some ancient code and see if we can increase the speed of the process. Speed. Not really the right word when it comes to ancient fill routines. I can still remember watching an object fill and it was SO slow. The results were impressive but the wait wasn't...
I was looking for a non-cursive fill routine that I could convert to Basic. Quick note: There were plenty of recursive routines. Recursive. From my understanding: A routine is executed to fill a shape and the same routine is executed within the routine. This increases the speed but uses a LOT of memory to keep track of all the pixels within the shape. Non-recursive will start from a fixed point and will draw a pixel in one direction while the current pixel is empty or the next pixel in line is not the boundary of the shape. If the boundary is reached, the drawing goes back to the point of origin and draws in the opposite direction. When both lines are complete, shift the either the X or Y axis origin point by one pixel and repeat process. It's known as Boundary or Scanline fill.
I will attach the program that I converted. In this case a circle is the object. I think this method only works with 'simple' shapes. (circles, squares and triangles etc....
Give it a try and make whatever changes you can to make it run faster....
The 'updates' were included to simulate the drawing speed....
Have fun
If you can set you way-back machines to the mid to late 80's, either recall or have read about, flood filling shapes. Before you say, "But we use Polygon() to fill shapes", I thought it might fun to try and modify some ancient code and see if we can increase the speed of the process. Speed. Not really the right word when it comes to ancient fill routines. I can still remember watching an object fill and it was SO slow. The results were impressive but the wait wasn't...
I was looking for a non-cursive fill routine that I could convert to Basic. Quick note: There were plenty of recursive routines. Recursive. From my understanding: A routine is executed to fill a shape and the same routine is executed within the routine. This increases the speed but uses a LOT of memory to keep track of all the pixels within the shape. Non-recursive will start from a fixed point and will draw a pixel in one direction while the current pixel is empty or the next pixel in line is not the boundary of the shape. If the boundary is reached, the drawing goes back to the point of origin and draws in the opposite direction. When both lines are complete, shift the either the X or Y axis origin point by one pixel and repeat process. It's known as Boundary or Scanline fill.
I will attach the program that I converted. In this case a circle is the object. I think this method only works with 'simple' shapes. (circles, squares and triangles etc....
xmax = 640
ymax = 480
title$ = "Boundary Fill - Maybe..."
WindowOpen(1,title$,0,0,xmax,ymax,0)
CanvasOpen(1,xmax,ymax,0,0,xmax,ymax,0)
ClearCanvas
white = rgb(255,255,255)
red = rgb(255,0,0)
blue = rgb(0, 0, 191)
'=========================================
sub boundary(x, y, fc, bc)
xmove = x
ymove = y
while getPixel(xmove, ymove) <> bc
' Fill upwards
while getPixel(xmove, ymove) <> bc
setColor(fc)
pset(xmove, ymove)
ymove = ymove - 1
wend
ymove = y
' Fill downwards
while getPixel(xmove, ymove) <> bc
setColor(fc)
pset(xmove, ymove)
ymove = ymove + 1
wend
ymove = y
xmove = xmove - 1
update()
wend
xmove = x
while getpixel(xmove, ymove) <> bc
' Fill upwards
while getpixel(xmove, ymove) <> bc
setColor(fc)
pset(xmove, ymove)
ymove = ymove - 1
wend
ymove = y
' Fill downwards
while getpixel(xmove, ymove) <> bc
setColor(fc)
pset(xmove, ymove)
ymove = ymove + 1
wend
ymove = y
xmove = xmove + 1
update()
wend
update()
end sub
'=========================================
setColor(white)
circle(320, 240, 150)
update()
boundary(320, 240, blue, white)
waitkey
Give it a try and make whatever changes you can to make it run faster....
The 'updates' were included to simulate the drawing speed....
Have fun