Open In App

Mandelbrot Set in C/C++ Using Graphics

Last Updated : 06 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Fractals

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.

  • The location of a point on a screen is fed into an equation as its initial solution and the equation is iterated a large number of times.
  • If that equation tends to zero (i.e. the value at the end of the iterations is smaller than the initial value), the point is coloured black.
  • If the equation tends to infinity (i.e. the final value is larger than the initial value) then depending on the rate of increase (i.e. the rate at which the value tends to infinity), the pixel is painted with an appropriate colour.

Defining Mandelbrot

The Mandelbrot set is the set of complex numbers c for which the function f_c(z) = z^2 + c  does not diverge when iterated from z=0, i.e., for which the sequence f_c(0), f_c(f_c(0))  , etc., remains bounded in absolute value. In simple words, Mandelbrot set is a particular set of complex numbers which has a highly convoluted fractal boundary when plotted. Implementation 

CPP14

#include <complex.h>
#include <tgmath.h>
#include <winbgim.h>
 
// Defining the size of the screen.
#define Y 1080
#define X 1920
 
// Recursive function to provide the iterative every 100th
// f^n (0) for every pixel on the screen.
int Mandle(double _Complex c,
           double _Complex t = 0,
           int counter = 0)
{
 
    // To eliminate out of bound values.
    if (cabs(t) > 4) {
        putpixel(creal(c) * Y / 2 + X / 2,
                 cimag(c) * Y / 2 + Y / 2,
                 COLOR(128 - 128 * cabs(t) / cabs(c),
                       128 - 128 * cabs(t) / cabs(c),
                       128 - 128 * cabs(t) / cabs(c)));
        return 0;
    }
 
    // To put about the end of the fractal,
    // the higher the value of the counter,
    // The more accurate the fractal is generated,
    // however, higher values cause
    // more processing time.
    if (counter == 100) {
        putpixel(creal((c)) * Y / 2 + X / 2,
                 cimag((c)) * Y / 2 + Y / 2,
                 COLOR(255 * (cabs((t * t))
                              / cabs((t - c) * c)),
                       0, 0));
        return 0;
    }
 
    // recursively calling Mandle with increased counter
    // and passing the value of the squares of t into it.
    Mandle(c, cpow(t, 2) + c, counter + 1);
 
    return 0;
}
 
int MandleSet()
{
 
    // Calling Mandle function for every
    // point on the screen.
    for (double x = -2; x < 2; x += 0.0015) {
        for (double y = -1; y < 1; y += 0.0015) {
            double _Complex temp = x + y * _Complex_I;
            Mandle(temp);
        }
    }
    return 0;
}
 
int main()
{
    initwindow(X, Y);
    MandleSet();
    getch();
    closegraph();
    return 0;
}

                    

Output



Similar Reads

Mandelbrot Fractal Set visualization in Python
Fractal: A fractal is a curve or geometrical figure, each part of which has the same statistical character as the whole. They are useful in modeling structures (such as snowflakes) in which similar patterns recur at progressively smaller scales, and in describing partly random or chaotic phenomena such as crystal growth and galaxy formation. In sim
7 min read
Julia Fractal set in C/C++ Using Graphics
CC BY-SA 4.0, Link The Julia set is associated with those points z = x + iy on the complex plane for which the series zn+1 = zn2 + c does not tend to infinity. c is a complex constant, one gets a different Julia set for each c. The initial value z0 for the series is each point in the image plane. The well known Mandelbrot set forms a kind of index
4 min read
2D Transformation in Computer Graphics | Set 1 (Scaling of Objects)
We can use a 2 × 2 matrix to change or transform, a 2D vector. This kind of operation, which takes in a 2-vector and produces another 2-vector by a simple matrix multiplication, is a linear transformation. By this simple formula, we can achieve a variety of useful transformations, depending on what we put in the entries of the matrix. For our purpo
5 min read
C program to create a pendulum clock using graphics
In this article, it is discussed how to design a Pendulum Clock in the C programming language using Graphics. Approach: Create two rectangles, one inside the other, using the rectangle() function to act as the outer outline of the clock &amp; the other is the inner outline of the clock.Color the space between the two rectangles brown using setfills
3 min read
Flood fill algorithm using C graphics
Given a rectangle, your task to fill this rectangle using flood fill algorithm. Examples: Input : rectangle(left = 50, top = 50, right= 100, bottom = 100) flood( x = 55, y = 55, new_color = 12, old_color = 0) Output : Input : rectangle(left = 50, top = 50, right= 200, bottom = 400) flood( x = 51, y = 51, new_color = 6, old_color = 0) Output : Flood
2 min read
C program to create a Rocket using Graphics
In this article, we will discuss how to draw the Rocket using Graphics. Approach: Draw a straight line using the line() function that will act as the Ground Outline.Below the above line drawn, fill it with the color green using two functions setfillstyle() and floodfill().Create a rectangle using the rectangle() function and color it brown that wil
2 min read
Draw a moving car using computer graphics programming in C
In computer graphics, use graphics.h which provide direct functions to draw different coordinate shapes (like circle, rectangle etc). By using these functions we can draw different objects like car, hut, trees, etc. In this program, we will draw a moving car using line and circles. Functions used in program: delay(n): This function is used for hold
4 min read
Program to draw India Gate using computer graphics in C
In C graphics, the graphics.h functions are used to draw different shapes like circles, rectangles, etc, display text(any message) in a different format (different fonts and colors). By using graphics.h one can make programs, animations, and also games. Function Used: rectangle(l, t, r, b): A function from graphics.h header file which is used to dr
6 min read
C program to draw a cricket ground using computer graphics
In this article, we will discuss how to draw a 2D cricket ground is being designed using computer graphics. Approach: Draw a circle using the circle() function. This will act as the Ground Outline.Color the above circle with green using the setfillstyle() and floodfill() functions.Then to implement 30 yards Outline using the ellipse() function.Then
2 min read
Creating Butterfly themed Fractal in C++ Using Graphics
In Layman's terms, Fractals are beautiful patterns brought into existence upon the intertwining of computation and mathematics. To get a bit technical they are recursive in nature such that when looked upon a certain subset of a given fractal a similar pattern appears to emerge out of it. In this article, a new type of fractal pattern is generated,
3 min read