五、 附程序
下面所有的程序在TC++3.1下实现并调试运行,本文的所有分形图形都是这些程序绘制而成。为节省篇章,每个程序都去掉了开头包含头文件的宏指令
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
(一) Koch曲线和Koch雪花
struct point{ int x,y; point *next;};
int main()
{ int driver=DETECT,mode;
initgraph(&driver,&mode,"..\\bgi");
setbkcolor(WHITE);setcolor(LIGHTBLUE);setlinestyle(0,0,3);
point *plist,*(pnew[3]),*p;
double x1,y1,x2,y2;
plist=(point *)malloc(sizeof(point));
plist->x=120;
plist->y=360;
p=(point *)malloc(sizeof(point));
p->x=520;
p->y=360;
plist->next=p;
p=(point *)malloc(sizeof(point));
p->x=320;
p->y=13;
plist->next->next=p;
p=(point *)malloc(sizeof(point));
p->x=120;
p->y=360;
p->next=NULL;
plist->next->next->next=p;
//上面四小段是向链表中添加初始的四点坐标,如果只做koch曲线,只需添加两个点
for(int i=0;i<5;i++)
{ for(p=plist;p->next;p=pnew[2]->next)
{ for(int j=0;j<3;j++) pnew[j]=(point *)malloc(sizeof(point));
x1=p->x;
y1=p->y;
x2=p->next->x;
y2=p->next->y;
pnew[0]->x=2*x1/3.0+x2/3.0;
pnew[0]->y=2*y1/3.0+y2/3.0;
pnew[1]->x=x1/2.0+x2/2.0-y2/2.0/sqrt(3.0)+y1/2.0/sqrt(3.0);
pnew[1]->y=y1/2.0+y2/2.0+x2/2.0/sqrt(3.0)-x1/2.0/sqrt(3.0);
pnew[2]->x=x1/3.0+2*x2/3.0;
pnew[2]->y=y1/3.0+2*y2/3.0;
pnew[2]->next=p->next;
p->next=pnew[0];
for(j=0;j<2;j++) pnew[j]->next=pnew[j+1];
}
cleardevice();
moveto(plist->x,480-plist->y);
for(p=plist;p;p=p->next)
lineto(p->x,480-p->y);
getch();
}
getch();
closegraph();
return 0;
}
(二) 由Koch曲线联想的分形一
struct point{ float x,y; point *next;};
int main()
{ int driver=DETECT,mode;
initgraph(&driver,&mode,"..\\bgi");
setbkcolor(WHITE);setcolor(BLUE);setlinestyle(0,0,1);
point *plist,*(pnew[4]),*p;
float x1,y1,x2,y2;
plist=(point *)malloc(sizeof(point));
plist->x=40;
plist->y=120;
p=(point *)malloc(sizeof(point));
p->x=600;
p->y=120;
p->next=NULL;
plist->next=p;
for(int i=0;i<5;i++)
{ for(p=plist;p->next;p=pnew[3]->next)
{ for(int j=0;j<4;j++) pnew[j]=(point *)malloc(sizeof(point));
x1=p->x;
y1=p->y;
x2=p->next->x;
y2=p->next->y;
pnew[0]->x=2*x1/3.0+x2/3.0;
pnew[0]->y=2*y1/3.0+y2/3.0;
pnew[1]->x=2*x1/3.0+x2/3.0-y2/3.0+y1/3.0;
pnew[1]->y=2*y1/3.0+y2/3.0+x2/3.0-x1/3.0;
pnew[2]->x=x1/3.0+2*x2/3.0-y2/3.0+y1/3.0;
pnew[2]->y=y1/3.0+2*y2/3.0+x2/3.0-x1/3.0;
pnew[3]->x=x1/3.0+2*x2/3.0;
pnew[3]->y=y1/3.0+2*y2/3.0;
pnew[3]->next=p->next;
p->next=pnew[0];
for(j=0;j<3;j++) pnew[j]->next=pnew[j+1];
}
cleardevice();
moveto(plist->x,480-plist->y);
for(p=plist;p;p=p->next)
lineto(p->x,480-p->y);
getch();
}
getch();
closegraph();
return 0;
}
(三) 由Koch曲线联想的分形二
struct point{ int x,y; point *next;};
int main()
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"..\\bgi");
setbkcolor(WHITE);setcolor(CYAN); setlinestyle(0,0,1);
point *plist,*(pnew[7]),*p;
float x1,y1,x2,y2;
p=(point *)malloc(sizeof(point));
p->x=40;
p->y=240;
p->next=NULL;//初始时线段的第一个端点
plist=(point *)malloc(sizeof(point));
plist->x=600;
plist->y=240;
plist->next=p; //初始时线段的第二个端点
for(int i=0;i<4;i++)
{ for(p=plist;p->next;p=pnew[6]->next)
{ for(int j=0;j<7;j++) pnew[j]=(point *)malloc(sizeof(point));
x1=p->x;
y1=p->y;
x2=p->next->x;
y2=p->next->y;
pnew[0]->x=3*x1/4.0+x2/4.0;
pnew[0]->y=3*y1/4.0+y2/4.0;
pnew[1]->x=3*x1/4.0+x2/4.0-y2/4.0+y1/4.0;
pnew[1]->y=3*y1/4.0+y2/4.0+x2/4.0-x1/4.0;
pnew[2]->x=x1/2.0+x2/2.0-y2/4.0+y1/4.0;
pnew[2]->y=y1/2.0+y2/2.0+x2/4.0-x1/4.0;
pnew[3]->x=x1/2.0+x2/2.0;
pnew[3]->y=y1/2.0+y2/2.0;
pnew[4]->x=x1/2.0+x2/2.0+y2/4.0-y1/4.0;
pnew[4]->y=y1/2.0+y2/2.0-x2/4.0+x1/4.0;
pnew[5]->x=x1/4.0+3*x2/4.0+y2/4.0-y1/4.0;
pnew[5]->y=y1/4.0+3*y2/4.0-x2/4.0+x1/4.0;
pnew[6]->x=x1/4.0+3*x2/4.0;
pnew[6]->y=y1/4.0+3*y2/4.0;
pnew[6]->next=p->next;
p->next=pnew[0];
for(j=0;j<6;j++) pnew[j]->next=pnew[j+1];
}
cleardevice();
moveto(plist->x,480-plist->y);
for(p=plist;p;p=p->next)
lineto(p->x,480-p->y);
getch();
}
getch();
closegraph();
return 0;
}
(四)
struct point{ float x,y; point *next;};
int main()
{ int driver=DETECT,mode;
initgraph(&driver,&mode,"..\\bgi");
setbkcolor(WHITE);setcolor(BLUE); setlinestyle(0,0,3);
point *plist,*(pnew[8]),*p;
float x1,y1,x2,y2;
plist=(point *)malloc(sizeof(point));
plist->x=600;
plist->y=240;
p=(point *)malloc(sizeof(point));
p->x=40;
p->y=240;
p->next=NULL;
plist->next=p;
for(int i=0;i<5;i++)
{ for(p=plist;p->next;p=pnew[7]->next)
{ for(int j=0;j<8;j++) pnew[j]=(point *)malloc(sizeof(point));
x1=p->x;
y1=p->y;
x2=p->next->x;
y2=p->next->y;
pnew[0]->x=4*x1/5.0+x2/5.0;
pnew[0]->y=4*y1/5.0+y2/5.0;
pnew[1]->x=4*x1/5.0+x2/5.0-y2/5.0+y1/5.0;
pnew[1]->y=4*y1/5.0+y2/5.0+x2/5.0-x1/5.0;
pnew[2]->x=3*x1/5.0+2*x2/5.0-y2/5.0+y1/5.0;
pnew[2]->y=3*y1/5.0+2*y2/5.0+x2/5.0-x1/5.0;
pnew[3]->x=3*x1/5.0+2*x2/5.0;
pnew[3]->y=3*y1/5.0+2*y2/5.0;
pnew[4]->x=2*x1/5.0+3*x2/5.0;
pnew[4]->y=2*y1/5.0+3*y2/5.0;
pnew[5]->x=2*x1/5.0+3*x2/5.0+y2/5.0-y1/5.0;
pnew[5]->y=2*y1/5.0+3*y2/5.0-x2/5.0+x1/5.0;
pnew[6]->x=x1/5.0+4*x2/5.0+y2/5.0-y1/5.0;
pnew[6]->y=y1/5.0+4*y2/5.0-x2/5.0+x1/5.0;
pnew[7]->x=x1/5.0+4*x2/5.0;
pnew[7]->y=y1/5.0+4*y2/5.0;
pnew[7]->next=p->next;
p->next=pnew[0];
for(j=0;j<7;j++) pnew[j]->next=pnew[j+1];
}
cleardevice();
moveto(plist->x,480-plist->y);
for(p=plist;p;p=p->next)
lineto(p->x,480-p->y);
getch();
}
getch();
closegraph();
return 0;
}
(五) 树
void drawbranch(int x1,int y1,int x2,int y2,int n)
{ if(n==0) return;
line(x1,480-y1,x2,480-y2);
drawbranch((2*x1+x2)/3,(2*y1+y2)/3,
(2*x1+x2)/3+(x2-x1)*sqrt(2)/4+(y2-y1)*sqrt(2)/4,
(2*y1+y2)/3+(y2-y1)*sqrt(2)/4-(x2-x1)*sqrt(2)/4,n-1);
drawbranch((2*x1+x2)/3,(2*y1+y2)/3,
(2*x1+x2)/3+(x2-x1)*sqrt(2)/4-(y2-y1)*sqrt(2)/4,
(2*y1+y2)/3+(y2-y1)*sqrt(2)/4+(x2-x1)*sqrt(2)/4,n-1);
drawbranch((x1+x2)/2,(y1+y2)/2,
(x1+x2)/2+(x2-x1)*sqrt(3)/4-(y2-y1)/4,
(y1+y2)/2+(y2-y1)*sqrt(3)/4+(x2-x1)/4,n-1);
drawbranch((x1+2*x2)/3,(y1+2*y2)/3,
(x1+2*x2)/3+(x2-x1)*sqrt(3)/6+(y2-y1)/6,
(y1+2*y2)/3+(y2-y1)*sqrt(3)/6-(x2-x1)/6,n-1);
drawbranch((x1+2*x2)/3,(y1+2*y2)/3,x2,y2,n-1);
}
int main()
{ int driver=DETECT,mode;
initgraph(&driver,&mode,"..\\bgi");
setbkcolor(DARKGRAY);setcolor(GREEN);setlinestyle(0,0,3);
drawbranch(320,0,320,480,8);
getch();
closegraph();
return 0;
}
(六) Mandelbrot集
int main()
{ int driver=DETECT,mode=0;
initgraph(&driver,&mode,"..\\bgi");
setbkcolor(BLACK);
int x,y,n;
double a,b,a1,b1;
for(x=0;x<640;x++)
{ for(y=0;y<480;y++)
{ n=0;a=b=0;
while(n<100&&fabs(a*a+b*b)<50)
{ a1=a*a-b*b+(x-3*640/4.0)/(640/4.0);
b1=2*a*b+(y-240)/240.0*1.5;
a=a1;
b=b1;
n++;
}
if(n<100) { putpixel(x,480-y,n%16);}
}
}
getch();
getch();
closegraph();
return 0;
}
(七) Julia集
int main()
{ int driver=DETECT,mode=0;
initgraph(&driver,&mode,"..\\bgi");
setbkcolor(BLACK);
int x,y,n;
double a,b,a1,b1,c,d;
c=0.364;
d=0.10;
for(x=0;x<640;x++)
{ for(y=0;y<480;y++)
{ n=0;
a=(x-320)/320.0*2;
b=(y-240)/240.0*2;
while(n<100&&fabs(a*a+b*b)<100)
{ a1=a*a-b*b+c;
b1=2*a*b+d;
a=a1;
b=b1;
n++;
}
if(n<100) { putpixel(x,480-y,n%16);}
}
}
getch();
getch();
closegraph();
return 0;
}