C语言实现扫描填充算法填充三角形
时间: 2023-06-02 13:08:25 浏览: 182
好的,以下是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;
}
```
此代码基于扫描线算法实现,可以填充由三个点构成的任意三角形。
阅读全文