This is the final post in the ChaosExplorer series of posts. ChaosExplorer was created to explore a number of chaotic systems and to create the displays for these systems in fragment shaders using OpenGL and GLSL. Four different fractal formulae were used to generate fractals and their corresponding Julia Sets. From those standpoints, I would call ChaosExplorer a success. The program is not commercial grade; there are many changes that should be made to ChaosExplorer.
There are many more fractal equations that could be used. In fact, Fractal Formulas Researched lists more than 1000 different fractal formulae. There are some duplicates in the list, but still more formulae that you are likely to investigate.When choosing a formula from that list, remember that z0 = 0, so make sure that z1 is not always also 0 or you will simply get a black display. Also, make sure that for formulae containing both a numerator and a denominator, that the denominator does not also calculate out to 0 or you will simply get a solid orange display.
ChaosExplorer currently displays different colours based solely on the number of iterations, so there is a step difference between each colour. The colour instead could be chosen based on the value of z after a constant number of iterations. This would potentially produce a much smoother transition between colours.
ChaosExplorer could be refactored to place additional functionality in the panel base classes. It may be possible to entirely eliminate the individual derived panels in all cases except the MultibrotPanel.
There are a few places in the code where magic numbers are used. These may be refactored to use defined constants.
Those are just a few ways that ChaosExplorer could be improved. Since my reasons for creating this program have been met, I will leave those modifications to you.
Each of these menu items displays the fractal (z0 = 0.0 + 0.0i, varying c over the complex plane). Selecting the first menu item displays the Mandelbrot Set; selecting any of the others displays the fractal for the indicated formula. This post specifically shows the fractal and Julia Sets for the second menu item, whose formula is:
zn+1 = (zn3 + 1) / (1 + czn2)
Here is the fractal display. This is for the region -10.0 ≤ x ≤ 10.0, -10.0 ≤ y ≤ 10.0:
And here are 4 Julia Set displays for points in this fractal:
As you can see, the fractal and Julia Sets are very different from the Mandelbrot/Multibrot fractals and Julia Sets.
That’s all fine and good, but how do we create a Julia Set mathematically? Certainly not by attempting to follow the Wikipedia entry! For a Julia Set that corresponds to the Mandelbrot set, we use the same equation, but generate the plot for a different plane. Remember the equation:
zn+1 = znp + c
For the Mandelbrot Set, z0 = 0 + 0i and p = 2. We set c to be each point in the section of the complex plane that we were looking at. For the Julia Set, we set c to a single value in the complex plane, then set z0 to each point in the section of the complex plane we are looking at, and iterate. For every point, c, inside the Mandelbrot Set, there is a connected Julia Set; for each point outside of the Mandelbrot Set, the Julia Set is disconnected.
The most interesting images are generated from points outside the Mandelbrot Set or just inside it. This image is generated for a point near the centre of the secondary bulb:
and this is image is generated for a point inside one of the small bulbs off the main bulb:
This image is generated for a point just outside the Mandelbrot set and slightly about the x-axis:
Here are a couple of images created from the tendrils of the Mandelbrot Set:
and finally, an image from a short distance outside the Mandelbrot Set:
Each value, c, produces a different image. To generate an image for a point in the Mandelbrot Set using the ChaosExplorer program, place the mouse cursor over the point, click the right mouse button, and select the Julia Set menu item. The image is generated in a panel in a new notebook tab. You can also zoom in on the image by selecting an area to zoom, and then selecting the Draw From Selection menu item. The source code and a wiki are available on GitHub.
This is the fragment shader for generating these Julia Sets:
If you compare this to the fragment shader for the Multibrot Sets included in the post Mandelbrot Set, you will see few changes.
This post discussed Julia Sets and showed images of some Julia Sets generated from several points inside and outside the Mandelbrot Set. The fragment shader that is used to generate the images was also shown.
The last two posts have discussed Chaotic Systems and Fractals, and the Mandelbrot Set. As stated in the second of these posts, the Mandelbrot Set is a specialization of the Multibrot Set. These sets are created by iterating zn+1 = znp + c where z, p, and c are complex numbers. c is the coordinate, in the complex plane, of the point being calculated. Points inside the set are bounded as n approaches infinity, and points outside the set are unbounded (tend to infinity) as n approaches infinity. More information is provided in the post on the Mandelbrot Set and in the ChaosExplorer program’s wiki on GitHub.
For the Mandelbrot Set, p = 2. This post will look at other values of p, including both real and complex values, and also the effects of varying the real and imaginary portions of Z0.
The first menu item in the context menu of MultibrotPanel is a submenu containing menu items that set p to integer values between 2 and 10. The next four images show the resulting images for the values 2, 3, 4, and 10.
Note that the number of secondary bulbs (the second largest bulbs) in each image is equal to p – 1 where p is the integer power in the iterated equation.
The Animate Real Powers menu item changes the power value in steps of 0.01 from 1.0 to 10.0. Here is a video showing the display when you select this menu item:
The Animate Imaginary Powers menu item changes the imaginary portion of power in steps of 0.01i from -1.0i to +1.0i. Here is a video showing the display when you select this menu item starting from the display of Power = 3:
The Animate Z0 Real menu item changes the real portion of z0 from -1.0 to +1.0 in increments of 0.01. Here is a video of the result with Power = 6:
And finally, the Animate z0 Imaginary menu item changes the imaginary portion of z0 from -1.0i to +1.0i in increments of 0.01i. Here is a video of the result with Power = 3:
This ends the exploration of the Mandelbrot and Multibrot Sets. In the next post, we will look at Julia Sets related to the Mandelbrot Set.
Before we discuss and generate the Mandelbrot set, we need to look at complex numbers, the complex plane, and Euler’s formula. If you are familiar with these concepts, then you can skip to the next section in this post. Or, if you can’t stand the math, just go look at the pretty pictures. 🙂
A complex number is of the form a + bi, where a and b are real numbers and i is defined such that i2 = -1. Note that i is defined only in terms of what the value of i2 is, and not by itself. You might quickly think that i = sqrt(-1), but this is only one of the roots of the equation. In fact, as my university algebra professor showed, by defining i this way, it is possible to prove that -1 = +1. That would certainly negate all of mathematics (and physics) as we know it.
Complex numbers are required to solve equations for which there is no real solution. Take for example:
(x + 1)2 = -9
There are no real number values for x that satisfy that equation. However, as shown here, there are two complex number values that do satisfy the equation:
x = -1 + 3i, and
x = -1 -3i.
A complex number can be viewed as a point or a position vector plotted in a two-dimensional Cartesian coordinate system. This is called the complex plane. Complex numbers are normally plotted such that the real portion of a complex number x + yi is plotted with the x value along the horizontal axis, and the y value plotted along the vertical axis.
Now that we have plotted a complex number in the two-dimensional Cartesian coordinate system, it is possible to specify the complex number using polar coordinates, and finally, using Euler’s formula.
See the Wikipedia page on Complex numbers for more information. Using Euler’s formula, it is even possible to calculate the complex power of a complex number. The Wolfram Mathworld website contains a web page that shows the formula for calculating these values. And finally, the Abecedarical Systems website contains a web page with the listing of a program that calculates the complex power of a complex number.
Now, why do we need all of this mathematics? C++ contains a templated complex number class, but GLSL does not support complex numbers. Therefore, to calculate the complex power of a complex number using GLSL, it will be necessary to use Euler’s formula. You will see this later when we look at the GLSL code for the fragment shader.
Now that we have covered some rather heavy mathematics, we can look at the Mandelbrot set. Assuming z0 = 0 and c is a point in the complex plane, then the Mandelbrot set is the set of all points c in the complex plane where the value zn+1 does not tend toward infinity as n approaches infinity in the following equation:
zn+1 = zn2 + c.
If we attempt to iterate this formula an infinite number of times for each point in the complex plane, the calculations will of course take an infinite amount of time. There are two pieces of information that we an use to limit the number of calculations:
For all values of zn where |zn| ≥ 2, the value approaches infinity as n approaches infinity.
As n increases, the set of points approaches the Mandelbrot set quite quickly. There is little difference between the points defined by z200 and z∞.
The program, ChaosExplorer, uses these two facts to limit the number of iterations that are required for each point. ChaosExplorer is a C++ program that uses wxWidgets for the windowing system, and GLSL to calculate and display the points that are inside and outside the Mandelbrot set. The majority of the code will not be shown or discussed in this post because similar or identical code has been covered in earlier posts. The program code is available on GitHub.
When ChaosExplorer starts, it displays the Mandelbrot set, shown below, in a tabbed window in the main window of the program.
This image shows the complex plane for -2.5 < x < 1.5 and -2.0 < y < 2.0. The portion in black is the Mandelbrot set. The colours change as the number of iterations (n) required for zn to exceed the value 2 increases. There are 50 colours used, with the full set repeated 4 times for a total of 200 iterations.
You can select an area to zoom in on by pressing the left mouse button and dragging towards the right and down. As you select an area, the area is highlighted by a white line around the selection. When you have chosen the area you want to select, release the left mouse button. A selection is shown in the image above. Now to zoom into that area, click the right mouse button anywhere in the image to display the context menu, and select the Draw From Selection menu item. The resulting display is shown in a second tabbed window. Here is the result for the region selected in the image above:
Note the smaller version of the main Mandelbrot set. You can continue to zoom in on the tendrils, such as the next image, to see ever smaller versions of this set. This shows that the Mandelbrot set is a fractal.
You can also animate the zoom by placing the mouse cursor at the point you want to zoom, clicking the right mouse button, and selecting Animate Magnification from the context menu.
Here are some additional zoomed images:
and a video of a zooming image:
To redisplay the full Mandelbrot set, select the Power=2 menu item in the MultibrotPower submenu.
Above, I stated that points selected by each iteration quickly approaches the Mandelbrot set as the number of iterations increases. You can see this by selecting the Animate Iterations menu item in the context menu. The image will change for each iteration, showing the pixels that are excluded by each iteration. You can run this animation at any zoom level.
I leave it to you as an exercise to investigate the various regions of the image at varying magnifications.
So, how are the images calculated and drawn? The calculations are done in the fragment shader:
GLSL does not contain a complex data type, so complex values are passed in as vec2 uniform values. The iPower function calculates the complex power of a complex number using Euler’s formula as mentioned in the first section of this post. For integer real powers of complex numbers, the mathematics could be simplified considerably, but the full calculation in iPower is included because it will be used in other functionality in this program.
Finally, the main function performs repeated iterations of the formula zn+1 = znp + c until the magnitude of zn+1 exceeds the escape value of 2. c is the complex number that specifies the coordinates of the point in the complex plane. The color for the fragment is then determined from the number of iterations (the value of i in the code). This video shows the full Mandelbrot set as the number of iterations increases. The number of iterations is shown in the status bar at the bottom of the window. Once the number of iterations exceeds about 40, the only place you will see any changes at this zoom level is at the junction between the main bulb and the bulb to its left.
Advantages and Disadvantages of Performing the Calculations in the Fragment Shader
The main advantage is the speed at which the image is calculated and drawn. Using modern GPUs with hundreds or thousands of cores, the image can be updated at the rate of many frames per second as seen when animating the magnification. If the image was calculated in the CPU, the number of frames per second that could be generated is much less (around 1 frame per second) even if the code were multithreaded.
GLSL does not contain a complex data type, so Euler’s formula must be used to calculate a complex power of a complex number.
Since version 4.0.0 of OpenGL, GLSL supports the double data type, but trigonometric functions are supplied only for floating point values. This means that the complex numbers can be specified only as two floating point numbers and not as two doubles. As the zoom factor of the image increases. the limitations on the values of floating point numbers become a factor, and pixelation of the image occurs much sooner than would be the case if doubles could have been used. Note the pixelation in the following image:
This post discussed the Mandelbrot set and the ChaosExplorer program. Several images of the Mandelbrot set are shown, as are examples of the Animate Iterations and Animate Magnification functions of the program.
The Mandelbrot set is a specialization of the formula zn+1 = znp + c where p = 2. The next post will look at the more general Multibrot set, wherein p takes on other values, including complex values.
More information about ChaosExplorer and the Mandelbrot Set are included in the wiki for the ChaosExplorer program on GitHub.