洪水填充算法

编程入门 行业动态 更新时间:2024-10-27 23:30:51
本文介绍了洪水填充算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

IM工作在一个简单的图形库下与涡轮C ++,因为即时通讯开发一个油漆样式的程序非常原始的版本,everyting效果很好,但我不能让洪水填充算法的工作。使用IM的4路洪水填充算法,首先我试着用递归版本,但它只有很小的地区工作,馅大面积使它崩溃;看完我发现,实现的是一个明确的堆栈版本解决这个问题,但我不真的看到了。

im working in a simple graphical library in C with turbo C++ because im developing a very primitive version of a paint style program, everyting works well but i can´t get the flood fill algorithm to work. Im using the 4 way flood fill algorithm, first i tried with the recursive version but it only work with small areas, filling large areas make it crash; reading i found that implement an explicit stack version of it solve the problem but i don´t really see it.

我公司开发的协议栈是这样的:

I have developed a stack like this:

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 }

这是codeI已使洪水填充功能:

And this is the code i have make to the flood fill function:

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; } }

但它仍然鸵鸟政策工作时,即时通讯triying填补大面积的它只是停下来,因为即时通讯与真正的640 X 480在我的计划that's解决一个问题的工作;任何想法,为什么它不工作?

But it still don´t work, when im triying to fill large areas it just stop and because im working with resolution 640 X 480 in my program that´s really a problem; any ideas why it dont work?

推荐答案

而不是在堆栈上推每一个像素,尽量在堆栈上推一个新位置之前水平,填补尽可能多的像素作为可能的。请参阅维基百科的文章的讨论。

Instead of pushing every pixel on the stack, try to fill as many pixels as possible horizontally before pushing a new position on the stack. See the Wikipedia article for a discussion.

更多推荐

洪水填充算法

本文发布于:2023-11-09 19:27:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:洪水   算法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!