Advertisement
Vladislav_Bezruk

Untitled

Nov 29th, 2021
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. void floodFillScanlineStack(Paint::Bitmap^ bm, int x, int y, System::Drawing::Color newColor, System::Drawing::Color oldColor)
  2. {
  3.     if (oldColor == newColor) return;
  4.  
  5.     int x1;
  6.     bool spanAbove, spanBelow;
  7.  
  8.     std::vector<int> stackX, stackY;
  9.     stackX.push_back(x);
  10.     stackY.push_back(y);
  11.  
  12.     while (!stackX.empty())
  13.     {
  14.         x = stackX.back(); y = stackY.back();
  15.         stackX.pop_back(); stackY.pop_back();
  16.  
  17.         x1 = x;
  18.         while (x1 >= 0 && bm->GetPixel(x1, y) == oldColor) x1--;
  19.         x1++;
  20.         spanAbove = spanBelow = 0;
  21.         while (x1 < bm->Width && bm->GetPixel(x1, y) == oldColor)
  22.         {
  23.             bm->SetPixel(x1, y, newColor);
  24.  
  25.             if (!spanAbove && y > 0 && bm->GetPixel(x1, y - 1) == oldColor)
  26.             {
  27.                 stackX.push_back(x1);
  28.                 stackY.push_back(y - 1);
  29.  
  30.                 spanAbove = 1;
  31.             }
  32.             else if (spanAbove && y > 0 && bm->GetPixel(x1, y - 1) != oldColor)
  33.             {
  34.                 spanAbove = 0;
  35.             }
  36.             if (!spanBelow && y < bm->Height - 1 && bm->GetPixel(x1, y + 1) == oldColor)
  37.             {
  38.                 stackX.push_back(x1);
  39.                 stackY.push_back(y + 1);
  40.  
  41.                 spanBelow = 1;
  42.             }
  43.             else if (spanBelow && y < bm->Height - 1 && bm->GetPixel(x1, y + 1) != oldColor)
  44.             {
  45.                 spanBelow = 0;
  46.             }
  47.             x1++;
  48.         }
  49.     }
  50. }
  51.  
  52. void Paint::PaintForm::Fill(int x, int y, Color newClr, Color oldClr)
  53. {
  54.     floodFillScanlineStack(Img, x, y, newClr, oldClr);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement