rvb Life - 2D

rvb animation snippet example

A short appliication of Conway's game of life applied to Rhino. Populate the space with cells and watch them grow as time passes. This is an introduction to algorithmic design, where objects interact with one another, and can respond to their boundary conditions in real time.

Option explicit

'Script by archiologics · www.archiologics.com

'original author: ivo stotz

 

Call Life()

Function Life()

Dim arrField(99,99), Field1, Field2

    Dim i, j, FielDimension, nCount

    Dim gen

FielDimension = 99

Rhino.Print "Calculating Field"

For i = 0 To FielDimension

        For j = 0 to FielDimension

arrField(i,j) = 0

        Next

Next

 

Rhino.Print "Initializing Field"

Call init(arrField)

Rhino.CurrentLayer "Life"

Rhino.LayerColor vBRed

    Field1 = arrField

    Field2 = Field1

    Call DrawField(Field1, FielDimension)

Rhino.Print "Iterating"

For gen = 0 To 600

        call iter(field1, field2, FielDimension)

        call iter(field2, field1, FielDimension)

    Next

End function

 

Function CountNeighbors(x, y, byref arrField, FielDimension)

    Dim Count, i, j

    Count = 0

    If x = 0 Then

        If y = 0 then

           If arrfield(FielDimension, FielDimension) = 1 then Count = Count + 1

           If arrfield(x+1,           FielDimension) = 1 then Count = Count + 1

If arrfield(x,             FielDimension) = 1 Then Count = Count + 1

           If arrfield(FielDimension, y+1)           = 1 then Count = Count + 1

           If arrfield(x+1,           y+1)           = 1 then Count = Count + 1

           If arrfield(x,             y+1)           = 1 then Count = Count + 1

           If arrfield(FielDimension, y)             = 1 then Count = Count + 1

           If arrfield(x+1,           y)             = 1 then Count = Count + 1

        Elseif y = FielDimension then

           If arrfield(0,             0)               = 1 then Count = Count + 1

           If arrfield(1,             0)               = 1 then Count = Count + 1

           If arrfield(FielDimension, 0)               = 1 then Count = Count + 1

           If arrfield(0,             FielDimension-1) = 1 then Count = Count + 1

           If arrfield(1,             FielDimension-1) = 1 then Count = Count + 1

           If arrfield(FielDimension, FielDimension-1) = 1 then Count = Count + 1

           If arrfield(1,             FielDimension)   = 1 then Count = Count + 1

           If arrfield(FielDimension, FielDimension)   = 1 then Count = Count + 1

        Else

           If arrfield(0,             y-1) = 1 then Count = Count + 1

           If arrfield(1,             y-1) = 1 then Count = Count + 1

           If arrfield(FielDimension, y-1) = 1 then Count = Count + 1

           If arrfield(1,             y)   = 1 then Count = Count + 1

           If arrfield(FielDimension, y)   = 1 then Count = Count + 1

           If arrfield(0,             y+1) = 1 then Count = Count + 1

           If arrfield(1,             y+1) = 1 then Count = Count + 1

           If arrfield(FielDimension, y+1) = 1 then Count = Count + 1

        End If

    ElseIf x = FielDimension then

        If y = 0 then

           If arrfield(0,             0)               = 1 then Count = Count + 1

           If arrfield(1,             0)               = 1 then Count = Count + 1

           If arrfield(FielDimension, 0)               = 1 then Count = Count + 1

           If arrfield(0,             FielDimension-1) = 1 then Count = Count + 1

           If arrfield(1,             FielDimension-1) = 1 then Count = Count + 1

           If arrfield(FielDimension, FielDimension-1) = 1 then Count = Count + 1

           If arrfield(1,             FielDimension)   = 1 then Count = Count + 1

           If arrfield(FielDimension, FielDimension)   = 1 then Count = Count + 1

        Elseif y = FielDimension then

           If arrfield(0,               0)               = 1 then Count = Count + 1

           If arrfield(FielDimension-1, 0)               = 1 then Count = Count + 1

           If arrfield(FielDimension,   0)               = 1 then Count = Count + 1

           If arrfield(0,               FielDimension-1) = 1 then Count = Count + 1

           If arrfield(FielDimension-1, FielDimension-1) = 1 then Count = Count + 1

           If arrfield(FielDimension,   FielDimension-1) = 1 then Count = Count + 1

           If arrfield(0,               FielDimension)   = 1 then Count = Count + 1

           If arrfield(FielDimension-1, FielDimension)   = 1 then Count = Count + 1

        Else

           If arrfield(x-1, y+1) = 1 then Count = Count + 1

           If arrfield(x,   y+1) = 1 then Count = Count + 1

           If arrfield(0,   y+1) = 1 then Count = Count + 1

           If arrfield(x-1, y-1) = 1 then Count = Count + 1

           If arrfield(x,   y-1) = 1 then Count = Count + 1

           If arrfield(0,   y-1) = 1 then Count = Count + 1

           If arrfield(x-1, y)   = 1 then Count = Count + 1

           If arrfield(0,   y)   = 1 then Count = Count + 1

        End If

    Else

        If y = 0 then

            If arrfield(x-1, y+1)             = 1 then Count = Count + 1

            If arrfield(x,   y+1)             = 1 then Count = Count + 1

            If arrfield(x+1, y+1)             = 1 then Count = Count + 1

            If arrfield(x-1, FielDimension)   = 1 then Count = Count + 1

            If arrfield(x,   FielDimension)   = 1 then Count = Count + 1

            If arrfield(x+1, FielDimension)   = 1 then Count = Count + 1

            If arrfield(x-1, y)               = 1 then Count = Count + 1

            If arrfield(x+1, y)               = 1 then Count = Count + 1

        ElseIf y = FielDimension then

            If arrfield(x-1, 0)     = 1 then Count = Count + 1

            If arrfield(x,   0)     = 1 then Count = Count + 1

            If arrfield(x+1, 0)     = 1 then Count = Count + 1

            If arrfield(x-1, y-1)   = 1 then Count = Count + 1

            If arrfield(x,   y-1)   = 1 then Count = Count + 1

            If arrfield(x+1, y-1)   = 1 then Count = Count + 1

            If arrfield(x-1, y)     = 1 then Count = Count + 1

            If arrfield(x+1, y)     = 1 then Count = Count + 1

        Else

            If arrfield(x-1, y+1)   = 1 then Count = Count + 1

            If arrfield(x,   y+1)   = 1 then Count = Count + 1

            If arrfield(x+1, y+1)   = 1 then Count = Count + 1

            If arrfield(x-1, y-1)   = 1 then Count = Count + 1

            If arrfield(x,   y-1)   = 1 then Count = Count + 1

            If arrfield(x+1, y-1)   = 1 then Count = Count + 1

            If arrfield(x-1, y)     = 1 then Count = Count + 1

            If arrfield(x+1, y)     = 1 then Count = Count + 1

        End If

    End If    

    CountNeighbors = count

