Everything that we have covered so far could have been achieved, with perhaps a little more hard work and head-scratching, using grasshopper without scripting. However there are certain kinds of algorithm that cannot be achieved within native grasshopper without the use of scripting or specialist plug-ins.

Perhaps the most prominent category of these algorithms are those which rely on **recursion** in order to generate forms with self-similarity such as fractals, L-systems and so on.

In computer science, ‘recursion’ is a term used to refer to functions whose definition includes a call to itself. For example:

(Whatever you do, don’t try to actually run this snippet – this is an example of **infinite recursion** and it will keep going ‘forever’ – until your computer runs out of memory and crashes!)

When a recursive function is called, it will perform a set of actions which includes calling itself – which will then perform a set of actions which includes calling itself – which will then perform a set of actions which includes calling itself and so on.

This probably seems like a very strange thing to want to do, but it is actually very useful for a large number of applications. For example, when the computer is parsing your code in the first place, it will recursively break each line down into smaller and smaller chunks until it reaches individual commands that it can then understand and execute.

Geometrically it is ideally suited to generative fractal patterns, where the overall pattern contains many smaller copies of itself. In this example we are going to use it to create a simple fractal branching pattern where a line sprouts two more lines from one end, each of which will then sprout two more lines, each of which will then sprout two more lines and so on.

To begin, create a new C# component as well as a line parameter component and three sliders. On the C# component, set up four inputs as follows: firstly an input called ‘ln’ with it’s type hint set to ‘Line’, which will be used to put in our starting line. Connect the line to this. Next, an input called ‘ang’, which will control the angle of our branches and an input called ‘fact’, which will control the length of our branches – both of these should be of type ‘double’ and should be controlled by two of the sliders. Finally, an input called ‘it’ which will control the number of times the fractal structure will branch. This should be an integer type – set the last slider up accordingly and connect it to the input.

Looking at the script, our **RunScript** function should now look something like this:

However to begin with we’re not going to touch** RunScript**. In order to use recursion we’re going to need to define our own function that we can later on call within itself. We’re also going to need somewhere to store our outputs that we can populate from that other function. So, in the white space below **RunScript**, write:

Here we are declaring two things: a list of lines called **outList** and a new subroutine called ‘**Branch**’. Because **outList** is not declared inside a function its scope is not limited to any one function – we can access it from anywhere inside this component.

The Branch subroutine has pretty much the same inputs as our component itself – a Line **ln**, two doubles called **ang** and **fact** and an integer called **it**.

Back in **RunScript**, let’s initialise our **outList** and set it up to be output to **A** and hook up our **Branch** subroutine to the component’s inputs:

Now, when our component starts,** Branch()** will be called. However, it won’t do anything yet – let’s change that.

Let’s begin by making **Branch** generate the new lines that branch out of the original. In your **Branch** function, write the following:

This is a multi-step process, but is really very simple. In order to determine the direction of our branches, we need to find the direction of our starting line. We do this by extracting the front and end points of our line – accessed through the ‘From’ and ‘To’ **properties**. We then subtract the start from the end point to get the vector that describes the direction and length of the line. Points and Vectors have **overloaded operators**, meaning that you can use certain mathematical symbols (+,-,* etc.) with them as if they were numbers, in this case to perform vector mathematics.

From this vector we can now calculate the vectors that will describe the path of our branching lines. We do this by first making a copy of **vAB**, then rotating it by our input angle ‘**ang**’ and then finally by scaling it by our scale factor ‘**fact**’. To rotate the vector we use the built-in **Rotate()** function. This takes in two parameters: the first of which is the angle and the second is the axis about which the rotation will take place. In this case we are only working on the XY plane so we can just rotate about the Z axis – if you wanted to create a 3d branching structure you could do so by replacing this with something else. To represent the Z axis, we create a new vector with components (0,0,1) – i.e. a unit vector pointing directly up along the Z axis.

Now that we have the vectors that provide the direction and size of our branching lines, all we need to do is find the end point of those branches by adding the vector to the end point of our initial line. We can then create a new line between the end point of the old line and the new end-point and we have got our branch. Now all we need to do is add it to our **outList** so that it will be output from the component.

If you close the script, you should see that from our initial line we have created a ‘Y’ shape. However it only branches once no matter what value we put into ‘**it**’ because we have not yet implemented the recursion.

Fortunately, recursion is much easier to implement than it is to understand. All we need to do is to add a call to our branch function at the end of the function itself, passing in our new branches instead of the starting line:

So, now each branch will generate two more branches, each of which will generate two more branches, each of which will generate two more branches… and so on. The problem that we have now is that this will continue infinitely – if you let this script run then it will keep going until you run out of memory and then it will crash your computer. To prevent this from happening we need to stop the recursion after a certain number of branches. Note that when we call **Branch()** in the sample above we are also passing in ‘**it** – 1’ rather than ‘**it**’ – so each time we branch ‘**it**’ in the new function will be one smaller than ‘it’ was in the parent function. We now just need to stop the function from executing once ‘**it**’ reaches zero by wrapping the whole thing in an if statement.

Now by adjusting the slider which controls **it** we can control the number of levels of recursion.

We can control the form of the resulting fractal by adjusting the scale factor and the branching angle to generate a range of different patterns.

Note that this is only one example of what is possible with recursion. You could generate any fractal pattern you like using the same basic structure but changing the geometric operations being performed. Recursion is also a useful tool whenever trying to program any system that needs to branch (whether geometrically or conceptually) – for example parsing text or mathematics, finding connecting paths through a network and so on.

The example file for this session can be downloaded here: