Using Inkscape to create fractals



This is a short tutorial to show how Inkscape 0.47 can be used to create simple fractal images using the Lindenmeyer system (L-system) extension included with Inkscape. A fractal is an object that is made from smaller copies of itself. Fractal shapes often occur in nature. For example, a small piece of a fern leaf often looks quite similar to the entire leaf.
In this tutorial, we will Inkscape to construct some simple fractals. The fractals we'll make are all deterministic (non-random) and will all consist of a single curve. That is, the fractal can be drawn on a piece of paper without lifting your pen off the paper. L-system fractals can also be used to create more advanced fractals, but we'll only consider simple fractals in this tutorial.

Let's get started. Open Inkscape and select Extensions then Render then L-system...



An L-sysem menu will open showing multiple options.



Axiom
A mathematical description of the fractal that will be drawn. The axiom is defined by the rules.
Rules
The mathematical rules that define the fractal.
Order
Fractals are defined in terms of copies of themselves. The order describes how many copies of the fractal to make.
Step length (px)
Fractals are drawn using short line segments. The step length defines the length of each line segment.
Randomize step (%)
Defines how often we randomize the angle. In this tutorial, this parameter will always be zero.
Left angle
The number of degrees to rotate to the left when a "-" sign is encountered in the rules.
Right angle
The number of degrees to rotate to the right when a "+" sign is encountered in the rules.
Randomize angle
The randomization angle. In this tutorial, this parameter will always be zero.


Let's start by making our first fractal: the Koch curve. This is a deceptively simple fractal, but will help you get started in making more advanced fractals. The Koch curve is made from four copies of itself. We can see this clearly illustrated in the figure below: the complete Koch curve is made from the black Koch curve, the red Koch curve, the green Koch curve, and the blue Koch curve. We can define the entire Koch curve by defining the relationships between the four pieces of the curve.



Let's name the rule defining the Koch curve "F" (for Fractal). The black Koch curve is identical to the original Koch curve (although smaller). The red Koch curve is identical to the black Koch curve, but rotated by 60 degrees to the left (counter-clockwise). The green Koch curve is identical to the red Koch curve, but rotated 60 degrees to the right of the black curve, which is 120 degrees to the right of the red Koch curve. The blue Koch curve is identical to the black curve, so it is rotated 60 degrees to the left of the green curve. Therefore, the rule to make the Koch fractal is

F=F-F++F-F



where "-" means rotate to the left by 60 degrees and "+" means rotate to the right by 60 degrees. Note that the angle of rotation is always relative to the part of the fractal immediately preceding the current part of the fractal. Thus, the green fractal is oriented 120 degrees (two 60 degree rotations) to the right of the red fractal.

We can now define the fractal by entering the following parameters:

Axiom: F
Rules: F=F-F++F-F
Order: 5
Step length: 2
Randomize step: 0
Left angle: 60
Right angle: 60
Randomize angle: 0


Click the Apply button to view the fractal. You may want to move the L-system menu window off to the side so you can see the fractal. Note that your fractal will be oriented in a vertical direction rather than a horizontal direction. Just click on the rotate icon in the Inkscape menu bar to rotate the fractal.
You can increase the amount of detail in the fractal by increasing the order, but this will also exponentially increase both the size of the fractal as well as the amount of compute time and memory required to render the fractal. If you're not sure about the order, start with a small number, like 3, then slowly increment the order until you have drawn the fractal with the level of detail you need.

Let's try to make a somewhat more complicated fractal: a Sierpinski triangle.



At first glance, it appears that the Sierpinski triangle is made from three identical copies of itself. However, we want to construct the triangle using a single curve, and it isn't possible to draw such a curve using three identical copies of the same curve. Look at triangle A: it is actually a curve that starts from the lower left corner and extends to the upper left corner. This means that we need to draw the red triangle, then the green triangle, and finally the blue triangle. However, the red triangle lies to the left of the drawing path, while the green triangle lies to the right of the drawing path. Looking at triangle A, we see that it lies to the right of its drawing path, while triangle B lies to the left of its drawing path. Thus, we need two sets of rules to draw the Sierpinski triangle:

A = B-A-B; B = A+B+A



with an axiom of either "A" or B" to create the fractal.

One more example: a fractal snowflake. The snowflake is shown in the figure below rendered using three different orders, each designated by the appropriate value of n. When the order is 3, you can see a pretty good representation of the snowflake fractal.



At first glance, the snowflake is made from seven copies of itself. However, each copy is a single curve, and the curves must each lead from one to the other. In fact, there are four different versions of each snowflake. Snowflake A leaves at an angle of 60 degrees to the right from the entering angle. Snowflake B leaves at an angle of 60 degrees to the left; snowflake C leaves at an angle of 120 degrees to the right, and snowflake D leaves at an angle of 120 degrees to the left. Having defined these directions, we now need rules to define each snowflake in terms of the others. This is done, as shown in the illustration above, by breaking each snowflake into seven smaller copies and then connecting the seven pieces together.
One cryptic step in these rules involves the final rotations for rules C and D. These final rotations are needed because the final orientation reached at the end of the rules points in the wrong direction. That is to say, without the final rotation, the rules would get you to the right spot to draw the next part of the snowflake, but the rules won't necessarily point you in the right direction.

Once you've created a fractal, you can then treat it like any other graphical object in Inkscape. For example, you can use the cut-out technique from Screencasters episode 98 together with three connected Koch curves to create the fancy snowflake image below.



The basic idea is to subtract the fractal from a white rectangle, then subtract the same fractal from a slightly smaller black rectangle. Blur the black rectangle and place it behind the white rectangle. The hole in the white rectangle lets you see the blurred black rectangle, which looks like a shadow when slightly offset from the white rectangle.

L-system fractals can create many more types of fractals than the ones you have seen here. For example, it's possible to create fractals with multiple lines. It's also possible to memorize a point on the fractal path and then later return to that memorized point. For example, the following rules will generate a Penrose tiling, which is not fractal but is also not a periodic structure:
Axiom
[N]++[N]++[N]++[N]++[N]
Rules
M=OA++PA----NA[-OA----MA]++;
N=+OA--PA[---MA--NA]+;
O=-MA++NA[+++OA++PA]-;
P=--OA++++MA[+PA++++NA]--NA;
A=

Order
4
Step length (px)
30
Randomize step (%)
0
Left angle
36
Right angle
36
Randomize angle
0