Recursion Scripts

RhinoCommon Code

Random Branching

Implemented in a Grasshopper VB.NET component.

Recursion2SMALL.jpg
  Private Sub RunScript(ByVal P As Point3d, ByVal N As Integer, ByVal G As Integer, ByVal S As Integer,  ByRef A As Object) 

    Dim LineList As New List(Of Line)
    Dim LocList As New  List(Of Point3d)
    Dim MyRandom As New Random(S)

    LocList.Add(P)

    'move range
    Dim x0 As Double = -10
    Dim x1 As Double = 10
    Dim y0 As Double = -10
    Dim y1 As Double = 10
    Dim z0 As Double = -10
    Dim z1 As Double = 10

    For i As Integer = 0 To G
      Dim NewLocList As New List(Of Point3d)
      For j As Integer = 0 To LocList.Count - 1
        Dim location As Point3d
        For k As Integer = 0 To N - 1

          location = LocList(j)
          Dim move As Point3d = New Point3d(MyRandom.Next(x0,x1), MyRandom.Next(y0,y1), MyRandom.Next(z0,z1))

          Dim oldlocation As Point3d = location
          location = location + move

          Dim myline As Line = New Line(oldlocation, location)
          LineList.Add(MyLine)

          NewLocList.Add(location)

        Next
      Next

      'move decay
      x0 = x0 * 0.7
      x1 = x1 * 0.7
      y0 = y0 * 0.7
      y1 = y1 * 0.7
      z0 = z0 * 0.7
      z1 = z1 * 0.7

      LocList = NewLocList

    Next

    A = LineList
  End Sub

Triangle Subdivision

Implemented in a Grasshopper VB.NET component.

RecSRF2.jpg
Version 1
  Private Sub RunScript(ByVal T As Curve, ByVal N As Integer, ByRef A As Object) 

    Dim triList As New List (Of NurbsCurve)
    Dim tri As NurbsCurve = t.ToNurbsCurve
    trilist.Add(tri)

    For i As Integer = 0 To N - 1
      Dim newTriList As New List(Of NurbsCurve)
      For j As Integer = 0 To trilist.Count - 1

        Dim verts As Rhino.Geometry.Collections.NurbsCurvePointList = triList(j).Points
        Dim cptA As Point3d = verts(0).location
        Dim cptB As Point3d = verts(1).location
        Dim cptC As Point3d = verts(2).location

        Dim lnA As New Line(cptA, cptB)
        Dim lnB As New Line(cptB, cptC)
        Dim lnC As New Line(cptC, cptA)

        Dim mptA As Point3d = lnA.PointAt(0.5)
        Dim mptB As Point3d = lnB.PointAt(0.5)
        Dim mptC As Point3d = lnC.PointAt(0.5)

        TriSubDiv(mptA, mptB, mptC, mptA, newTriList)
        TriSubDiv(mptB, mptC, cptC, mptB, newTriList)
        TriSubDiv(mptA, mptB, cptB, mptA, newTriList)
        TriSubDiv(mptC, mptA, cptA, mptC, newTriList)

      Next
      trilist = newTriList
    Next

    A = trilist

  End Sub 

  Sub TriSubDiv (ByRef ptA As Point3d, ByRef ptB As Point3d, ByRef ptC As Point3d, ByRef ptD As Point3d, ByRef TriList As List (Of NurbsCurve))
    Dim pts As New List (Of Point3d)
    pts.Add(ptA)
    pts.Add(ptB)
    pts.Add(ptC)
    pts.Add(ptA)

    Dim tri As PolylineCurve
    tri = NurbsCurve.CreateControlPointCurve(pts, 1)
    TriList.Add(tri.ToNurbsCurve)

  End Sub
Version 2 (Sierpinski)
  Private Sub RunScript(ByVal T As Curve, ByVal N As Integer, ByRef A As Object, ByRef B As Object) 

    Dim triList As New List (Of NurbsCurve)
    Dim triListB As New List (Of NurbsCurve)
    Dim tri As NurbsCurve = t.ToNurbsCurve
    trilist.Add(tri)

    For i As Integer = 0 To N - 1
      Dim newTriList As New List(Of NurbsCurve)
      For j As Integer = 0 To trilist.Count - 1

        Dim verts As Rhino.Geometry.Collections.NurbsCurvePointList = triList(j).Points
        Dim cptA As Point3d = verts(0).location
        Dim cptB As Point3d = verts(1).location
        Dim cptC As Point3d = verts(2).location

        Dim lnA As New Line(cptA, cptB)
        Dim lnB As New Line(cptB, cptC)
        Dim lnC As New Line(cptC, cptA)

        Dim mptA As Point3d = lnA.PointAt(0.5)
        Dim mptB As Point3d = lnB.PointAt(0.5)
        Dim mptC As Point3d = lnC.PointAt(0.5)

        TriSubDiv(mptA, mptB, mptC, mptA, trilistB)
        TriSubDiv(mptB, mptC, cptC, mptB, newTriList)
        TriSubDiv(mptA, mptB, cptB, mptA, newTriList)
        TriSubDiv(mptC, mptA, cptA, mptC, newTriList)

      Next
      trilist = newTriList
    Next

    A = trilist
    B = trilistB

  End Sub 

  Sub TriSubDiv (ByRef ptA As Point3d, ByRef ptB As Point3d, ByRef ptC As Point3d, ByRef ptD As Point3d, ByRef TriList As List (Of NurbsCurve))
    Dim pts As New List (Of Point3d)
    pts.Add(ptA)
    pts.Add(ptB)
    pts.Add(ptC)
    pts.Add(ptA)

    Dim tri As PolylineCurve
    tri = NurbsCurve.CreateControlPointCurve(pts, 1)
    TriList.Add(tri.ToNurbsCurve)

  End Sub

License

The code and components on this page are free software: you can redistribute them and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

http://www.gnu.org/licenses/.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License