RhinoScript fractals and recursive subdivision

From KokkugiaWiki

what is recursion

recursion - wikipedia

recursion is where a function is defined within its own definition. typically in algorithmic design this involves a function calling itself to operate on a version of what it has generated. this type of algorithm is typically used in architecture to iteratively subdivide geometry (fractals) or to develop growth algorithms.


Function aRecursiveAct(someThing)

    ' generate a newThing from someThing

    Call aRecursiveAct(newThing)

End Function

fractals + subdivision

Image:Fractal_2d.jpg

the following is a script to subdivide triangles and produce sierpiƄski triangles

Option Explicit

Call recursiveExample()

' sub routine to make input
Sub recursiveExample()
  
  Dim triangle
  
  ' user input - base triangle
  triangle = Rhino.GetObject("select a starting triangle", 4)
  
  ' call the recursive function
  subdivideTriangle triangle
  
End Sub



' function which is recursive
Function subdivideTriangle(triangle)
  
  Dim triangleLength, vertexPts, midPt0, midPt1, midPt2, triangle1, triangle2, triangle3
  
  triangleLength = Rhino.CurveLength(triangle)
  
  If triangleLength > 0.005 Then
    
    ' get the vtx
    vertexPts = Rhino.PolylineVertices(triangle)
    
    ' get the midpoints
    midPt0 = Array( ( vertexPts(0)(0) + vertexPts(1)(0) )/2, (vertexPts(0)(1)+vertexPts(1)(1))/2, (vertexPts(0)(2)+vertexPts(1)(2))/2 )
    midPt1 = Array( ( vertexPts(1)(0) + vertexPts(2)(0) )/2, (vertexPts(1)(1)+vertexPts(2)(1))/2, (vertexPts(1)(2)+vertexPts(2)(2))/2 )
    midPt2 = Array( ( vertexPts(2)(0) + vertexPts(0)(0) )/2, (vertexPts(2)(1)+vertexPts(0)(1))/2, (vertexPts(2)(2)+vertexPts(0)(2))/2 )
  
    ' draw triangles
    triangle1 = Rhino.AddPolyline( Array(vertexPts(0), midPt0, midPt2, vertexPts(0)) )
    triangle2 = Rhino.AddPolyline( Array(midPt0, vertexPts(1), midPt1, midPt0) )
    triangle3 = Rhino.AddPolyline( Array(midPt1, vertexPts(2), midPt2, midPt1) )
    
    ' call this function with the new triangles as input
    subdivideTriangle triangle1
    subdivideTriangle triangle2
    subdivideTriangle triangle3
    
  End If
  
  
End Function


Views
Personal tools