Simple Recursion Study
This is an initial attempt to build recursive code on Rhino. “Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition; specifically it is defining an infinite statement using finite components.” Perhaps the most interesting definition, “To understand recursion, one must first understand recursion” – Wikipedia.
Wouldn’t take long to realize the potential of recursion in its ability to generate relatively high levels of complexity from relatively few lines of code. Immediate objective of this exercise is to develop subdivision strategies (work in progress) for surfaces. Shown here are the sequential progress of recursive iterations developed starting from a simple rectangular grid of points.
Code is inefficient and unclean, and I am far too lazy to correct it, nevertheless, it works
Option Explicit 'Script written by <karthik.dondeti> 'Script copyrighted by <yet.another.script> 'Script version Wednesday, February 17, 2010 10:48:30 AM Call Main() Sub Main() f_face array(0,0,0),array(0,1,0),array(0,0,0),-90 f_face array(0,0,0),array(1,0,0),array(0,0,0),90 f_face array(0,0,0),array(1,0,0),array(0,0,180),0 arrObjects = rhino.AllObjects rhino.UnlockObjects arrObjects End Sub Function f_face (arrBase,arrAxis,arrMove,angle) Dim arrCrvPt,gridPt,count,index,arrObj Dim strCrv,i,j,iMax,jMax,k,height,n n=10:iMax=n:jMax=n:index=0:height=0 ReDim arrCrvPt(3),gridPt(iMax-1,jMax-1) For i=0 To iMax-1 For j=0 To jMax-1 If i=0 Or j=0 Or i=iMax-1 Or j=jMax-1 Then gridPt(i,j)=array(i*20,j*20) Else gridPt(i,j)= array(i*20+rnd*20,j*20+rnd*20) End If If i>0 And j>0 Then arrCrvPt(0)=gridPt(i,j) arrCrvPt(1)=gridPt(i-1,j) arrCrvPt(2)=gridPt(i-1,j-1) arrCrvPt(3)=gridPt(i,j-1) count = int(rnd*5)+2 f_recurse arrCrvPt,count,index,0 End If Next Next arrObj = rhino.AllObjects (True) arrObj = rhino.SelectedObjects rhino.RotateObjects arrObj,arrBase,angle,arrAxis rhino.MoveObjects arrObj,arrBase,arrMove rhino.LockObjects arrObj End Function Function f_recurse (arrPts,count,ini_index,height) Dim index,strSrf,k index= int(rnd*4) k = (rnd*(1/2))+(1/4) Dim midPt,arrCrv,arrCentro,strCrv,arrCrvPt,i ReDim midPt(3),arrCrv(3),arrCrvPt(3) For i=0 To 3 If i<3 Then arrCrv(i)=rhino.AddCurve (array(arrPts(i),arrPts(i+1))) midPt(i)=array((1-k)*arrPts(i)(0)+k*arrPts(i+1)(0),(1-k)*arrPts(i)(1)+k*arrPts(i+1)(1)) Else arrCrv(i)=rhino.AddCurve (array(arrPts(i),arrPts(0))) midPt(i)=array((1-k)*arrPts(i)(0)+k*arrPts(0)(0),(1-k)*arrPts(i)(1)+k*arrPts(0)(1)) End If Next strCrv = rhino.JoinCurves (arrCrv) strSrf = rhino.ExtrudeCurvestraight (strCrv(0),array(0,0,count-2),array(0,0,count)) rhino.CapPlanarHoles strSrf rhino.MoveObject strSrf,array(0,0,0),array(0,0,height) rhino.DeleteObjects arrCrv arrCentro = rhino.CurveAreaCentroid (strCrv) arrCrvPt(0)=arrCentro(0):arrCrvPt(1)=midPt(index) If index<3 Then arrCrvPt(2)=arrPts(index+1):arrCrvPt(3)=midPt(index+1) Else arrCrvPt(2)=arrPts(0):arrCrvPt(3)=midPt(0) End If count=count-1 height=height+2 If count>0 Then f_recurse arrCrvPt,count,index,height End If End Function
‘Script written by <karthik.dondeti>
‘Script copyrighted by <yet.another.script>
‘Script version Wednesday, February 17, 2010 10:48:30 AMCall Main()
Sub Main()
Dim arrObjects
arrObjects = Rhino.AllObjectsIf IsArray(arrObjects) Then
Call rhino.DeleteObjects (arrObjects)
End Ifrhino.ViewDisplayModeEx, “Wireframe”f_face array(0,0,0),array(0,1,0),array(0,0,0),-90
f_face array(0,0,0),array(1,0,0),array(0,0,0),90
f_face array(0,0,0),array(1,0,0),array(0,0,180),0
arrObjects = rhino.AllObjects
rhino.UnlockObjects arrObjects
Rhino.ViewDisplayModeEx, “ghosted”
End Sub
Function f_face (arrBase,arrAxis,arrMove,angle)
Dim arrCrvPt,gridPt,count,index,arrObj
Dim strCrv,i,j,iMax,jMax,k,height,n
n=10:iMax=n:jMax=n:index=0:height=0
ReDim arrCrvPt(3),gridPt(iMax-1,jMax-1)
For i=0 To iMax-1
For j=0 To jMax-1
If i=0 Or j=0 Or i=iMax-1 Or j=jMax-1 Then
gridPt(i,j)=array(i*20,j*20)
Else
gridPt(i,j)= array(i*20+rnd*20,j*20+rnd*20)
End If
If i>0 And j>0 Then
arrCrvPt(0)=gridPt(i,j)
arrCrvPt(1)=gridPt(i-1,j)
arrCrvPt(2)=gridPt(i-1,j-1)
arrCrvPt(3)=gridPt(i,j-1)
count = int(rnd*5)+2
f_recurse arrCrvPt,count,index,0
End If
Next
Next
arrObj = rhino.AllObjects (True)
arrObj = rhino.SelectedObjects
rhino.RotateObjects arrObj,arrBase,angle,arrAxis
rhino.MoveObjects arrObj,arrBase,arrMove
rhino.LockObjects arrObj
End Function
Function f_recurse (arrPts,count,ini_index,height)
Dim index,strSrf,k
index= int(rnd*4)
k = (rnd*(1/2))+(1/4)
Dim midPt,arrCrv,arrCentro,strCrv,arrCrvPt,i
ReDim midPt(3),arrCrv(3),arrCrvPt(3)
For i=0 To 3
If i<3 Then
arrCrv(i)=rhino.AddCurve (array(arrPts(i),arrPts(i+1)))
midPt(i)=array((1-k)*arrPts(i)(0)+k*arrPts(i+1)(0),(1-k)*arrPts(i)(1)+k*arrPts(i+1)(1))
Else
arrCrv(i)=rhino.AddCurve (array(arrPts(i),arrPts(0)))
midPt(i)=array((1-k)*arrPts(i)(0)+k*arrPts(0)(0),(1-k)*arrPts(i)(1)+k*arrPts(0)(1))
End If
Next
strCrv = rhino.JoinCurves (arrCrv)
strSrf = rhino.ExtrudeCurvestraight (strCrv(0),array(0,0,count-2),array(0,0,count))
rhino.CapPlanarHoles strSrf
rhino.MoveObject strSrf,array(0,0,0),array(0,0,height)
rhino.DeleteObjects arrCrv
arrCentro = rhino.CurveAreaCentroid (strCrv)
arrCrvPt(0)=arrCentro(0):arrCrvPt(1)=midPt(index)
If index<3 Then
arrCrvPt(2)=arrPts(index+1):arrCrvPt(3)=midPt(index+1)
Else
arrCrvPt(2)=arrPts(0):arrCrvPt(3)=midPt(0)
End If
count=count-1
height=height+2
If count>0 Then
f_recurse arrCrvPt,count,index,height
End If
End Function




