I implemented iterative floodfill algorithm, but the problem is that the algorithm is checking pixels which was already visited by my while loop and pushed on Stack. My question is how can I check if the pixel was on the stack or what can i do to make him much faster?
void FloodfillStack(int x, int y, BITMAP *ekran,ALLEGRO_COLOR newColor, ALLEGRO_COLOR startPixelColor){
if (y < 0 || y > ScreenHeight || x < 0 || x > ScreenWidth)
return;
bool visited[ScreenHeight][ScreenWidth] = {false};
struct stackElement *top;
top = NULL;
push(&top, x,y);
visited[x][y] = true;
int newX, newY;
while (pop(&top, &newX, &newY))
{
visited[newX][newY] = true;
if ((y < 0 || y > ScreenHeight || x < 0 || x > ScreenWidth) || compareColors(al_get_pixel(ekran, newX,newY), startPixelColor) == false)
continue;
al_put_pixel(newX,newY, newColor);
if(visited[newX+1][newY] == false)
push(&top, newX+1,newY);
if(visited[newX-1][newY] == false)
push(&top, newX-1,newY);
if(visited[newX][newY+1] == false)
push(&top, newX,newY+1);
if(visited[newX][newY-1] == false)
push(&top, newX,newY-1);
}
}