[分形几何]IFS系统的实现.

[分形几何]IFS系统的实现.

                                                                                                                   EmilMatthew

                                                                                                                          05/11/15

       IFS(Iterator Function System)是分形几何中一个十分生动而且吸引人的领域.

       用这个系统,可以生成很多自然景观,如树等.

       先看用这个系统可以生成的一些效果:

       http://www.flash8.net/UploadFile/2005-11/20051115211728611.swf

       很有意思,不是吗?

       那么,这样的图形是如何通过程序进行控制的呢,其实从应用的角度去理解,还是相当好懂的。

       那就是仿射坐标变换.

       何谓仿射坐标变换,便是旋转,扭曲,平移三种效果的迭加。

       数学上对应的变换矩阵为:

       http://www.flash8.net/UploadFile/2005-11/20051115211745488.jpg

       所以,只要能根据我们最后所需要的迭代效果,确定出a,b,c,d,e,f的具体取值(当然,这同时也是最难的),根据我们所定出的需要显示的像素点,便能达到显示的效果.

       下面摘录一些我所收集的IFS系统的相应参数(xml文件数据格式)

       <data>中的参数依次为a,b,c,d,e,f,p(p指的是这种迭代效果出现的概率)

 

LEVY曲线:

<inputDatas iterLimit='10000'  fps='20' offsetX='100' offsetY='250' conditionTimes='2' scale='100'>                                                         

       <data>0.5,-0.5,0.5,0.5,0,0,0.5</data>

       <data>0.5,0.5,-0.5,0.5,0.5,0.5,1</data>

</inputDatas>

       http://www.flash8.net/UploadFile/2005-11/20051115211813246.bmp

 

分形树:

<inputDatas iterLimit='10000'  fps='20' offsetX='100' offsetY='250' conditionTimes='4' scale='300'>                                                         

       <data>0,0,0,0.5,0,0,0.05</data>

       <data>0.42,-0.42,0.42,0.42,0,0.2,0.45</data>

       <data>0.42,0.42,-0.42,0.42,0,0.2,0.85</data>

       <data>0.1,0,0,0.1,0,0.2,1</data>

</inputDatas>

      

羊齿草:

<?xml version="1.0" encoding="gb2312" ?>

 <inputDatas iterLimit="20000" fps="20" offsetX="150" offsetY="500" conditionTimes="4" scale="50">

  <data>0,0,0,0.16,0,0,0.01</data>

  <data>0.85,0.04,-0.04,0.85,0,1.6,0.86</data>

  <data>0.2,-0.26,0.23,0.22,0,1.6,0.93</data>

  <data>-0.15,0.28,0.26,0.24,0,0.44,1</data>

  </inputDatas>

 

FLAMBOYENT皇冠:

<?xml version="1.0" encoding="gb2312"?>

<inputDatas iterLimit='20000'  fps='20' offsetX='150' offsetY='500' conditionTimes='5' scale='200'>                                                         

       <data>0.25,0,0,0.5,0,0,0.154</data>

       <data>0.5,0,0,0.5,-0.25,0.5,0.461</data>

       <data>-0.25,0,0,-0.25,0.25,1,0.539</data>

       <data>0.5,0,0,0.5,0,0.75,0.846</data>

       <data>0.5,0,0,-0.25,0.5,1.25,1</data>

</inputDatas>

       http://www.flash8.net/UploadFile/2005-11/20051115211843705.bmp

 

下面给出的是AS2中的具体实现(只列出核心部分):

function mainF(inTransXml:XML)

