Algorithms

Julia Sets: Pixel Algorithm

The formation of Julia sets is discussed in the Types of Fractals section. The algorithm for generating them is very similar to that of Mandelbrot sets. Similarly to them, Julia sets use two complex numbers — the Julia set constant (c) and the variable (z) that we use to check if the point goes to infinity or not. Since we cannot perform operations with complex numbers in most computer languages, we will store both of these in terms of two variables that will define their real and imaginary coefficients. Thus, we will let c = a + bi and z = x + yi. To find out how to make a complex number formula suit this form of expression, check out the programming reference. The algorithm below can generate any Julia Set using the formula z = z^2 + c.

Constants used:

real_coefficient, imaginary_coefficient: coefficients of the Julia Set constant (c)
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

```‘ set the Julia Set constant
a = real_coefficient
b = imaginary_coefficient
‘ run through every point on the screen, setting
‘ m and n to the coordinates
FOR m = x_minimum TO x_maximum STEP x_resolution
FOR n = y_minimum TO y_maximum STEP y_resolution
‘ the initial z value is the current pixel,
‘ so x and y have to be set to m and n
x = m: y = n
‘ 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 (m, n), color
NEXT n
NEXT m```