首页 \ 问答 \ 洪水填充算法(Flood fill algorithm)

洪水填充算法(Flood fill algorithm)

即时通讯工作在一个简单的图形库在C与涡轮C ++,因为即时通讯开发一个非常原始的版本的油漆风格程序,everyting工作得很好,但我不能让洪水填充算法工作。 我使用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;
    }
}

但它仍然没有工作,当我试图填补大面积停止,因为我在我的程序中使用640 X 480分辨率,这真的是一个问题; 任何想法,为什么它不工作?


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 
}

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

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?


原文:https://stackoverflow.com/questions/1879371
更新时间:2021-07-10 12:07

最满意答案

print(__FUNCTION__) // Swift
NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C

Swift 3及以上

print(#function)

print(__FUNCTION__) // Swift
NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C

Swift 3 and above

print(#function)

相关问答

更多
  • 一个静态变量决不是全局的。 一个静态变量具有文件范围和内部链接 ,因此包含该头文件的每个文件都将获得它自己的g_count 。 如果您想要一个全局变量,只需在一个实现文件中写入int g_count ,然后将extern int g_count放入一个标头中,使用该全局变量的其他文件将导入该标头。 A static variable is decidedly not global. A static variable has file scope and internal linkage, so each ...
  • 是的,这是可以接受的语法。 实际上,我总是在我的实现中这样做,因为从界面复制和粘贴方法定义到实现,反之亦然,而不用记住你的位置。 它有助于我在不同的行上使用对齐的括号。 Wil Shipley 同意我的看法 : 使用分号结束方法实现的定义行,因此可以根据需要在头文件(或文件顶部的“私有”类别)中复制粘贴它们。 “接口”部分需要分号,但不要在“实现”部分中受到任何影响。 Yes, it is acceptable syntax to do this. In fact, I always do this in ...
  • 这是一个很好的概述: http://reference.jumpingmonkey.org/programming_languages/objective-c/types.html 或运行此代码: 32位程序: NSLog(@"Primitive sizes:"); NSLog(@"The size of a char is: %d.", sizeof(char)); NSLog(@"The size of short is: %d.", sizeof(short)); NSLog(@"Th ...
  • print(__FUNCTION__) // Swift NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C Swift 3及以上 print(#function) print(__FUNCTION__) // Swift NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C Swift 3 and above print(#function)
  • 您发布的代码不是C; 它更像是C ++。 现在,Objective-C和C ++可以互操作,但是你需要在Objective-C端使用.mm扩展。 所以,如果你要导入.h. 将文件放入.m文件中,显然会出现错误,因为编译器不期望class定义(C ++)。 如果你将.h文件导入.mm文件,你有可能它会工作(如果所有其他的都是正确的,我的意思是)。 如果您需要更多帮助,请提供更多代码,以便我们更好地了解您的工作。 The code you posted is not C; it is rather C++. N ...
  • 其他答案有一些优点,但也有一些困惑。 这是我的看法: 括号表示法是发送消息/调用方法的一般语法。 有各种不同的方法。 一种特殊的方法是访问器 - 属性的setter和getter。 点语法是调用访问器的另一种形式。 这是一种更新的风格,在过去几年中刚刚添加到语言中。 使用它纯粹是可选的,如果你发现它更清晰,更简洁。 区别仅仅是装饰性的。 使用点语法与调用访问器(getter或setter)相同,具体取决于在较大表达式中如何使用子表达式。 The other answers have some merit, ...
  • 使用NSURLRequest / NSMutableURLRequest,您可以使用您喜欢的任何方法设置身份验证...这是获取一些XML结果的HTTP Basic示例。 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; // or POST or whatever [request setValue:@"application/xml" ...
  • 我认为你已经过度复杂了。 当您需要的只是一个所有其他对象都可以访问的通用集合对象时,没有理由创建单例类。 授予对某些对象的通用访问权限的最佳方法是将其驻留在应用程序委托中。 如果你建议你会有类似的东西: @interface mytAppDelegate : NSObject { NSMutableArray *recipies; ... 然后您可以在应用中的任何地方致电: UIApplicationDelegate *appDelegate=[[UI ...
  • 看一些NSDate比较方法的灵感。 例如,遵循像timeIntervalSinceDate:或descriptionWithCalendarFormat:timeZone:locale: ,如何[NDVector angleWhenIntersectingWithVector:axis1:axis2] ? 这可能看起来像: - (float) angleWhenIntersectingWithVector:(NDVector *)vector2 ...
  • 使用NSLog ,例如: NSLog(@"This is my NSArray: %@", [myNSArray description]); Use NSLog, e.g.: NSLog(@"This is my NSArray: %@", [myNSArray description]);

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)