Call Main
Sub Main()
'Base point of the grid will be world origin
'Define number of cells in each direction
Dim intNumU: intNumu = Rhino.GetInteger ("Number of Cells in x direction",10,10, 100)
Dim intNumV: intNumV = Rhino.GetInteger ("Number of Cells in y direction",10,10, 100)
'Define number of generatons
Dim intGeneration: intGeneration = Rhino.GetInteger ("Number of generations",50,1, 100)
'Create 2D array of rectangles (of type string) for cells
'We will first generate all cells, then show and hide them depending on each cell state
Dim arr2DCells()
ReDim arr2DCells(intNumU-1, intNumV-1)
'Generate all cells
Call GenerateCells(arr2DCells, intNumU, intNumV)
'Define 2D array of cells states (of type int that takes values 0 and 1)
Dim arr2DStates()
ReDim arr2DStates(intNumU-1, intNumV-1)
'Create the initial state by generating random distribution
Call GenerateRandomState(arr2DStates,intNumU, intNumV)
'Run the Game of Life
Dim i
Dim arrLifeObjs()
Dim arrDeadObjs()
For i = 0 To intGeneration
'Print which generation we are in
Rhino.Print "Generation: " & i 'CStr(i)
'Run the current generation
Call NewGeneration( arr2DStates, intNumU, intNumV )
'Generate single dimention array of alive and dead objects (to be hidden)
Call ExtractLifeList( arr2DCells, arr2DStates, intNumU, intNumV, arrLifeObjs, arrDeadObjs )
'Hide all objects
Call Rhino.HideObjects( arrDeadObjs )
Call Rhino.ShowObjects( arrLifeObjs )
Call Rhino.Redraw()
Call Rhino.Sleep( 500 )
Next
End Sub
'-------------------------------------------------------------
'Generate Calls
'Starting from world origin, create squares of 1 unit width
Sub GenerateCells(arr2DCells, intNumX, intNumY)
Dim i, j
Dim arrV : arrV = Array(0,0,1)
Dim arrCenter
For i = 0 To intNumX-1
For j = 0 To intNumY-1
'Create a circle
arrCenter = Array(2*i,2*j,0)
'Assign object string value To its proper location In the array of cells
arr2DCells(i,j) = Rhino.AddSphere(arrCenter, 1 )
Next
Next
End Sub
'Generate states randomly
'States are either 0 or 1
Sub GenerateRandomState(arr2DStates, intNumX, intNumY)
Dim i, j
For i = 0 To intNumX-1
For j = 0 To intNumY-1
Randomize
arr2DStates(i,j) =Int((1 - 0 + 1) * Rnd) + 0
Next
Next
End Sub
'Run a generaion in the game of life
'Edge condition periodic
Sub NewGeneration( arrStates, intNumX, intNumY )
Dim i,j
Dim intLCount
Dim prev_i, next_i, prev_j, next_j
For i = 0 To (intNumX - 1)
'First index
If i = 0 Then
'Take last index
prev_i = intNumX-1
Else
prev_i = i-1
End If
'Last index
If i = intNumX-1 Then
'Take first index
next_i = 0
Else
next_i = i+1
End If
For j = 0 To (intNumY - 1)
If j = 0 Then
prev_j = intNumY - 1
Else
prev_j = j - 1
End If
'Check next col
If j = intNumY-1 Then
next_j = 0
Else
next_j = j+1
End If
'Zero the living cells count
intLCount = 0
'Count number of live neighbors (8 of them)
'Check top cell
intLCount = intLCount + arrStates( next_i, j )
'Check bottom cell
intLCount = intLCount + arrStates( prev_i, j )
'Check right cell
intLCount = intLCount + arrStates( i, next_j )
'Check left cell
intLCount = intLCount + arrStates( i, prev_j )
'Check top right cell
intLCount = intLCount + arrStates( next_i, next_j )
'Check top left cell
intLCount = intLCount + arrStates( next_i, prev_j )
'Check bottom right cell
intLCount = intLCount + arrStates( prev_i, next_j )
'Check bottom left cell
intLCount = intLCount + arrStates( prev_i, prev_j )
'Check if a cell is live or dead and to
' change/maintain state based On count of live neighbors
If arrStates(i,j) = 1 Then 'Live cell
If intLCount < 2 Then
arrStates(i,j) = 0
End If
If intLCount > 3 Then
arrStates(i,j) = 0
End If
Else 'Dead Cell
If intLCount = 3 Then
arrStates(i,j) = 1
End If
End If
Next
Next
End Sub
'Extract one dimentional array of living and dead objects
Sub ExtractLifeList( arr2DCells, arr2DStates, intNumX, intNumY, arrLifeObjs, arrDeadObjs )
Dim k : k=0
Dim d : d=0
Dim i, j
For i = 0 To intNumX-1
For j = 0 To intNumY-1
'Check if cell is alife
If arr2DStates(i,j) = 1 Then
ReDim Preserve arrLifeObjs(k)
arrLifeObjs( k ) = arr2DCells(i,j)
k = k+1
Else
ReDim Preserve arrDeadObjs(d)
arrDeadObjs( d ) = arr2DCells(i,j)
d = d+1
End If
Next
Next
End Sub |