Objectives

The task was given with the following objectives;

Create a simple L-Systems modeller;

  1. Read a configuration file that contains L-System parameters; firstly initial values for length, angle and number of iterations, and secondly a set of rules to build the tree, including an axiom (initial rule).
  2. Display the resulting L-System, such that use of hotkeys can step through successive iterations of the L-System.

Additionally;

  1. Using hotkeys, have the ability to load at least 4 different config files.
  2. Using hotkeys, have the ability to change at least 3 different parameters of the L-System.

In this project, I achieved these goals, as well as adding some of my own.

Design

When approaching this task, I thought it would be best to break down the task into tasks, and draw up a flowchart of processes in the system.

Read File & Parse Configuration

This should be the first state in the system. The system should start by opening the file and reading it, committing its contents to memory. This will mean that the same file will not need to be read many times over during the creation of the rule, and instead be read from memory.

Create Master String

This part of the program involves creating a very long series of commands, made by expanding the rules in the config file for the number of iterations there are. For each iteration, the program will go through the string and replace rule identifiers for the rule they represent.

So for example;

a: F[+X]F[-X]+X

With the rules;

X = F[+X]F[-X]+X

F = FF

the first identifier F in a will expand and result in;

a1: FF[+X]F[-X]+X

Repeating this for the remainder of the string a will result in;

b: FF[+F[+X]F[-X]+X]FF[-F[+X]F[-X]+X]+ F[+X]F[-X]+X

This results in a very long string that is stored in memory, and will represent a linear series of instructions for the turtle to follow.

Display Tree

In this state, the turtle redraws the tree, using the master thread created in the previous state. The turtle follows the master list of commands and executes them as they are outlined in The Algorithmic Beauty of Plants, (F; move forward, +; turn left, -; turn right, etc). If the turtle sees a command that it doesn’t recognise, because it hasn’t been expanded, it ignores it and moves on through the string.

If angle and length change via the hotkeys, the rule string doesn’t change. The only thing that changes is how the turtle moves. If length is increased, the turtle moves further, if the angle is increased, the turtle turns further. This is why when those parameters are changed the program only goes back to the redrawing tree state.

If the number of iterations changes, then the master rule string must be remade again. The number of rules that must be expanded changes. The system will rebuild the string from scratch again, and expand each rule for the number of iterations requested.

If the file changes, obviously a new file must be read, parsed, and a new string must be created.