質問

im作業の簡単なグラフィカル図書館のCターボC++でim開発は非常に原始的なバージョン塗料スタイルプログラム、あらゆ作品がっ洪水の記のアルゴリズムです。Imの4方洪水を埋アルゴリズム、また、再帰版でのみの小さい領域を充填し、大きな地域でクラッシュ;読書の見を実装する明確なスタックで問題が少し苦手なってしまいます。

の開発を行っているスタックのようになります:

struct node
{
    int x, y;
    struct node *next;
};

int push(struct node **top, int x, int y)
{
    struct node *newNode;
    newNode = (struct node *)malloc(sizeof(struct node));
    if(newNode == NULL) //If there is no more memory
        return 0;
    newNode->x = x;
    newNode->y = y;
    newNode->next = *top;
    *top = newNode;
    return 1; //If we push the element correctly
}

int pop(struct node **top, int &x, int &y)
{
    if(*top == NULL) //If the stack is empty
        return 0;
    struct node *temporal;
    temporal = *top;
    x = (*top)->x;
    y = (*top)->y;
    *top = (*top)->next;
    free(temporal);
    return 1; //If we pop an element 
}

このコードをってについて意見表明するための洪水を埋機能:

void floodFill(int x, int y, int color_to_replace, int color_to_fill)
{
    if(color_to_replace == color_to_fill)
  return;
 struct node *stack = NULL;
 if(push(&stack, x, y) == 0) //If we can´t push the pixel
            return;
    while(pop(&stack, x, y) == 1) //While are pixels in the stack
    {
        pixel(x, y, color_to_fill);
        if(x+1 < 640 && read_pixel(x+1, y) == color_to_replace)
            if(push(&stack, x+1, y) == 0)
                return;
        if(x-1 >= 0 && read_pixel(x-1, y) == color_to_replace)
            if(push(&stack, x-1, y) == 0)
                return;
        if(y+1 < 480 && read_pixel(x, y+1) == color_to_replace)
            if(push(&stack, x, y+1) == 0)
                return;
        if(y-1 >= 0 && read_pixel(x, y-1) == color_to_replace)
            if(push(&stack, x, y-1) == 0)
                return;
    }
}

もんきim triying入には大きな地域で停止でimと解像度640X480私のプログラムょっ問題そのアイデアはなぜでん?

役に立ちましたか?

解決

の代わりにスタック上のすべてのピクセルを押す、水平スタック上の新しい位置をプッシュする前に、できるだけ多くのピクセルを埋めるためにしてみてください。 href="http://en.wikipedia.org/wiki/Flood_fill"は、議論のためのWikipediaの記事をrel="noreferrer">

他のヒント

いと思われboundscheckingでもどこでも---

まずは、XとYの値なのか。

編集:

追加アイデアはなぜことができていなかった作品:

  • の読み取り/書き込みのピクセルの機能のバグ
  • 色の値が返されますがスケールまでの32ビット(例えば写真は16ビットの色にしようとしていを書く、読むも一致しなくなります。(例:書きのカラー:0xff00ffが返されます:0xf800f8ので色んスケールからの16ビット)は、この洪水の入っ永遠に存続していくことでした。
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top