Algorithms

Base-Motif Fractals

Base-motif fractals are formed by repeatedly substituting line segments with a figure called the motif. In order to develop algorithms for the base-motif fractals, we need an efficient way of storing the motif. One of the best ways to do it, which we will use is storing the angles at which the segments of the motif are located. For example, for the following motif, the angles would be 0, 60, 300, 0:

We can easily store the angles in an array. When generating the fractal, we can find the angle of every line segment by adding up the angles corresponding to it location. The location can be found using the digits of the segments number in the corresponding base. For example, if we use the above motif with 2 iterations, we will get the following picture in which we numbered the segments. Suppose we want to find the angle of the 6th segment. Since there are 4 segments in the motif, we use base for. Expressing 6 in base 4, we get 12. We now take the array and add the 1st and 2nd numbers. We get 60 + 360 = 360, which is the correct angle (the numbering in the array starts from 0).

The algorithm below can be used to generate base-motif fractals.

Constants used:

size_of_every_segment: the size of every segment in the motif
number_of_iterations: more is more accurate, but it is limited by of the resolution and speed
array_size: number of segments in the array
motif_array: the array containing the angles of every segment in the motif

``` determine the actual size of each segment
size = size_of_every_segment ^ number_of_iterations
 determine the number of segments
num_of_segments = array_size ^ number_of_iterations
 initialize the angle and coordinates
x = 0
y = 0
angle = 0
 start the iteration
FOR seg = 0 TO num_of_segments - 1
 loop through every digit, calculating the angle
FOR dig = 0 TO number_of_iterations - 1
round = FIX(seg / array_size ^ dig)
digit = round - FIX(round / array_size) * array_size
angle = angle + motif_array(digit)
NEXT dig
 determine the new coordinates of the position
new_x = x + size * COS(angle)
new_y = y + size * SIN(angle)
 connect the previous point with the current one
LINE (x, y) - (new_x, new_y)
 set the new coordinates
x = new_x
y = new_y
NEXT seg```