植物分形
原理:分形之分叉结构 迭代法
绘图库:Easy Graphics Engine (EGE) 编程语言:c++
示例:树叶
角度60度 伸缩率0.6 角度45度 伸缩率0.5
代码:
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <time.h>
const double pi = 3.1415926536;
struct Point
{
double x;
double y;
};
Point Rotate(Point p1, Point p2, double A)
{
Point r;
r.x = p1.x + (p2.x - p1.x) * cos(A) + (p2.y - p1.y) * sin(A);
r.y = p1.y + (p2.y - p1.y) * cos(A) - (p2.x - p1.x) * sin(A);
return r;
}
Point Zoom(Point p1, Point p2, double a)
{
Point r;
r.x = p1.x + (p2.x - p1.x) * a;
r.y = p1.y + (p2.y - p1.y) * a;
return r;
}
void D1(Point p1, Point p2,double u,double k,int n)
{
if(n>0)
{
Point p3,p4,p5,p,p6,p7;
p3.x=p1.x+(p2.x-p1.x)/3;
p3.y=p1.y+(p2.y-p1.y)/3;
p=Zoom(p3,p2,k);
p4=Rotate(p3,p,u*pi/180);
p5=Rotate(p3,p,2*pi-u*pi/180);
line_f(p3.x,p3.y,p4.x,p4.y);
line_f(p3.x,p3.y,p5.x,p5.y);
line_f(p1.x,p1.y,p2.x,p2.y);
D1(p3,p4,u,k,n-1);
D1(p3,p5,u,k,n-1);
D1(p3,p2,u,k,n-1);
}
}
int main()
{
double u,k;
struct Point p1,p2;
p1.x=300;
p1.y=570;
p2.x=300;
p2.y=50;
printf(" 树叶\n");
cc:
printf("角度:\n");
scanf("%lf",&u);
printf("伸缩率:\n");
scanf("%lf",&k);
printf("\n\n");
initgraph(600,600);
setcolor(RED);
D1(p1,p2,u,k,10);
goto cc;
return 0;
}
|