最近上了计算机图形学发现了一个有趣的地方,就是分形几何,这是些利用神奇的算法画出的几何图形,很神奇。可以模拟自然景物,还可以画出说不出是什么东西的绚丽图片。
下面这是一个画分形树的代码,抄的老师给的ppt,不过转化成MFC上的代码了。
void CbView::Tree(double x,double y,double L,double A)
{
CDC *dc;
dc = GetDC();
double PI = 3.1415926;
double B=10, C=9, s1=2, s2=3, s3=1.3;
int x1, y1, x2, y2, x1L, y1L, x1R, y1R, x2L, y2L, x2R, y2R;
if (L > s1)
{
x2 = int(x + L * cos(A*PI/ 180));
y2 = int(y - L * sin(A*PI / 180));
x2L = x2 + L / s2 * cos((A + B)*PI / 180);
y2L = y2 - L / s2 * sin((A + B)*PI / 180);
x2R = x2 + L / s2 * cos((A - B)*PI / 180);
y2R = y2 - L / s2 * sin((A - B)*PI / 180);
x1 = x + L / s2 * cos(A*PI / 180);
y1 = y - L / s2 * sin(A*PI / 180);
x1L = x1 + L / s2 * cos((A + B)*PI / 180);
y1L = y1 - L / s2 * sin((A + B)*PI / 180);
x1R = x1 + L / s2 * cos((A - B)*PI / 180);
y1R = y1 + L / s2 * sin((A - B)*PI / 180);
dc->MoveTo(x, y);
dc->LineTo(x2,y2);
dc->MoveTo(x2, y2);
dc->LineTo(x2L, y2L);
dc->MoveTo(x2, y2);
dc->LineTo(x2R, y2R);
dc->MoveTo(x1, y1);
dc->LineTo(x1R, y1R);
dc->MoveTo(x1, y1);
dc->LineTo(x1L, y1L);
Tree(x2, y2, L / s3, A - C);
Tree(x2L, y2L, L / s2, A + B);
Tree(x2R, y2R, L / s2, A - B);
Tree(x1R, y1R, L / s2, A - B);
Tree(x1L, y1L, L / s2, A - B);
}
}
image.png
这看起来是一棵树,也像一个羽毛,反正就是这么个东西,很神奇
网友评论