24小时热门版块排行榜    

查看: 645  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 化工270求调剂 +9 什么名字qwq 2026-03-02 9/450 2026-03-02 19:31 by caszguilin
[考研] 0805总分292,求调剂 +9 幻想之殇 2026-03-01 9/450 2026-03-02 19:28 by caszguilin
[考研] 275求调剂 +7 明远求学 2026-03-01 7/350 2026-03-02 19:22 by zhukairuo
[考研] 085600 英一数二272求调剂 5+6 vida_a 2026-03-01 16/800 2026-03-02 19:13 by zhukairuo
[考研] 289求调剂 +8 yang婷 2026-03-02 9/450 2026-03-02 19:08 by zhukairuo
[考研] 中国科学技术大学材料与化工281求调剂,有科研和获奖经历 +4 wsxw 2026-03-02 4/200 2026-03-02 18:43 by caszguilin
[考研] 299求调剂 +4 kkcoco25 2026-03-02 4/200 2026-03-02 18:04 by barlinike
[考研] 清华大学 材料与化工 353分求调剂 +4 awaystay 2026-03-02 5/250 2026-03-02 17:33 by yeahyou
[考研] 一志愿华中科技大学,化学专业344分,求调剂 +3 邢xing1 2026-03-02 3/150 2026-03-02 17:32 by houyaoxu
[考研] 303求调剂 +5 今夏不夏 2026-03-01 5/250 2026-03-02 15:01 by 向上的胖东
[考研] 26考研报考西工大材料308分求调剂 +4 weizhong123 2026-03-01 4/200 2026-03-02 12:46 by 无际的草原
[基金申请] 成果系统访问量大,请15分钟后再尝试。由此给您造成的不便,敬请谅解。 +5 xhuama 2026-03-02 5/250 2026-03-02 12:34 by stidwellNK
[考研] 哈工大计算机刘劼团队招生 +4 hit_aiot 2026-03-01 6/300 2026-03-02 11:53 by 一声问好
[考研] 调剂 +3 13853210211 2026-03-02 4/200 2026-03-02 10:16 by 13853210211
[考研] 322求调剂 +3 熊境喆 2026-03-01 3/150 2026-03-02 08:44 by houyaoxu
[考研] 291分工科求调剂 +9 science饿饿 2026-03-01 10/500 2026-03-01 18:55 by 18137688336
[考研] 290求调剂 +9 材料专硕调剂; 2026-02-28 11/550 2026-03-01 17:21 by sunny81
[考研] 求调剂 +6 repeatt?t 2026-02-28 6/300 2026-03-01 14:37 by Sakura绘
[考研] 317一志愿华南理工电气工程求调剂 +6 Soliloquy_Q 2026-02-28 11/550 2026-03-01 11:14 by 歌liekkas
[考研] 085600材料工程一志愿中科大总分312求调剂 +8 吃宵夜1 2026-02-28 10/500 2026-02-28 20:27 by L135790
信息提示
请填处理意见