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 |