Chaos game-混沌游戏

      In mathematics, the term chaos game, as coined by Michael Barnsley,originally referred to a method of creating a fractal, using a polygon and an initial point selected at random inside it. The fractal is created by iteratively creating a sequence of points, starting with the initial random point, in which each point in the sequence is a given fraction of the distance between the previous point and one of the vertices of the polygon; the vertex is chosen at random in each iteration. Repeating this iterative process a large number of times, selecting the vertex at random on each iteration, and throwing out the first few points in the sequence, will often (but not always) produce a fractal shape. Using a regular triangle and the factor 1/2 will result in the Sierpinski triangle, while creating the proper arrangement with four points and a factor 1/2 will create a display of a "Sierpinski Tetrahedron", the three-dimensional analogue of the Sierpinski triangle. As the number of points is increased to a number N, the arrangement forms a corresponding (N-1)-dimensional Sierpinski Simplex.

Sierpinski triangle的原理图:

Chaos game-混沌游戏_第1张图片

我的计算机图形学基础教程中绘制的Sierpinski triangle效果图:

Chaos game-混沌游戏_第2张图片

Chaos game之Sierpinski triangle代码(孔令德编写):

void CTestView::ChaosGame(int n)
{
     CClientDC dc(this);
     double x=100,y=100;
     double x1,y1;
     double xa=MaxX/2,ya=MaxY/2-300;//三角形顶点A的坐标
     double xb=MaxX/2-300,yb=MaxY/2+300;//三角形顶点B的坐标
     double xc=MaxX/2+300,yc=MaxY/2+300;//三角形顶点C的坐标
    for(int k=n;k>0;k--)
    {
          double r=double(rand())/RAND_MAX;
          if(r<=0.3)//计算随机产生的三角形顶点A和上一点连线的中点坐标
         {
                x1=(x+xa)/2;y1=(y+ya)/2;
         }
        else if(r<=0.6)//计算随机产生的三角形顶点B和上一点连线的中点坐标
         {
              x1=(x+xb)/2;y1=(y+yb)/2;
         }
         else//计算随机产生的三角形顶点C和上一点连线的中点坐标
        {
              x1=(x+xc)/2;y1=(y+yc)/2;
        }
         x=x1;y=y1;
         dc.SetPixelV((ROUND(x)),ROUND((y)),RGB(x*r,r*200,y*r*1000));      
      }
}    
我绘制的Sierpinski Tetrahedron效果图:

Chaos game-混沌游戏_第3张图片

Chaos game-混沌游戏_第4张图片

Chaos game-混沌游戏_第5张图片

你可能感兴趣的:(c++,图形,计算机,game)