The purpose of the formula section is to define the fractal equation that we use to actually create an image. Before we discuss the format of the formula section, here is an overview of how fractal images are generated:
The mapping section establishes a relationship between a region in the complex plane and an image size in pixels. As an example, consider this mapping:
mapping { (-2, -2, 2, 2) => (100, 100) }
The resulting image will be 100 pixels by 100 pixels so a total of 10,000 pixels will need to be assigned a color to generate the image. It is the job of the formula section to assign those color values. The formula section will be executed once for each pixel - a total of 10,000 times for this example.
A key concept for the formula section is the idea of "the current point". In the example above, we'll create an image that is 100 pixels wide and 100 pixels tall - the steps the program goes through to accomplish this are as follows:
The formula the program uses to determine the step value is:
(end_value - start_value) / (number_of_pixels - 1)
So for our example we end up with:
(2 - -2) / (100 - 1) = .0404040404
The horizontal values (in real coordinates) corresponding to the pixel values then would be:
pixel number | real coordinate value |
---|---|
0 | -2 |
1 | -1.9595 |
2 | -1.9191 |
3 | -1.8787 |
... | ... |
96 | 1.8787 |
97 | 1.9191 |
98 | 1.9595 |
99 | 2 |
for each pixel in the vertical direction for each pixel in the horizontal direction determine the current point execute the formula section to color this pixel end horizontal loop end vertical loop
Using the real and imaginary step values, the program calculates the current point, loads this value into the predefined variable current and then executes the formula section.
Structure of the formula section
formula { zero or more statements while(boolean_expression) { zero or more statements } zero or more statements }We refer to statements in three places above, more formally the definition of statements is:
One or more of the following:
A detailed explanation of the various statements can be found here
while
The sequence of events that occurs when we are executing the formula section for a particular pixel is:
The final point about the formula section concerns color assignment. Each time the formula section is executed, whatever value is assigned to the current pixel (via an indexed or direct color assignment statment) determines the color that pixel will have in the resulting image. Though the set_color statement can occur anywhere, is usually only occurs in the set of statements that follow the while loop. The reason is that the statements inside the while loop get executed many, many times and color assignment typically needs to be done only once - after the while loop has terminated.
Futher details about color assignment can be found here