Basic Branching RhinoScript

branching.jpg

Option Explicit
'Script written by Che-Wei Wang
'Script version Friday, March 14, 2008 11:38:10 AM

Call Main()
Sub Main()
	
	ReDim arrStart(0)
	'set startpoint
	arrStart(0)= array(0,0,0) 
	'call drawBraches Function with initial settings(arrStart startPostition, L initialLength, number of branches, number of generations)
	Call drawBranches(arrStart, 2, 2, 6)

End Sub

Function drawBranches( arrStart(), L, nBranches, generations)
	ReDim arrEndPoints(nBranches)
	Dim k,j	
	Dim arrEnd, strobject, branchCount
	'reset branch count	
	branchCount=0				    
	'for each start point
	For k = 0 To uBound(arrStart) 
		'draw branches
		For j=0 To nBranches-1	
			'endpoint based on startpoint
			arrEnd=array(arrStart(k)(0),arrStart(k)(1)+L,arrStart(k)(2)+rnd*L) 
			'add line
			strObject=rhino.addline(arrStart(k), arrEnd)		
			'rotate line randomly between -30 and 30				
			Call rhino.RotateObject(strObject, arrStart(k), rnd*60-30)	 		
		
			'save each branches endpoint in an array
			ReDim Preserve arrEndPoints(branchCount)			  		 		
			arrEndPoints(branchCount)=	Rhino.CurveEndPoint(strObject)
			'keep a count of the number of branches drawn
			branchCount=branchCount+1								     		
			
		Next
	Next
	
	'exit loop if generations=0
	If generations>0 Then 
		'recursive call with endpoints as startpoints, modified length, number of branches, and generation count-1
		Call drawBranches(arrEndPoints, l*.85, nBranches , generations-1)
	End If				
	
End Function