当前位置:文档之家› koch雪花图形

koch雪花图形

#include "graphics.h"
#include "extgraph.h"
#include "genlib.h"
#include "simpio.h"
#include "conio.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <windows.h>
#include <olectl.h>
#include <stdio.h>
#include <mmsystem.h>
#include <wingdi.h>
#include <ole2.h>
#include <ocidl.h>
#include <winuser.h>
#include <math.h>
#define pi 3.14159265357989
//转换成极坐标画线
static void DrawPolarLine(double r,double theta)
{ double radians; //弧度
radians=theta*2*pi/360;
DrawLine(r*cos(radians),r*sin(radians));
}
//递归部分
static void DrawFractalLine(double len,double theta, int order) { if(order==0) DrawPolarLine(len,theta);
else{ DrawFractalLine(len/3,theta,order-1);
DrawFractalLine(len/3,theta-60,order-1);
DrawFractalLine(len/3,theta+60,order-1);
DrawFractalLine(len/3,theta,order);
}
}
//基础结构为正三边形
static void KochFractal(double size,int order)
{ double x0,y0;
x0=GetWindowWidth()/2-size/2;
y0=GetWindowHeight()/2-sqrt(3)*size/6;//为在窗口正中间作图,计算坐标MovePen( x0,y0);
DrawFractalLine(size,0,order);//递归
DrawFractalLine(size,120,order);
DrawFractalLine(size,240,order);
}
void main()
{ double size;
int order;
InitGraphics();
printf("Please input the size\n");
size=GetReal();
printf("Please input the order\n");
order=GetInteger();
KochFractal(size, order);
getch();
}。

相关主题