VC use recursive method to draw fractal graph

Source: Internet
Author: User

Fractal geometry is a new subject in the field of mathematics, if each element of the graph is deformed according to some rules, the new figure is obtained, and so on, the graph obtained after several times of deformation is fractal shape. The couch curve is the most typical fractal shape:

To transform a line segment according to Figure 1, get figure 1, and then the figure 1 of each line in Figure 1 of the line transformation to get figure 2, and so on, 6 times to transform to get figure 6, if the infinite transformation, it is couch curve, couch curve of the dimension is not an integer dimension, For more details, see the fractal aspect of the book.

With fractal graphics can draw many beautiful patterns and is widely used, the following will be a few simple fractal graphics code and graphics to share.

//Couch曲线的画法
void Couch(CDC *pDC,int x1,int y1,int x2,int y2,int n)
{
//pDC是画图的设备上下文的指针
//x1,y1,x2,y2是起始的两点
//其中参数n是递归的层数
int x3,y3,x4,y4,x5,y5;
//以下是根据空间几何计算出来的坐标
x3=x1+(x2-x1)/3;
y3=y1+(y2-y1)/3;
x4=x1+(x2-x1)*2/3;
y4=y1+(y2-y1)*2/3;
x5=x3+(x4-x3)/2+int(sqrt(3)*(y4-y3)/2);
y5=y3-int(sqrt(3)*(x4-x3)/2)+(y4-y3)/2;
//递归最后一层,递归的出口
if(n==1)
{
pDC->MoveTo(x1,y1);
pDC->LineTo(x3,y3);
pDC->LineTo(x5,y5);
pDC->LineTo(x4,y4);
pDC->LineTo(x2,y2);
}
else
{
//递归画图
Couch(pDC,x1,y1,x3,y3,n-1);
Couch(pDC,x3,y3,x5,y5,n-1);
Couch(pDC,x5,y5,x4,y4,n-1);
Couch(pDC,x4,y4,x2,y2,n-1);
}
}

//斯宾斯基篓垫的画法
void Floor(CDC *pDC,int x1, int y1,int x2,int y2,int x3,int y3,int n)
{
//pDC是画图的设备上下文的指针
//x1,y1,x2,y2,x3,y3是起始的三角形的三点坐标
//其中参数n是递归的层数
int x11,x22,x33,y11,y22,y33;
//以下是根据空间几何计算出来的坐标
x11=(x2+x3)/2;
y11=(y2+y3)/2;
x22=(x1+x3)/2;
y22=(y1+y3)/2;
x33=(x1+x2)/2;
y33=(y1+y2)/2;

pDC->MoveTo(x11,y11);
pDC->LineTo(x22,y22);
pDC->MoveTo(x11,y11);
pDC->LineTo(x33,y33);
pDC->MoveTo(x22,y22);
pDC->LineTo(x33,y33);
//递归最后一层,递归的出口
if(n==1)
{
pDC->MoveTo(x11,y11);
pDC->LineTo(x22,y22);
pDC->LineTo(x33,y33);
pDC->LineTo(x11,y11);
}
else
{
//递归画图
Floor(pDC,x1,y1,x33,y33,x22,y22,n-1);
Floor(pDC,x33,y33,x2,y2,x11,y11,n-1);
Floor(pDC,x22,y22,x11,y11,x3,y3,n-1);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.