Fractal Tree 1

In geometry terms, a fractal tree, is one of the easiest to create types of fractals. Each tree is created by splitting a line segment into two smaller segments at the end, and then splitting the two smaller segments as well, and so on, and so on… Fractal trees are distinguished by the angle between concurrent adjacent segments and ratio between lengths of successive segments.

The first fractal tree in this series will be one of the simplest, having the successive length ratio and the angle between the segments as constant values throughout.

A simple fractal tree

The simple fractal tree starts with its ‘trunk’ (initiator) straight upwards with a length of 14. The two successive segments after that has each a length of 8.4 (0.6*14) and a relative angle branching from the trunk at 29° each. This system will continue for each segment until the final iteration.

The first two branches of the main trunk are 8.4 long and rotated 29° in relation to the previous segment
The next iteration. Segments are now 5.04 long
Iterations 1-10

And if we would like it a bit fancier, we can use native Grasshopper component Multipipe in order to have some width to the tree and later add some color to it.

Fractal tree with some width and color added to it

With Grasshopper’s DataTree class it is easy to store each iteration of the fractal tree within a own branch. A DataTree is a hierarchical structure for storing data in nested lists in Grasshopper. However the code might be at first glance a bit hard to read when we are creating code about trees and branches when storing them in DataTrees and Branches…

The Grasshopper UI of the component
private void RunScript(double startLength, double lengthRatio, double degreeRotation, int iterations, ref object fractalTree)
  {
    Point3d startPoint = new Point3d(0, 0, 0);
    Vector3d startVector = new Vector3d(0, 1, 0);
    Vector3d z = new Vector3d(0, 0, 1);

    Line first = new Line(startPoint, startVector, startLength);

    int path = 0;
    GH_Path firstPath = new GH_Path(path);
    DataTree<Line> treeBranches = new DataTree<Line>();
    treeBranches.Add(first, firstPath);

    for(int i = 0;i < iterations;i++)
    {

      List<Line> branchList = treeBranches.Branch(treeBranches.BranchCount - 1);
      path++;
      GH_Path pth = new GH_Path(path);
      foreach(Line branch in branchList)
      {
        Line newBranchLeft = treeBranch(branch, degreeRotation, lengthRatio, z, true);
        Line newBranchRight = treeBranch(branch, degreeRotation, lengthRatio, z, false);
        treeBranches.Add(newBranchLeft, pth);
        treeBranches.Add(newBranchRight, pth);
      }
    }
    fractalTree = treeBranches;
  }

  // <Custom additional code> 
  Line treeBranch (Line Branch, double degreeRotation, double LengthRatio, Vector3d RotationAxis, bool RotationDirection)
  {
    double radianRotation = RhinoMath.ToRadians(degreeRotation);
    Point3d endPoint = Branch.To;
    Vector3d direction = Branch.To - Branch.From;
    if(RotationDirection)
    {
      direction.Rotate(radianRotation, RotationAxis);
    }
    else
    {
      direction.Rotate(-radianRotation, RotationAxis);
    }
    double length = Branch.Length * LengthRatio;
    Line newBranch = new Line(endPoint, direction, length);

    return newBranch;
  }