Creative Coding for Surpac - Fractal Trees

Hi Everyone and welcome to 2025!  

Last year we looked at some TCL/SCL basics and covered a lot of ground in a 10 part macro series.  If you haven't seen those, I highly recommend you take a look.  This year we will be looking at some creative coding examples to produce some visually interesting outcomes and in the process, discover new ways to work with TCL/SCL.

Part of the reason I code is to explore the many and varied algorithms that have been discovered and implemented in software over the years. Each time I learn a new algorithm or concept, I find that it unlocks another part of my brain to think in different ways to solve otherwise unrelated problems.  I'm hoping that this creative coding series will do the same for you.

Today's post will be about fractal trees.  You can easily search what these are, so I won't bloat the contents of this post by explaining the details, but here is a short summary in case you've never heard of them before.

Fractal trees fall under a larger concept called algorithmic botany, whereby certain algorithms can be used to describe plant growth. The example attached and shown below is based on a L-System (Lindenmayer System) however doesn't follow the symbolic path that the typical L-System uses.  (I will cover that in a subsequent post.) 

What this example does cover; 

1.Recursive Branch Generation using TCL:

The procedure createBranch creates two new branches (branchA and branchB) for each segment of the tree, akin to an L-system's production rules. These new branches are based on scaling and rotation parameters (lengthMultA, lengthMultB, rotationA, rotationB).
 

2. Iterative Growth using TCL for loops and procedures:

The for loop incrementally generates "generations" of branches, similar to how L-systems apply production rules iteratively to grow the structure.
 

3. Parameterized Rules from a GUIDO form:

Parameters like rotationA, rotationB, and length multipliers dictate how each branch splits and scales, which is typical in L-systems for controlling the complexity and appearance of the fractal.
 

4. Data Representation using SCL Commands:

The tree is represented as a list of segments (treebranchList), where each segment's start and end coordinates are stored and used for the next generation. This corresponds to an L-system's string of symbols or geometric instructions.

The output in Surpac will be similar to this;

I have attached a macro and styles file so you can experiment and see the code used to create fractal trees.  If you manage to create your own fractal tree, please leave a comment and attach the tree for others to enjoy.  (Note, styles are very important for this to display well in Surpac) 

 

If you have any questions about the code, please let me know in the comments as well.