im工作在一个简单的图形图书馆在C涡轮增压C++是因为我的发展中一个非常原始版本的油漆风格的程序,任何东西运作良好但我不能获得的洪水填写算法的工作。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;
    }
}

但它仍然不工作,当我访,以填补大的地区,它刚刚停止,因为口工作的决议640X480在我的程序是一个真正的问题;任何想法为什么它不工作吗?

有帮助吗?

解决方案

相反推堆栈上的每个像素的,尝试水平推栈上的新位置之前填充尽可能多的像素作为可能的。请参阅维基百科文章的讨论。

其他提示

我没有看到任何boundschecking任何地方...

你肯定是X和Y值不出去的图片?

编辑:

额外的想法为什么它不能工作:

  • 读和写素的功能有一个错误
  • 色彩的价值,你回来是扩大到32位(因为,例如你的照片是16位)和颜色你想写读后再返回将不会完全匹配。(例如你写的颜色:0xff00ff但是你得到回:0xf800f8因为颜色得到了扩大,从16位),这将导致洪水填补会在永远。
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top