LoginSignup
1
1

More than 5 years have passed since last update.

画像の領域塗りつぶし

Posted at

いきなり必要になった画像の領域塗りつぶしのアルゴリズム。

stack じゃなくて queue でも良いはず。
最初再帰で処理した名残。再帰で処理すると stack over flow で死んだ。

void fill(int x, int y) {
  struct POINT {
    int x;
    int y;
  };

  std::stack<POINT> stack;
  stack.push(POINT({x, y}));

  while (stack.empty() == false) {
    auto point = stack.top();
    stack.pop();

    // すでに塗りつぶされている点であればスキップ
    if (isAlreadyProecssed(point)) {
      continue;
    }

    // 塗りつぶしが必要か判定
    if (needProcess(point) == false) {
      continue;
    }

    // 塗りつぶし
    process(point);

    stack.push(POINT({point.x - 1, point.y}));
    stack.push(POINT({point.x + 1, point.y}));
    stack.push(POINT({point.x, point.y - 1}));
    stack.push(POINT({point.x, point.y + 1}));
  }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1