End Function

 

Function LiveOrDie(state,N)

    If State = 1 then

        If N < 2 Then

            LiveOrDie = 0

            Exit Function

        ElseIf N > 3 Then

            LiveOrDie = 0

            Exit Function

        Else

            LiveOrDie = 1

            Exit Function

        End if

    Else

        If N = 3 then

            LiveOrDie = 1

            Exit Function

        End If

    End If

End Function

 

Function DrawField(byref Field,FielDimension)

    Dim i,j, arrVrtx(3), face(0)

Call Rhino.EnableRedraw(False)

 

    For i = 0 to FielDimension

        For j = 0 to FielDimension

            If Field(i,j) = 1 Then

                'Rhino.AddPoint Array(i,j,0)

                arrVrtx(0) = array(i,j,0)

                arrVrtx(1) = array(i+1,j,0)

                arrVrtx(2) = array(i+1,j+1,0)

                arrVrtx(3) = array(i,j+1,0)

                Face(0) = array(0,1,2,3)

                call Rhino.AddMesh(arrVrtx, Face)

            End if

        Next

    Next

Call Rhino.EnableRedraw(True)

End Function

 

Function init(ByRef arrField)

CreatePts()

    Dim arrobs, pt, i, strOb

arrobs = rhino.GetObjects("SelectPoints",0)

Rhino.HideObjects arrObs

For Each strOb In arrobs

pt = Rhino.PointCoordinates(strOb)

arrField(pt(0),pt(1)) = 1

Next

End Function

 

Function iter(byref Field1, Byref Field2, FielDimension)

    Dim i,j, liv, N

    For i = 0 to FielDimension

        For j = 0 to FielDimension

            N = CountNeighbors(i,j,Field1,FielDimension)

            liv = LiveOrDie(Field1(i,j),N)

            If liv = 1 then

                Field2(i,j) = 1

            Else

                Field2(i,j) = 0

            End If

        Next

    Next

    Call DrawField(Field2, FielDimension)

End Function

 

Function PtsToInt

Dim arrPts : arrPts = Rhino.GetObjects("Pts",0)

Dim arrPt

For Each arrPt In arrPTs

Dim pt0, pt1,pt2

Dim arrPtc : arrPtC = Rhino.PointCoordinates (arrPt)

pt0 = Round(arrPtc(0))

pt1 = Round(arrPtc(1))

pt2 = Round(arrPtc(2))

Rhino.MoveObject arrPt,arrPtc,array(pt0,pt1,pt2)

Next

End Function

 

Function CreatePts

Rhino.EnableRedraw False

Dim i,j

For i = 0 To 99

For j=0 To 99

If Rnd >0.85 Then

Rhino.AddPoint array(i,j,0)

End If

Next

Next

Rhino.EnableRedraw True

End Function

 

Search Site




archi·o·logics survey

I am interested in: