1.2. First example: The snowflake curve

Figure 1.1. Sequence of structures

Sequence of structures

The snowflake curve is a good starting point to enter the world of rule-based modelling. The swedish mathematician Helge von Koch introduced his construction in 1904; nowadays, this construction is known as the origin of a family of generalized Koch constructions based on the simple rule-based principle of initiator and generator.

Koch's construction starts with a straight line, the initiator. This line is divided into three parts, the middle part then replaced by an equilateral triangle without baseline, see Figure 1.2, “Koch's construction step”. The resulting shape, the generator, consists of four lines, and by repeatedly replacing single lines by this shape, we reproduce Koch's construction.

Figure 1.2. Koch's construction step

Koch's construction step

If we combine three initiator lines to an initial triangle and repeatedly apply Koch's construction on this structure, we get the snowflake curve (Figure 1.1, “Sequence of structures”).

1.2.1. Snowflake curve within GroIMP

In order to see the snowflake curve within GroIMP, click here to load the example and here to apply Koch's construction step.

The description of Koch's construction is coded in the four lines of source code which you can see in the text editor below. They should look like:

public void derivation() [
    Axiom ==> F RU(120) F RU(120) F;
    F ==> F RU(-60) F RU(120) F RU(-60) F;
]

This is the translation of Koch's construction into a relational growth grammar (RGG) programme which can be executed within GroIMP. The source code is written in the RGG dialect of the XL programming language. The relation between these lines and Koch's construction is as follows:

  • RGGs (and L-systems) operate on sets of symbols. The symbols of the example are F, RU and Axiom. In the context of RGGs, symbols are in fact nodes of a graph.

  • To obtain a geometric outcome, RGGs and L-systems use a so-called turtle graphics interpretation of their symbols: The symbol F commands a virtual turtle to move forward and draw a line, the symbol RU leads to a turn by the given angle. Thus, lines in Koch's construction are represented by symbols F in the RGG model. These special symbols are called turtle commands (for a list of all turtle commands, see Chapter 2, Turtle commands and geometric objects).

  • Turtles read symbols from left to right and obey the commands: When a turtle reads the sequence F RU(120) F RU(120) F, it draws three lines with angles of 120° inbeetween, which is an equilateral triangle. The sequence F RU(-60) F RU(120) F RU(-60) F exactly corresponds to the generator of Koch's construction.

  • Now the arrow ==> indicates a replacement rule: All symbols which look like the left hand side of the arrow are replaced by the right hand side. Initially, in every RGG there exists just a single symbol Axiom, so the first rule replaces this initial symbol with a triangle:

        Axiom ==> F RU(120) F RU(120) F;
    

    Koch's construction step is implemented by the second replacement rule:

        F ==> F RU(-60) F RU(120) F RU(-60) F;
    

    This is a straightforward translation of the statement "Replace a line by four lines with certain angles in-between" into the symbolic notation of RGGs.

Both replacement rules lead to the desired development of the structure:

Before first replacement.  The initial structure is Axiom. One cannot see any geometric outcome, because Axiom is invisible.

After first replacement.  The rule Axiom ==> F RU(120) F RU(120) F; has been applied to Axiom, this results in F RU(120) F RU(120) F. The geometry which is produced by this sequence of turtle commands is an equilateral triangle.

After second replacement.  The rule F ==> F RU(-60) F RU(120) F RU(-60) F; has been applied to every F, this results in F RU(-60) F RU(120) F RU(-60) F RU(120) F RU(-60) F RU(120) F RU(-60) F RU(120) F RU(-60) F RU(120) F RU(-60) F. The geometry which is produced by this sequence of turtle commands is a star.

The two rules for the initial structure and the construction step are surrounded by a so-called method declaration:

public void derivation() [
    ...
]

This leads to a menu button with the same name derivation in the RGG toolbar which is displayed in the upper right part of this window. By clicking on this item, the method is invoked, i.e., its contained rules are applied to the structure.

1.2.2. Parametrization

As you may have noticed, the replacement of one line by four lines should come along with a shortening of the lines to one third of the original length. However, this is not yet included in the example: The structure becomes very large after a number of construction steps.

This problem can be solved in a parametrized version of the snowflake RGG: If F(x) stands for a line of length x, we can simply begin with the start word F(1) RU(120) F(1) RU(120) F(1) and write

F(x) ==> F(x/3) RU(-60) F(x/3) RU(120) F(x/3) RU(-60) F(x/3);

This reduces the length of the F segments to one third of their previous length. Now we have created a simple parametric L-system. Click here to load the improved, parametric version. The construction step can be performed by clicking here or on the menu button derivation.

1.2.3. Modification of the model

At this point, you should be able to play with the model:

  1. Modify the source code in the text editor. For example, modify the angles in the contruction step:

        F(x) ==> F(x/3) RU(-30) F(x/3) RU(120) F(x/3) RU(-90) F(x/3);
    

  2. After a modification of source code, you have to save it within the text editor. Within the text editor panel, click on File/Save or the corresponding button in the tool bar of the text editor. This compiles the source code into an internal representation (Java byte-code); if there are errors in the source code, they are displayed within GroIMP.

  3. Invoke methods of the source code in the RGG tool bar which is displayed above the 3D view. Here, the method derivation can be invoked to apply the construction step.

  4. Use the mouse to click on a single line of the snowflake. This brings up the attribute editor, which displays the attributes of the selected line. Modify the length interactively and see how this influences the further development of the model.