## FloodFill Function

Discuss any general programming issues here
sinisrus
Posts: 269
Joined: Tue Apr 21, 2015 5:43 pm

### FloodFill Function

Hello,

I have problem for port of code C# FloodFill to Hollywood not work :-/

(Source: https://lodev.org/cgtutor/floodfill.html)

Origine Code:

Code: Select all

``````//The scanline floodfill algorithm using stack instead of recursion, more robust
void floodFillScanlineStack(int x, int y, int newColor, int oldColor)
{
if(oldColor == newColor) return;

int x1;
bool spanAbove, spanBelow;

std::vector<int> stack;
push(stack, x, y);
while(pop(stack, x, y))
{
x1 = x;
while(x1 >= 0 && screenBuffer[y * w + x1] == oldColor) x1--;
x1++;
spanAbove = spanBelow = 0;
while(x1 < w && screenBuffer[y * w + x1] == oldColor)
{
screenBuffer[y * w + x1] = newColor;
if(!spanAbove && y > 0 && screenBuffer[(y - 1) * w + x1] == oldColor)
{
push(stack, x1, y - 1);
spanAbove = 1;
}
else if(spanAbove && y > 0 && screenBuffer[(y - 1) * w + x1] != oldColor)
{
spanAbove = 0;
}
if(!spanBelow && y < h - 1 && screenBuffer[(y + 1) * w + x1] == oldColor)
{
push(stack, x1, y + 1);
spanBelow = 1;
}
else if(spanBelow && y < h - 1 && screenBuffer[(y + 1) * w + x1] != oldColor)
{
spanBelow = 0;
}
x1++;
}
}
}
``````
And my code hollywood (not work)

Code: Select all

``````Stack={}

Function Stack:Push(X,Y) ; Ajouter une valeur

Local Pos={X=X,Y=Y}

InsertItem(Self,Pos)
EndFunction

Function Stack:Pop() ; Supprimer une valeur
IF (ListItems(Self)=0) THEN Return("NULL")
Local Pos=RemoveItem(Self,ListItems(Self))
Return(Pos.X,Pos.Y)
EndFunction

Function Stack:Size() ; Taille de la pile
IF (ListItems(Self)=0) THEN Return("NULL")
Return(ListItems(Self))
EndFunction

Function Stack:Clear() ; Vider la pile
IF (ListItems(Self)<>0) THEN Self={}
Return(ListItems(Self))
EndFunction

/********************/

;The scanline floodfill algorithm using stack instead of recursion, more robust
Function floodFillScanlineStack(x, y, newColor)

oldcolor = HexStr(oldcolor) debugprint(oldcolor)

w=35
h=35

IF (ToNumber(oldColor) = ToNumber(newColor)) THEN RETURN()

Stack:Push(x,y)
WHILE(ToNumber(Stack:pop())<>"NULL")

x1 = x
WHILE((x1>=0) AND ReadBrushPixel(PICTURE,x1,y) = ToNumber(oldColor)) DO x1=x1-1
x1=x1+1

spanAbove, spanBelow = 0, 0
WHILE(x1 < w AND ReadBrushPixel(PICTURE,x1,y) = ToNumber(oldColor))
X,Y = Self:Pop()
WriteBrushPixel(PICTURE,x1,y,newColor)
IF(NOT spanAbove AND y > 0 AND ReadBrushPixel(PICTURE,x1,y-1) = ToNumber(oldColor))
Stack:Push(x1,y-1)
spanAbove = 1
ELSEIF(spanAbove AND y > 0 AND ReadBrushPixel(PICTURE,x1,y-1) <> ToNumber(oldColor))
spanAbove = 0
EndIF

IF(NOT spanBelow AND y < h - 1 AND ReadBrushPixel(PICTURE,x1,y+1) = ToNumber(oldColor))
Stack:Push(x1,y+1)
spanBelow = 1
ELSEIF(spanBelow AND y < h - 1 AND ReadBrushPixel(PICTURE,x1,y+1) <> ToNumber(oldColor))
spanBelow = 0
EndIF
x1=x1+1
WEND

WEND

EndFunction

``````

SamuraiCrow
Posts: 249
Joined: Fri May 15, 2015 5:15 pm