c语言实区域填充算法代码
时间: 2023-12-04 13:41:52 浏览: 41
以下是C语言实现区域填充算法的代码:
```c
#include <graphics.h>
#include <conio.h>
void floodFill(int x, int y, int fillColor, int oldColor) {
if (getpixel(x, y) == oldColor) {
putpixel(x, y, fillColor);
floodFill(x + 1, y, fillColor, oldColor);
floodFill(x - 1, y, fillColor, oldColor);
floodFill(x, y + 1, fillColor, oldColor);
floodFill(x, y - 1, fillColor, oldColor);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// 画一个多边形
int poly[10] = {100, 100, 200, 100, 200, 200, 150, 250, 100, 200};
drawpoly(5, poly);
// 填充多边形
floodFill(150, 150, YELLOW, BLACK);
getch();
closegraph();
return 0;
}
```
相关问题
C语言实现扫描填充算法填充三角形
好的,以下是C语言实现扫描填充算法填充三角形的代码:
```
#include <stdio.h>
#include <graphics.h>
void scanLineFill(int x1, int y1, int x2, int y2, int x3, int y3, int fill_color) {
int i, j, x, y, tmp;
float dx1, dx2, dx3, zx1, zx2, zx3;
if (y1 > y2) {
tmp = y1; y1 = y2; y2 = tmp;
tmp = x1; x1 = x2; x2 = tmp;
}
if (y1 > y3) {
tmp = y1; y1 = y3; y3 = tmp;
tmp = x1; x1 = x3; x3 = tmp;
}
if (y2 > y3) {
tmp = y2; y2 = y3; y3 = tmp;
tmp = x2; x2 = x3; x3 = tmp;
}
dx1 = (float)(x2 - x1) / (float)(y2 - y1);
dx2 = (float)(x3 - x1) / (float)(y3 - y1);
dx3 = (float)(x3 - x2) / (float)(y3 - y2);
zx1 = x1; zx2 = x1 + 0.5; zx3 = x2;
for (i = y1; i <= y3; i++) {
if (i < y2) {
x = zx1; y = i;
while (x <= zx3) {
putpixel(x, y, fill_color);
x++;
}
}
else {
x = zx2; y = i;
while (x <= zx3) {
putpixel(x, y, fill_color);
x++;
}
}
zx1 += dx1; zx2 += dx2; zx3 += dx3;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
scanLineFill(100, 100, 200, 200, 100, 200, RED);
getch();
closegraph();
return 0;
}
```
此代码基于扫描线算法实现,可以填充由三个点构成的任意三角形。
c语言建立二叉树的算法代码
以下是C语言建立二叉树的算法代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef char tree_datatype;
typedef struct tree_t {
tree_datatype data; // 数据域
struct tree_t *lchild; // 指向左子树的结构体指针
struct tree_t *rchild; // 指向右子树的结构体指针
} bitree_t;
// 创建二叉树
bitree_t *create_bitree() {
bitree_t *root = NULL;
tree_datatype ch;
scanf("%c", &ch);
if (ch == '#') {
root = NULL;
} else {
root = (bitree_t *) malloc(sizeof(bitree_t));
root->data = ch;
root->lchild = create_bitree();
root->rchild = create_bitree();
}
return root;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)