Algorithms   

Mandelbrot Sets

The formation of Mandelbrot sets is discussed in the Types of Fractals section. Most Mandelbrot sets use some formula and complex 2 numbers. One of them (z) is the variable number that you change to see if it goes to infinity or not. The other one (c) is a number that defined a point you are currently looking at. Since we cannot perform operations with complex numbers in most computer languages, we express z and c in terms of two variables as x + yi and a + bi respectively. The formulas have to be changed to suit non-complex numbers (see programming reference). The following algorithm uses the most common formula z = z^2 + c.

Constants used:
x_minimum, x_maximum, y_minimum, y_maximum: coordinates of the viewing window
x_resolution, y_resolution: the resolution of the screen
number_of_iterations: more makes the program more accurate, but slower
number_of_colors: used to take a remainder of a number and choose a color

‘ run through every point on the screen, setting a and b to the coordinates
FOR a = x_minimum TO x_maximum STEP x_resolution
            FOR b = y_minimum TO y_maximum STEP y_resolution
                       ‘ the initial z value is 0 + 0i, so x and y have to be set to 0
                        x = 0: y = 0
                        ‘ perform the iteration
                          FOR num = 1 to number_of_iterations
                                       ‘ exit the loop if the number becomes too big
                                       IF x^2 + y^2 > 4 THEN END FOR
                                       ‘ use the formula
                                       new_x = x^2 - y^2 + a
                                       new_y = 2*x*y + b
                                       ‘ set the new values
                                       x = new_x: y = new_y
                          NEXT num
                          ‘ determine the color using the number of iterations it took
                          ‘ for the number to become too big
                          color = num MOD number_of_colors
                          ‘ plot the point
                          PSET (a, b), color
             NEXT b
NEXT a

forward