使用 C 图形填充算法
概念
对于给定的矩形,我们的任务是应用填充算法来填充该矩形。
输入
rectangle(left = 50, top = 50, right= 100, bottom = 100) floodFill( a = 55, b = 55, NewColor = 12, OldColor = 0)
输出

方式
// 一个递归函数,在 (a, b) 处将之前的颜色“OldColor”替换为新颜色“NewColor”,并将 (a, b) 的所有相邻像素也用新颜色填充,即 floodFill(a, b, NewColor, OldColor)
如果 a 或 b 在屏幕外,则返回。
如果 getpixel(a, b) 的颜色与 OldColor 相同,则
递归遍历上方、下方、右侧和左侧。
floodFill(a+1, b, NewColor, OldColor);
floodFill(a-1, b, NewColor, OldColor);
floodFill(a, b+1, NewColor, OldColor);
floodFill(a, b-1, NewColor, OldColor);
示例
// Shows program to fill polygon using floodfill
// algorithm
#include <graphics.h>
#include <stdio.h>
// Describes flood fill algorithm
void flood(int x1, int y1, int new_col, int old_col){
// Checking current pixel is old_color or not
if (getpixel(x1, y1) == old_col) {
// Putting new pixel with new color
putpixel(x1, y1, new_col);
// Shows recursive call for bottom pixel fill
flood(x1 + 1, y1, new_col, old_col);
//Shows recursive call for top pixel fill
flood(x1 - 1, y1, new_col, old_col);
// Shows recursive call for right pixel fill
flood(x1, y1 + 1, new_col, old_col);
// Shows recursive call for left pixel fill
flood(x1, y1 - 1, new_col, old_col);
}
}
int main(){
int gd1, gm1 = DETECT;
// Initializing graph
initgraph(&gd1, &gm1, "");
//Shows rectangle coordinate
int top1, left1, bottom1, right1;
top1 = left1 = 50;
bottom1 = right1 = 300;
// Shows rectangle for print rectangle
rectangle(left1, top1, right1, bottom1);
// Fills start cordinate
int x1 = 51;
int y1 = 51;
// Shows new color to fill
int newcolor = 12;
// Shows old color which you want to replace
int oldcolor = 0;
// Calling for fill rectangle
flood(x1, y1, newcolor, oldcolor);
getch();
return 0;
}输出

广告
数据结构
计算机网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP