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的原理图:


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


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效果图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值