{

       var i:Number=0;

       var j:Number=0;

       var tmpStr:String=new String();

       var tmpArr:Array=new Array();

      

       _root.gIteratorLimit=parseInt(inTransXml.firstChild.attributes.iterLimit);

       _root.gFps=parseInt(inTransXml.firstChild.attributes.fps);

 

       _root.gXOffset=parseInt(inTransXml.firstChild.attributes.offsetX);

       _root.gYOffset=parseInt(inTransXml.firstChild.attributes.offsetY);

      

       _root.gConditionTimes=parseInt(inTransXml.firstChild.attributes.conditionTimes);

      

       _root.gScale=parseInt(inTransXml.firstChild.attributes.scale);

      

       for(i=0;i<_root.gConditionTimes;i++)

       {

              tmpStr=new String(inTransXml.firstChild.childNodes[i].childNodes[0]);

              tmpArr=tmpStr.split(",");

      

              _root.a[i]=Number(tmpArr[0]);

              _root.b[i]=Number(tmpArr[1]);

              _root.c[i]=Number(tmpArr[2]);

              _root.d[i]=Number(tmpArr[3]);

              _root.e[i]=Number(tmpArr[4]);

              _root.f[i]=Number(tmpArr[5]);

              _root.gCondition[i]=Number(tmpArr[6]);

       }

             

       _root.Xn=_root.Xn_1=_root.Yn=_root.Yn_1=0;

       _root.gIterTimes=0;

       _root.gIntervalID=setInterval(IFSFun,_root.gFps);

}

 

function  IFSFun():Void

