Mandelbrot and Julia sets with PANORAMIC and FreeBASIC

By jdebord

I. Introduction

We present here a program written in two BASIC dialects, PANORAMIC and FreeBASIC, to explore the Mandelbrot and Julia sets. These fractal figures are created by iteration of a function of complex variable z → f(z) + c, where c is a complex constant. The present version is limited to the power function f(z) = zp, where p is an integer or real exponent such that p > 1.

The program (sources and executable) are available from the author's web site.

II. The Mandelbrot set

The Mandelbrot set, named after the mathematician Benoît Mandelbrot, corresponds to the case p = 2. More precisely, it is defined as the set of complex numbers c for which the sequence:

z0 = c
zn = (zn-1)2 + c

converges to a finite value.

In practice, the sequence is iterated at each point c in the complex plane. The points belonging to the set are given the same color, and the points outside the set are given a color which depends on their divergence rate.

A global view of the Mandelbrot set is shown here:

III. The Julia sets

These sets, named after the mathematician Gaston Julia, are defined in a manner similar to the Mandelbrot set, except that here the parameter c is constant and the sequence is initialized with the coordinates zpixel of the point:

z0 = zpixel
zn = (zn-1)2 + c

So, there is a Julia set for each value of the parameter c.

It has been shown that:

c = (-0.75 , 0)

This point belongs to the Mandelbrot set.
(it is the contact point between the cardioid and the main circle)
The Julia set is connected.

c = (-0.75 , 0.1)

This point is outside the Mandelbrot set. The Julia set is disconnected.

IV. Variation of the exponent p

IV.A. Integer exponent

For the integer values of p (> 2) the Mandelbrot set is centered at (0,0) and is comprised of (p - 1) symmetrical lobes. When p is even, one of these lobes is located along the negative Ox axis. There is no such lobe when p is odd.


p = 3

2 lobes (symmetry of order 2). No lobe on the Ox axis.

p = 4

3 lobes (symmetry of order 3). One lobe on the Ox axis.

The corresponding Julia sets have a symmetry of order p.

Example with p = 5 and c = (-0.5 , 0.64) :

IV.B. Non-integer exponent

The pictures display discontinuities. This is due to the fact that, for non-integer values of p, zp is evaluated as exp(p ln z). But the logarithm of a complex number is a multi-valued function. The discontinuities arise from the choice of a single value at each point.

Example with p = 3.5 :

It is interesting to study the transition from an odd integer p to an even integer while following the intermediaite values. We can observe the progressive formation of the lobe located along the Ox axis. This lobe is formed by fusion of multiple "buds" which develop progressively and are surrounded by complex structures.

p = 3.85

A step of the formation of the lobe located on the Ox axis, from several fragments.

p = 3.85

Zoom (× 10) of the previous image, showing some fragments and the structures around them.

V. Theoretical aspects

We will explain here some mathematical principles involved in the computation of these sets. An elementary knowledge of complex numbers is required.

V.A. Iteration of the complex function

The sequence zn = (zn-1)p + c is iterated until the modulus |zn| exceeds the escape radius, or the iteration number n exceeds a predefined value. In the last case, the point is considered as belonging to the Mandelbrot set.

V.B. The distance estimator

As its name implies, this parameter estimates the distance between the tested point and the Mandelbrot set. It is computed by using the fact that the iteration number required to reach the escape radius increases faster as the point approaches the set. This rate is estimated by the derivative of the iterated function.

z'n = p (zn-1)p-1 (z'n-1) + 1

with z'0 = 0

z'n = p (zn-1)p-1 (z'n-1)

with z'0 = 1

At the end of the iterations, the distance estimator is given by:

D = ( p |zn| ln |zn| ) / |z'n|

The weaker this value, the closer we are to the set.

V.C. The continuous dwell method

As we have shown previously, points outside the Mandelbrot set are colored according to the iteration number required to reach the escape radius. At the vicinity of the set, this number varies rapidly from one point to another, resulting in a color mixing which makes the picture look "fuzzy". To avoid this effect, it is possible to "smooth" the variation of iteration numbers by means of a logarithmic transform. The formula used here is the following:

