Physics 版 (精华区)
发信人: zjliu (秋天的萝卜), 信区: Physics
标 题: 分形:三角形和树的生成 /源程序
发信站: 哈工大紫丁香 (Sat May 24 16:17:47 2003) , 转信
发信站: BBS 水木清华站
//本程序利用分形技术画图
//先画一个Sierpinsky三角形,再画一棵树
//BC 3.1 编译,也可用TC或BCW
//其中函数initgraph的第三个参数可能需要修改
#include "graphics.h"
#include "math.h"
#include "stdlib.h"
#include "conio.h"
#include "time.h"
#define N 65535 //迭代次数
//=============================================================
main()
{ int GraphDriver=DETECT;
int GraphMode;
unsigned int k,mod;
double TempX,TempY,StartX,StartY;
initgraph(&GraphDriver,&GraphMode,"c:\\bc31\\bgi");
randomize();
//===============================================================
//Draw triangle 用迭代函数系(IFS)画三角形
//===============================================================
setbkcolor(BLUE);
cleardevice();
TempX=(double)getmaxx();
TempY=(double)getmaxy();
StartX=TempX;
StartY=TempY;
for(k=1;k<N;k++)
{ TempX=TempX*0.5;
TempY=TempY*0.5;
mod=random(32766) % 3;
switch(mod)
{ case 0: break;
case 1: TempX+=(double)StartX*0.5;
break;
case 2: TempY+=(double)StartY*0.5;
}
putpixel((int)TempX,(int)TempY,YELLOW);
}
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,0);
setusercharsize(3,1,3,1);
outtextxy(450,340,"OK");
getch();
//==============================================================
//Draw tree 用迭代函数系(IFS)画树
//==============================================================
setbkcolor(BLUE);
cleardevice();
break;
case 2: TempX=0.2*StartX-0.16*StartY;
TempY=0.23*StartX+0.22*StartY+1.2;
break;
case 3: TempX=0.2*StartY-0.16*StartX;
TempY=0.26*StartX+0.28*StartY+0.44;
}
putpixel(
100+ (unsigned int)(TempX * (double)(120)),
480- (unsigned int)(TempY * (double)(100)),
GREEN
); //Draw first tree 第一棵树
putpixel(
100+ (unsigned int)(TempX * (double)(120)),
480- (unsigned int)(TempY * (double)(100)),
GREEN
); //Draw first tree 第一棵树
StartX=TempX;
StartY=TempY;
}
settextstyle(TRIPLEX_FONT,HORIZ_DIR,0);
setusercharsize(2,1,3,1);
outtextxy(450,340,"OK");
getch();
closegraph();
return 0;
}
--
※ 来源:.哈工大紫丁香 http://bbs.hit.edu.cn [FROM: 202.118.229.86]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.069毫秒