Formula


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:

  1. Take the starting and ending real values (-2 and 2 for this example) and using the number of horizontal pixels, determine the needed step value so that the correct number of pixels is generated. In this case, we want the first pixel to correspond the the first value (-2) and that last (100th) pixel to correspond the the last value (2).

    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 numberreal coordinate value
    0-2
    1-1.9595
    2-1.9191
    3-1.8787
    ......
    961.8787
    971.9191
    981.9595
    992

  2. Take the starting and ending imaginary values (-2 and 2 for this example) and perform a similar calculation to determine the appropriate step value so that the correct number of vertical pixels is generated. In this case since the imaginary values are the same as the real values and the height in pixels is the same as the width in pixels, the step value will be: .0404040404 in the imaginary direction, just as it was in the real direction.

  3. Having calculated the two step values, the program then loops through all the pixels and gives the formula section a chance to assign the color for each pixel. The logic looks like this:

            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:

    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