洪水填充算法(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
最满意答案
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 ...
-
分号后面的方法名称在Objective-C实现文件中(Semicolon after the method name in Objective-C implementation file)[2022-10-05]
是的,这是可以接受的语法。 实际上,我总是在我的实现中这样做,因为从界面复制和粘贴方法定义到实现,反之亦然,而不用记住你的位置。 它有助于我在不同的行上使用对齐的括号。 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 ++和Objective-c(C++ and Objective-c)[2023-07-28]
您发布的代码不是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" ...
-
在Objective-C(iPhone)中传递引用的灵活方式?(Flexible way to pass references in Objective-C (iPhone)?)[2023-04-21]
我认为你已经过度复杂了。 当您需要的只是一个所有其他对象都可以访问的通用集合对象时,没有理由创建单例类。 授予对某些对象的通用访问权限的最佳方法是将其驻留在应用程序委托中。 如果你建议你会有类似的东西: @interface mytAppDelegate : NSObject{ NSMutableArray *recipies; ... 然后您可以在应用中的任何地方致电: UIApplicationDelegate *appDelegate=[[UI ... -
看一些NSDate比较方法的灵感。 例如,遵循像timeIntervalSinceDate:或descriptionWithCalendarFormat:timeZone:locale: ,如何[NDVector angleWhenIntersectingWithVector:axis1:axis2] ? 这可能看起来像: - (float) angleWhenIntersectingWithVector:(NDVector *)vector2 ...
-
在php for iphone(objective-c)中替代print_r?(Alternative for print_r in php for iphone(objective-c)?)[2023-03-09]
使用NSLog ,例如: NSLog(@"This is my NSArray: %@", [myNSArray description]); Use NSLog, e.g.: NSLog(@"This is my NSArray: %@", [myNSArray description]);