RhinoScript dol-systems

From KokkugiaWiki

DOL-Systems
L-System is an abrreviated term for a Lyndenmayer-System, created by Aristid Lyndenmayer. The example below explores Aristad's DOL-System (deterministic) code structure. You can find his research on these here
L-Systems utilise a string to identify a series of commands that generate geometry. The string contains a number of procedures that through recursion, are substituted in a similar fashion to our previous substitution scripts. L-Systems are most well known for their ability to produce tree-like structures through recursive branching. Before we explore these, we will quickly visit a basic fractal substitution script that utilises Aristad's DOL-System syntax.
Image:Kotchcompiled.jpg koch curve: generation #0 generation #1 generation #2 generation #3

Option explicit
'------------------------------------------------------------------------------
' Koch Curve algorithm based on Aristid Lyndenmayer's DOL-Sytem syntax    	
' Author: Rob Stuart-Smith | 2008 | www.kokkugia.com
'------------------------------------------------------------------------------
Call DOL_LSystem()

Sub DOL_LSystem()
	

	Dim initialString  : initialString = "F,-F,-F,-F"
	Dim generatorString  : generatorString = "F,+F,F,-F,F,-F,-F,+F,+F,F,-F,-F,+F,+F,F,+F,F,-F" 

	Dim numberOfGenerations : numberOfGenerations = 1

	Dim i,j,k
	Dim a,d
	Dim strObj, arrObj
	Dim arrTokens, arrObjTemp
	Dim m,n
	Rhino.EnableRedraw False
	d = 1/6
	a = 90.0
	Rhino.AddLine Array(0,0,0), Array(0,100, 0)

	ReDim arrObj(0)
	arrObj(0) = Rhino.FirstObject

	For j=0 To numberOfGenerations
		
		If j = 0 Then
			arrTokens = Rhino.Strtok (initialString, ",")
		Else
			arrTokens = Rhino.Strtok (generatorString, ",")
		End If

		For m = 0 To Ubound(arrObj)
			n = 0
			strObj = arrObj(m)
			For k=0 To Ubound(arrTokens)
				'print arrTokens(k)
				Select Case arrTokens(k)
					Case "F" 						
						Call addCurve(strObj,n,d,0.0)
						strObj = Rhino.FirstObject				
					Case "-F"		
						Call addCurve(strObj,n,d,-a)
						strObj = Rhino.FirstObject					
					Case "+F"
						Call addCurve(strObj,n,d,a)
						strObj = Rhino.FirstObject
				End Select
				
			Next

		Next

		arrObjTemp = Rhino.AllObjects
		ReDim arrObj(Ubound(arrObjTemp))
		arrObj = arrObjTemp
	Next
	Rhino.EnableRedraw True
End Sub

Function addCurve(strObj,n,d,a)
	Dim arrPt1, arrPt2, arrPt3, angle, arrAxis
	Dim arrVec1,arrVec2,strNewObj
	
	arrPt1 = Rhino.CurveStartPoint(strObj)
	arrPt2 = Rhino.CurveEndPoint(strObj)	
	arrVec1 = Rhino.VectorCreate(arrPt2,arrPt1)
	arrVec2 = Rhino.VectorScale(arrVec1,d)

	If n = 0 Then
		Rhino.DeleteObject(strObj)
		n = n + 1
		arrPt3 = Array(arrPt1(0)+ arrVec2(0),arrPt1(1) + arrVec2(1), arrPt1(2) + arrVec2(2))
		Rhino.AddLine arrPt1, arrPt3
		strNewObj = Rhino.FirstObject
		Rhino.RotateObject strNewObj, arrPt1,a, ,False
	Else
		n = n + 1
		arrPt3 = Array(arrPt2(0)+ arrVec1(0),arrPt2(1) + arrVec1(1), arrPt2(2) + arrVec1(2))
		Rhino.AddLine arrPt2, arrPt3
		strNewObj = Rhino.FirstObject
		Rhino.RotateObject strNewObj, arrPt2,a, ,False
	End If

End Function
Views