Dwell = n - logp ( ln |zn| ) + logp ( ln Esc )

where Esc denotes the escape radius. The function logp is the base-p logarithm, such that logp z = ln z / ln p


The above computations are implemented in the fbmandel.bas DLL, written in FreeBASIC and based on the fbcomplex library for complex numbers. We present here the main algorithms used by the DLL, as well as the exported functions.

VI.A. Picture format

The picture is 640 × 480, 32 bits. It is centered at (x0 , y0). The zoom is defined by the ZoomFact parameter, such that the value ZoomFact = 1 corresponds to a vertical scale of 4, resulting in a horizontal scale of 4 × (640 / 480) = 5,333 ... The vertical scale for a given ZoomFact value is therefore 4 / ZoomFact. If Scale denotes the pixel scale, we can define a scale factor:

ScaleFact = 4 / (Scale * ZoomFact)

This factor represents the distance between 2 pixels. So, the pixel coordinates (Nx, Ny) of a point are converted to the algebraic coordinates (x, y) by:

   x = x0 + ScaleFact * (Nx - HalfPicWidth)
   y = y0 - ScaleFact * (Ny - HalfPicHeight)

where HalfPicWidth and HalfPicHeight denote the half-width and half-height of the picture, i. e. 320 and 240 in our case.

A complete scan of the picture is performed with two loops:


  for Ny = 0 to PicHeight - 1
     yt = y0 - ScaleFact * (Ny - HalfPicHeight)
     for Nx = 0 to PicWidth - 1
       xt = x0 + ScaleFact * (Nx - HalfPicWidth)
           pset (Nx, Ny), Mandelbrot(xt, yt)
     next Nx
   next Ny

where PicWidth and PicHeight denote the width and height of the picture, i. e. 640 and 480 in our case. The Mandelbrot function returns the color of the point.

VI.B. Computing the iterations

The Mandelbrot function performs the iterations at the point of algebraic coordinates (xt, yt) by computing simultaneously the complex function zn and its derivative z'n, according to the following code:


  if Julia then
     c = cJulia
     z = Cmplx(xt, yt)
     dz = C_one
     c = Cmplx(xt, yt)
     z = C_zero
     dz = C_zero
   end if
   Iter = 0
   Module = CAbs(z)
   do while Iter < MaxIter and Module < Esc
     zp1 = z ^ p1
     zn = z * zp1 + c
     dzn = p * zp1 * dz
     if not Julia then dzn = dzn + 1
     Module = CAbs(zn)
     z = zn
     dz = dzn
     Iter = Iter + 1


The iterations can be terminated in two ways:

VI.C. Color attribution

We use a simplified version of an algorithm described by Robert Munafo. Its main features are the following:

VI.D. Functions exported by the DLL

The DLL exports 4 functions:

VII. The PANOMAND program

This program written in PANORAMIC supplies a graphic interface to the previous DLL.

The values of the various parameters are entered in the corresponding cells. The X and Y coordinates relate to the center of the picture. The "Graph" button starts the computation. The picture is plotted in a secondary window, then transferred to the main window. The picture is saved in the file fractal.bmp. The parameters are saved in the text file fractal.par. The "Save file" button allows to save to another file name, while the "Open file" button allows to load a previously saved picture.

To switch from the Mandelbrot set to the Julia set, click on the "Mandelbrot" button. Its caption will become "Julia" and the coordinates of the current Mandelbrot picture will become the c parameter of the Julia set, appearing in the "c_X" and "c_Y" boxes. The old coordinates will become zero, to ensure that the Julia set is centered at (0, 0).

By clicking on a point of the picture, we get its coordinates in the cells X and Y. We can then zoom on the selected point by increasing the zoom factor.

VIII. Conclusion

We hope that this program will incite you to further explore the Mandelbrot and Julia sets.

Don't hesitate to generate your own pictures. There are still plenty of things to discover!

As an example, the author's web site displays an exploration of the seahorse valley, located between the cardioid and the main circle. Note that the web site is in french, but the example consists mainly of pictures, so that you can enjoy them even if you can't read french!

IX. Links