24小时热门版块排行榜    

查看: 637  |  回复: 1
当前主题已经存档。

ganchunlei

至尊木虫 (著名写手)

优秀版主

[交流] 元胞自动机模拟

元胞自动机模拟 cellular automaton
/*------------------------------------------------------------------------------*
* File Name: Cellular Automator example for Origin C                                                        *
*------------------------------------------------------------------------------*/

#include

//**********************************
// to implement a command
// ca
// for a simple celluar automator program
// usage:
// 1. compile this file
// 2. close CodeBuilder for faster execution
// 3. create a new matrix
// 4. from script window, type ca and press enter
//**********************************

////////////////////////////////////////////////////////////////////////////////////
//static int temp[8] = {0,0,0,1,1,1,1,0}; // Rule 30
static int my_rules[8] = {0,0,0,1,1,0,1,0};

////////////////////////////////////////////////////////////////////////////////////
// our basic rule function
// we take all the 8 possible permutations for the three cells above the
// current cell and we need to determine live (1) or die (0)
// and the determinations are stored in the my_rules array above
//
// use static function to avoid being visible from LabTalk
static int rule(int left, int top, int right)
{
        if(left && top && right)
                return my_rules[0];
       
        if(left && top && !right)
                return my_rules[1];
       
        if(left && !top && right)
                return my_rules[2];
       
        if(left && !top && !right)
                return my_rules[3];
       
        if(!left && top && right)
                return my_rules[4];
       
        if(!left && top && !right)
                return my_rules[5];
       
        if(!left && !top && right)
                return my_rules[6];
       
        // for left=top=right=0
        return my_rules[7];
}

// this is our main function to be called from labtalk
void ca()
{
        MatrixLayer ml = Project.ActiveLayer();
       
        if(ml==NULL)
        {
                out_str("Must have matrix as active window";
                return;
        }
       
        ml.SetInternalData(FSI_BYTE);
        ml.SetNumCols(1000);
        ml.SetNumRows(500);
       
        Matrix        mat(ml);
        int                nCols = mat.GetNumCols();
       
        //---------
        // init 1st row of matrix to be all zero except 1 in the middle
        for(int ii = 0; ii < nCols; ii++)
        {
                mat[0][ii] = 0;
        }
        mat[0][nCols/2] = 1;
        //---------

        int        left,top,right;
       
        // start from 2nd row
        progressBox        show("calculating....";
        show.SetRange(1,mat.GetNumRows());
       
        for(int nR = 1, nRLast=0; nR < mat.GetNumRows(); nR++,nRLast ++)
        {
                if(!show.Set(nR))  // user click cancel
                        break;

                for(int nC = 0; nC < nCols; nC++)
                {
                        left = nC > 0? mat[nRLast][nC-1] : 0;
                        top = mat[nRLast][nC];
                        right = nC < nCols-1? mat[nRLast][nC+1] : 0;
                        mat[nR][nC] = rule(left,top,right);
                }
        }
        ml.SetViewImage(TRUE);
        ml.LT_execute("Z1=0;Z2=1";// this is needed to set Z range to show bitmap as black and white
}

//----------------------------- EOF
回复此楼

» 猜你喜欢

人生的精彩不是拿一副好牌,而是把一副赖牌打成好牌!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

huqifang

金虫 (正式写手)

好文章啊,做凝固模拟的顶起来啊!

好像做凝固模拟的不多啊,两个讨论的人都找不到啊!多谢搂住的法帖
2楼2006-10-16 17:47:00
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 ganchunlei 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见