Better floodfill

This commit is contained in:
visionmercer 2026-05-04 09:01:12 +02:00
commit 8037406b33
2 changed files with 62 additions and 99 deletions

View file

@ -145,99 +145,62 @@ sub filledcircle(x,y,r,col as long)
wend wend
end sub end sub
SUB FloodFill (startX, startY, fillColor~&)
sub floodfill (x as integer, y as integer, fillcolor as long) ' We use a simple array as a stack for (x, y) pairs
dim as integer leftx, rightx, currenty ' For large images, you may need to increase this size
dim as integer arrsize, stackptr DIM stackX(2000) AS INTEGER
DIM stackY(2000) AS INTEGER
' Create a stack for storing coordinates targetColor~&=point(startX,startY)
arrsize = 1000 ' Adjust based on expected complexity stackPtr = 1
redim stack(1 to arrsize, 1 to 2) as integer
stackptr = 0 stackX(stackPtr) = startX
stackY(stackPtr) = startY
' Get the color at starting point
dim startcolor as long WHILE stackPtr > 0
startcolor = point(x, y) curX = stackX(stackPtr)
curY = stackY(stackPtr)
' If starting on boundary color or fill color, exit stackPtr = stackPtr - 1
if startcolor = fillcolor then exit sub
' Move to the left edge of the span
' Push starting point to stack x = curX
stackptr = stackptr + 1 WHILE POINT(x, curY) = targetColor~& AND x >= 0
stack(stackptr, 1) = x x = x - 1
stack(stackptr, 2) = y WEND
x = x + 1
' Process until stack is empty
do while stackptr > 0 spanAbove = 0
' Pop a point from stack spanBelow = 0
x = stack(stackptr, 1)
y = stack(stackptr, 2) ' Process the span moving right
stackptr = stackptr - 1 WHILE POINT(x, curY) = targetColor~& AND x < _WIDTH
PSET (x, curY), fillColor~&
' Skip if current point is not startcolor
if point(x, y) <> startcolor then _continue ' Check row above
IF curY > 0 THEN
' Find leftmost point on this scanline IF spanAbove = 0 AND POINT(x, curY - 1) = targetColor~& THEN
leftx = x stackPtr = stackPtr + 1
do while leftx > 0 and point(leftx - 1, y) = startcolor stackX(stackPtr) = x
leftx = leftx - 1 stackY(stackPtr) = curY - 1
loop spanAbove = 1
ELSEIF spanAbove = 1 AND POINT(x, curY - 1) <> targetColor~& THEN
' Find rightmost point on this scanline spanAbove = 0
rightx = x END IF
do while rightx < _width - 1 and point(rightx + 1, y) = startcolor END IF
rightx = rightx + 1
loop ' Check row below
IF curY < _HEIGHT - 1 THEN
' Fill the scanline IF spanBelow = 0 AND POINT(x, curY + 1) = targetColor~& THEN
line (leftx, y)-(rightx, y), fillcolor stackPtr = stackPtr + 1
stackX(stackPtr) = x
' Check scanline above stackY(stackPtr) = curY + 1
currenty = y - 1 spanBelow = 1
if currenty >= 0 then ELSEIF spanBelow = 1 AND POINT(x, curY + 1) <> targetColor~& THEN
for x = leftx to rightx spanBelow = 0
if point(x, currenty) = startcolor then END IF
' Push seed point to stack END IF
stackptr = stackptr + 1
if stackptr > arrsize then x = x + 1
' Resize stack if needed WEND
arrsize = arrsize * 2 WEND
redim _preserve stack(1 to arrsize, 1 to 2) as integer END SUB
end if
stack(stackptr, 1) = x
stack(stackptr, 2) = currenty
' Skip adjacent pixels of same color
do while x <= rightx and point(x, currenty) = startcolor
x = x + 1
loop
x = x - 1 ' Adjust for loop increment
end if
next x
end if
' Check scanline below
currenty = y + 1
if currenty < _height then
for x = leftx to rightx
if point(x, currenty) = startcolor then
' Push seed point to stack
stackptr = stackptr + 1
if stackptr > arrsize then
' Resize stack if needed
arrsize = arrsize * 2
redim _preserve stack(1 to arrsize, 1 to 2) as integer
end if
stack(stackptr, 1) = x
stack(stackptr, 2) = currenty
' Skip adjacent pixels of same color
do while x <= rightx and point(x, currenty) = startcolor
x = x + 1
loop
x = x - 1 ' Adjust for loop increment
end if
next x
end if
loop
end sub

View file

@ -180,8 +180,8 @@ sub canvas
if _mousex > boxX1 then if _mousex > boxX1 then
' Start Drawing Logic ' Start Drawing Logic
if state.tool = 9 and (mouseclicked or rmouseclicked) then if state.tool = 9 and (mouseclicked or rmouseclicked) then
_dest layers(2).ihandle _dest layers(1).ihandle
_source layers(2).ihandle _source layers(1).ihandle
if mouseclicked then if mouseclicked then
floodfill canX,canY,state.fcolor floodfill canX,canY,state.fcolor
else else
@ -230,7 +230,7 @@ sub canvas
thickline state.startX, canY, state.startX, state.startY, drawCol thickline state.startX, canY, state.startX, state.startY, drawCol
case 6 ' Filled Rect case 6 ' Filled Rect
line (state.startX, state.startY)-(canX, canY), drawCol, bf line (state.startX, state.startY)-(canX, canY), drawCol, bf
case 7, 8 ' Polygons[cite: 2] case 7, 8 ' Polygons
if pointCount > 0 then if pointCount > 0 then
for p = 1 to pointCount - 1 for p = 1 to pointCount - 1
thickline polypoints((p - 1) * 2), polypoints((p - 1) * 2 + 1), polypoints(p * 2), polypoints(p * 2 + 1), drawCol thickline polypoints((p - 1) * 2), polypoints((p - 1) * 2 + 1), polypoints(p * 2), polypoints(p * 2 + 1), drawCol