{

       var curIndex:Number=0;

       var stepLen:Number=100;

       var i:Number=0;

      

       i=0;

       //trace(String(_root.a[0])+String(_root.b[0])+String(_root.c[0])+String(_root.d[0])+String(_root.e[0])+String(_root.f[0])+String(_root.gCondition[0]));

       //trace(String(a[1])+String(b[1])+String(c[1])+String(d[1])+String(e[1])+String(f[1])+String(_root.gCondition[1]));

       while(i<stepLen&&_root.gIterTimes<_root.gIteratorLimit)

       {

              curIndex=Math.random();

              //trace(_root.gIterTimes);

              //trace(curIndex);

              switch(_root.gConditionTimes)

              {

              case 2:

                     if(curIndex>=0&&curIndex<=_root.gCondition[0])

                     {

                            _root.Xn_1=_root.a[0]*_root.Xn+_root.b[0]*_root.Yn+_root.e[0];

                            _root.Yn_1=_root.c[0]*_root.Xn+_root.d[0]*_root.Yn+_root.f[0];

                            //trace("===level1==");

                           

                            //trace("Xn_1"+String(_root.Xn_1));

                            //trace("Yn_1"+String(_root.Yn_1));

                     }

                     else

                     {

                            _root.Xn_1=_root.a[1]*_root.Xn+_root.b[1]*_root.Yn+_root.e[1];

                            _root.Yn_1=_root.c[1]*_root.Xn+_root.d[1]*_root.Yn+_root.f[1];

                            //trace("===level2==");

                           

                            //trace("Xn_1"+String(_root.Xn_1));

                            //trace("Yn_1"+String(_root.Yn_1));

                     }

                     break;

      

              case 3:

              if(curIndex>=0&&curIndex<=_root.gCondition[0])

              {

                            _root.Xn_1=_root.a[0]*_root.Xn+_root.b[0]*_root.Yn+_root.e[0];

                            _root.Yn_1=_root.c[0]*_root.Xn+_root.d[0]*_root.Yn+_root.f[0];

              }

              else if(curIndex<=_root.gCondition[1])

              {

                            _root.Xn_1=_root.a[1]*_root.Xn+_root.b[1]*_root.Yn+_root.e[1];

                            _root.Yn_1=_root.c[1]*_root.Xn+_root.d[1]*_root.Yn+_root.f[1];

              }

              else

              {

                            _root.Xn_1=_root.a[2]*_root.Xn+_root.b[2]*_root.Yn+_root.e[2];

                            _root.Yn_1=_root.c[2]*_root.Xn+_root.d[2]*_root.Yn+_root.f[2];

              }

              break;

      

              case 4:

              if(curIndex>=0&&curIndex<=_root.gCondition[0])

              {

                            _root.Xn_1=_root.a[0]*_root.Xn+_root.b[0]*_root.Yn+_root.e[0];

                            _root.Yn_1=_root.c[0]*_root.Xn+_root.d[0]*_root.Yn+_root.f[0];

              }

              else if(curIndex<=_root.gCondition[1])

              {

                            _root.Xn_1=_root.a[1]*_root.Xn+_root.b[1]*_root.Yn+_root.e[1];

                            _root.Yn_1=_root.c[1]*_root.Xn+_root.d[1]*_root.Yn+_root.f[1];

              }

              else if(curIndex<=_root.gCondition[2])

              {

                            _root.Xn_1=_root.a[2]*_root.Xn+_root.b[2]*_root.Yn+_root.e[2];

                            _root.Yn_1=_root.c[2]*_root.Xn+_root.d[2]*_root.Yn+_root.f[2];

              }

              else

              {    

                            _root.Xn_1=_root.a[3]*_root.Xn+_root.b[3]*_root.Yn+_root.e[3];

                            _root.Yn_1=_root.c[3]*_root.Xn+_root.d[3]*_root.Yn+_root.f[3];

              }

              break;

      

              case 5:

              if(curIndex>=0&&curIndex<=_root.gCondition[0])

              {

                            _root.Xn_1=_root.a[0]*_root.Xn+_root.b[0]*_root.Yn+_root.e[0];

                            _root.Yn_1=_root.c[0]*_root.Xn+_root.d[0]*_root.Yn+_root.f[0];

              }

              else if(curIndex<=_root.gCondition[1])

              {

                            _root.Xn_1=_root.a[1]*_root.Xn+_root.b[1]*_root.Yn+_root.e[1];

                            _root.Yn_1=_root.c[1]*_root.Xn+_root.d[1]*_root.Yn+_root.f[1];

              }

              else if(curIndex<=_root.gCondition[2])

              {

                            _root.Xn_1=_root.a[2]*_root.Xn+_root.b[2]*_root.Yn+_root.e[2];

                            _root.Yn_1=_root.c[2]*_root.Xn+_root.d[2]*_root.Yn+_root.f[2];

              }

              else if(curIndex<=_root.gCondition[3])

              {    

                            _root.Xn_1=_root.a[3]*_root.Xn+_root.b[3]*_root.Yn+_root.e[3];

                            _root.Yn_1=_root.c[3]*_root.Xn+_root.d[3]*_root.Yn+_root.f[3];

              }

              else

              {    

                            _root.Xn_1=_root.a[4]*_root.Xn+_root.b[4]*_root.Yn+_root.e[4];

                            _root.Yn_1=_root.c[4]*_root.Xn+_root.d[4]*_root.Yn+_root.f[4];

              }

             

                     break;

 

              default:

                     trace3("error while random num produce/n");

                     trace("error while random num produce/n");

                     break;

              }

              //switch ends

              //draw new node

              drawNode2(_root.gXOffset+Xn_1*_root.gScale,_root.gYOffset-Yn_1*_root.gScale,_root.gColorArr[13]);

              //update data.

              _root.Xn=_root.Xn_1;

              _root.Yn=_root.Yn_1;

              i++;

              _root.gIterTimes++;

       }    

      

       if(_root.gIterTimes==_root.gIteratorLimit)

              clearInterval(_root.gIntervalID);

}

 

function drawNode2(x:Number,y:Number,nodeColor:Number):Void

{

       //trace("invoke");

       with(eval("_root.gBrush"))

       {

              lineStyle(1,nodeColor,100);

              moveTo(x-0.5,y);

              lineTo(x+0.5,y);

       }

}

 

源码下载:

http://free3.e-168.cn/as2forward/downloads/IFSSystem.rar

//如果上面这个链接无法响应下载(有可能是被网站给屏蔽掉了),则可使用下载工具(如迅雷等)下载。欢迎提出批评与指正意见.

你可能感兴趣的:(String,function,iterator,Random,encoding,下载工具)