NCF参数化建筑论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 60653|回复: 41
打印 上一主题 下一主题

[网络资源] 元胞自动机脚本

[复制链接]
跳转到指定楼层
1m
发表于 2009-12-25 13:32:13 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
CA in rhinoscript:

网上看到的不错的版本 运行有bug的话 我会帮忙改 (群里叫紫暗)
Option Explicit
'1 dimensional Cellular automata

Call Main()
Sub Main()
      'define the starting condition
     Dim strGen
      strGen = "0,1,0,0,1,0,0,1,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,1,1,0,0,1,1,0,1,1,1,1,0,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0"
      Dim intHowMany : intHowMany = Rhino.GetInteger ("how many generations should I plot?", 20)
      Dim j
      For j=0 To intHowMany

          dim arrTokens : arrTokens = rhino.strtok(strGen, ",")
          Dim i
          For i=0 To Ubound(arrTokens)
              Dim strCurrentChar : strCurrentChar = arrTokens(i)
              Dim arrPoint       : arrPoint       = array(i,j,0)
              'Dim strTextDotID   : strTextDotID   = Rhino.AddTextDot (strCurrentChar, arrPoint)
             Dim strObjectID    : strObjectID    = Rhino.AddSrfPt (array(array((i-.5),(j-.5),0),array((i-.5),(j+.5),0),array((i+.5),(j+.5),0),array((i+.5),(j-.5),0)))         
              if strCurrentChar=0 Then
                 'Call Rhino.ObjectColor ( strTextDotID, RGB(250,250,250) )
                Call Rhino.ObjectColor ( strObjectID,  RGB(250,250,250) )
              Else
                 'Call Rhino.ObjectColor ( strTextDotID, RGB(0,0,0) )
                Call Rhino.ObjectColor ( strObjectID,  RGB(0,0,0) )
              End if
           Next
           strGen = NextGeneration(strGen)
      Next

End Sub



Function NextGeneration (strGen)
     'define 3 variables for each character and define a new string for next generation
    Dim strCharacter, strLeftCharacter, strRightCharacter, strNextGen, arrTokens


     strNextGen = ""
     'tokenize the strGen
    arrTokens = rhino.strtok(strGen, ",")
     Dim i
     For i=0 To Ubound(arrTokens)
         strCharacter      = arrTokens(i)
         
         If i=0 Then
             strLeftCharacter  = arrTokens(Ubound(arrTokens))
         Else
             strLeftCharacter  = arrTokens(i-1)
         End If

         If i=Ubound(arrTokens) Then
             strRightCharacter  = arrTokens(0)
         Else
             strRightCharacter  = arrTokens(i+1)
         End If

         'loop through each of the arrTokens
        'go through the wolfram rules
        'if rule applies change the character
        if strLeftCharacter = 1 AND strCharacter = 1 AND strRightCharacter = 1  Then
               strNextGen = strNextGen & ",0"
         End If

         if strLeftCharacter = 1 AND strCharacter = 1 AND strRightCharacter = 0  Then
               strNextGen = strNextGen & ",0"
         End If

         if strLeftCharacter = 1 AND strCharacter = 0 AND strRightCharacter = 1  Then
               strNextGen = strNextGen & ",0"
         End If
         
         if strLeftCharacter = 1 AND strCharacter = 0 AND strRightCharacter = 0  Then
               strNextGen = strNextGen & ",1"
         End If
         
         if strLeftCharacter = 0 AND strCharacter = 1 AND strRightCharacter = 1  Then
               strNextGen = strNextGen & ",1"
         End If
         
         if strLeftCharacter = 0 AND strCharacter = 1 AND strRightCharacter = 0  Then
               strNextGen = strNextGen & ",1"
         End If
         
         if strLeftCharacter = 0 AND strCharacter = 0 AND strRightCharacter = 1  Then
               strNextGen = strNextGen & ",1"
         End If
         
         if strLeftCharacter = 0 AND strCharacter = 0 AND strRightCharacter = 0  Then
               strNextGen = strNextGen & ",0"
         End If

     
     Next

     'return the new created sting
    NextGeneration = strNextGen
End Function

评分

参与人数 1强度 +3 照度 +50 收起 理由
skywoolf + 3 + 50 精品资源

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享分享
2m
 楼主| 发表于 2010-1-12 23:38:32 | 显示全部楼层
7# iceking
网上的脚本 类似于树的生成
我只关心算法和运用

小黑屋|手机版|NCF参数化建筑论坛 ( 浙ICP备2020044100号-2 )    辽公网安备21021102000973号

GMT+8, 2024-5-17 08:41 , Processed in 0.146569 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表