Open In App

Fractals in C/C++

A Fractal is a never-ending pattern. Fractals are infinitely complex patterns that are self-similar across different scales. They are created by repeating a simple process over and over in an ongoing feedback loop. Mathematically fractals can be explained as follows.

Mandelbrot Set :
The Mandelbrot set is the set of complex numbers c for which the function fc(z) = z2 + c does not diverge when iterated from z = 0, i.e, for which the sequence fc(0), fc(fc(0)) etc, remains bounded in absolute value.



The Mandelbrot set is the set of values of c in the complex plane for which the orbit of 0 under iteration of the quadratic map Zn+1 = Zn2 + c remains bounded. That is, a complex number c is part of the Mandelbrot set if, when starting with Z0 = 0 and applying the iteration repeatedly, the absolute value of Zn remains bounded however large n gets. Below given is the initial image of a Mandelbrot set zoom sequence. Black points correspond to numbers that are outside the set.


Properties of Mandelbrot Set :



Implementation : Since the concept of Fractals involves the mathematical properties of equations, the algorithm and the programs that create fractals are difficult to write and optimize. One can find many commercial software that create fractals. These programs represent some of the most optimized and probably the best fractal algorithms and implementations that have been created. Below given is the approach :




// C++ implementation for mandelbrot set fractals
#include <graphics.h>
#include <stdio.h>
#define MAXCOUNT 30
  
// Function to draw mandelbrot set
void fractal(float left, float top, float xside, float yside)
{
    float xscale, yscale, zx, zy, cx, tempx, cy;
    int x, y, i, j;
    int maxx, maxy, count;
  
    // getting maximum value of x-axis of screen
    maxx = getmaxx();
  
    // getting maximum value of y-axis of screen
    maxy = getmaxy();
  
    // setting up the xscale and yscale
    xscale = xside / maxx;
    yscale = yside / maxy;
  
    // calling rectangle function
    // where required image will be seen
    rectangle(0, 0, maxx, maxy);
  
    // scanning every point in that rectangular area.
    // Each point represents a Complex number (x + yi).
    // Iterate that complex number
    for (y = 1; y <= maxy - 1; y++) {
        for (x = 1; x <= maxx - 1; x++)
        {
            // c_real
            cx = x * xscale + left;
  
            // c_imaginary
            cy = y * yscale + top;
  
            // z_real
            zx = 0;
  
            // z_imaginary
            zy = 0;
            count = 0;
  
            // Calculate whether c(c_real + c_imaginary) belongs
            // to the Mandelbrot set or not and draw a pixel
            // at coordinates (x, y) accordingly
            // If you reach the Maximum number of iterations
            // and If the distance from the origin is
            // greater than 2 exit the loop
            while ((zx * zx + zy * zy < 4) && (count < MAXCOUNT))
            {
                // Calculate Mandelbrot function
                // z = z*z + c where z is a complex number
  
                // tempx = z_real*_real - z_imaginary*z_imaginary + c_real
                tempx = zx * zx - zy * zy + cx;
  
                // 2*z_real*z_imaginary + c_imaginary
                zy = 2 * zx * zy + cy;
  
                // Updating z_real = tempx
                zx = tempx;
  
                // Increment count
                count = count + 1;
            }
  
            // To display the created fractal
            putpixel(x, y, count);
        }
    }
}
  
// Driver code
int main()
{
    // gm is Graphics mode which is
    // a computer display mode that
    // generates image using pixels.
    // DETECT is a macro defined in
    // "graphics.h" header file
    int gd = DETECT, gm, errorcode;
  
    float left, top, xside, yside;
  
    // setting the left, top, xside and yside
    // for the screen and image to be displayed
    left = -1.75;
    top = -0.25;
    xside = 0.25;
    yside = 0.45;
    char driver[] = "";
  
    // initgraph initializes the
    // graphics system by loading a
    // graphics driver from disk
    initgraph(&gd, &gm, driver);
  
    // Function calling
    fractal(left, top, xside, yside);
  
    getch();
  
    // closegraph function closes the
    // graphics mode and deallocates
    // all memory allocated by
    // graphics system
    closegraph();
  
    return 0;
}

Output :

Applications of Fractals : The basic idea of Fractals is to find regularities in the existing irregularities. Below given are some applications of Fractals :


Article Tags :