#include<easyx.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
struct point
{
float x;
float y;
};
void drawtriangle(point pa, point pb, point pc)
{
struct point pa1;//pb、pc边中点
struct point pb1;//pa、pc边中点
struct point pc1;//pb、pa边中点
pa1.x = (pb.x + pc.x) / 2;
pa1.y = (pb.y + pc.y) / 2;
pc1.x= (pa.x + pb.x) / 2;
pc1.y = (pa.y + pb.y) / 2;
pb1.x = (pa.x + pc.x) / 2;
pb1.y = (pa.y + pc.y) / 2;
if (fabs(pb1.x - pc1.x) < 5)//三角形边长小于某个值时停止迭代
{
return;
}
line(pc1.x, pc1.y, pb1.x, pb1.y);
line(pc1.x, pc1.y, pa1.x, pa1.y);
line(pb1.x, pb1.y, pa1.x, pa1.y);
drawtriangle(pa, pb1, pc1);
drawtriangle(pc1, pb, pa1);
drawtriangle(pb1, pa1, pc);
}
int main()
{
initgraph(800, 640);
struct point pa;
struct point pb;
struct point pc;
pa.x = 400, pa.y = 10;
pb.x = 10, pb.y = 600;
pc.x = 750, pc.y = 600;
setlinecolor(WHITE);
line(pa.x, pa.y, pb.x, pb.y);
line(pa.x, pa.y, pc.x, pc.y);
line(pb.x, pb.y, pc.x, pc.y);
drawtriangle(pa,pb,pc);
_getch();
closegraph();
return 0;
}
运行结果图像如下