We’ve read our data, and set up the parameters. The data structures are working according to our debug reports, and we have dipped our toe into recursion. Now to pull it all together and create some shapes. Since we have parent/child relationships, the idea here is that the parent shape will be big enough to accommodate all its children, grandchildren and so on. This is where recursion will be needed, to predict the height a shape is going to be.

What does it look like

Well- not too good. But notice that all the shapes including the frame are the right height. The starting points and widths we still need to work on – but that will be the subject of the next section. Lets look at how recursion has helped to calculate the heights of each shape, and the processing order ‘ me, then my children’  has ensured that the zOrder (what goes on top), is correct.
There it is. Needs a bit of work, but most of the tricky stuff is done. Lets start by looking at the height recursion

Calculating the height of each shape

To make this simpler, i have defined some properties that are calculated for each shape, using parameters taken from the parameter sheet as a base. These are combined using options from the parameter sheet to decide the height of any given shape.

The code for these properties is as follows – the properties paramXXXX that are referenced are taken from the parameter sheet, the full code will be at the end of this section as usual. The interesting ones from a recursion perspective are

  • myExpansion – calls the recursive function biggestBranch(), whose purpose is to work down the layers of children from here to see the largest number of children any of my chidren have. The reason is that we have provided an option to suppress expansion of a parent if there is only one child. But this can only apply if none of the children have more than one child.
  • mySpace- calls itself until the chain of children is completed. This is because mySpace for any item is in fact the space needed for all its children and all its childrens’ children and so on. Since the frame is treated as shape, the same way as all the others, mySpace for the frame is calculated to be the space needed for all the shapes, their children and so on.
This is a tricky concept, so read the code over and make sure you get it.

Making the shapes

Once we have the ability to figure out the height of any given shape, all thats left now (aside from scaling and formatting) is deciding where to plot them. Again we have to do this recursively because we have to plot all our children, childrens’ children etc, before we can move on to the next shape and know where to start plotting it.
Here is the code below. Notice that we create our own shape of myShapeHeight  – which itself calls the recursive mySpace to figure out the space needed for the parent, then we call our selves for each of the children.

Next steps

The next recursion topic will be sorting. Sorting is a classic instance of recursion. Our roadmapper is going to need the capability to sort the items in various ways to allow the most effective presentation. In the next section, we will cover how to sort a collection, using recursion.

Complete code so far – version 4

The code is getting rather large now. You can replace the Roadmapper module and the cShapeContainer class with the code below, or go to the downloadable items page and pick up version 4.
RoadMapper Module
cShapeContainer Class
For help and more information join our community,  follow the blog or  follow me on Twitter.