#include <graphics.h> //包含图形库头文件

void fill(int x, int y, int fillColor, int oldColor) //定义填充函数 { if (getpixel(x, y) == oldColor && x >= 0 && y >= 0 && x <= getmaxx() && y <= getmaxy()) //如果当前像素颜色与原始颜色相同且在屏幕范围内 { putpixel(x, y, fillColor); //将当前像素点颜色填充为新颜色 fill(x + 1, y, fillColor, oldColor); //递归调用fill函数填充当前像素点右边的像素 fill(x, y + 1, fillColor, oldColor); //递归调用fill函数填充当前像素点下面的像素 fill(x - 1, y, fillColor, oldColor); //递归调用fill函数填充当前像素点左边的像素 fill(x, y - 1, fillColor, oldColor); //递归调用fill函数填充当前像素点上面的像素 } }

int main() { int gd = DETECT, gm; initgraph(&gd, &gm, ""); //初始化图形界面 rectangle(100, 100, 200, 200); //绘制矩形 fill(150, 150, YELLOW, BLACK); //填充矩形 getch(); //等待用户按键 closegraph(); //关闭图形界面 return 0; }

用C语言写一个区域填充算法,用进栈出栈知识,简洁有注释,适用于Visual C++6.0

原文地址: https://www.cveoy.top/t/topic/z7y 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录