In mathematics, **fractals** are a self-similar subset of Euclidean space whose fractal dimensions strictly exceeds its topological dimension. So when you draw these functions in their topological height, fractals appear the same at different levels, as illustrated in successive magnifications of the **Mandelbrot set**.

Mandelbrot set is a good example to see how fractals are changing in every x,y and z planes. Drawing a Mandelbrot set exhibit an elaborate and infinitely complicated boundary that reveals progressively ever-finer recursive details at increasing zooming that makes the boundary of the Mandelbrot set as in fractal curves. The Mandelbrot set basically defined as a function of f(z)=z^2+c where z and c are complex numbers.

C++ Builder is easy to draw or print these kind of graphics, it includes compilers for Win32, Win64, Android and iOS. C++Builder has both CLANG Enhanced C/C++ Compiler and a Borland C/C++ Compiler. It also features a modern, high-productivity RAD Studio IDE, debugger tools, and enterprise connectivity for to accelerate cross-platform UI development. You can develop GUI based applications easily, as it comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition that can be used by students, beginners and startups with limitations.

You can download Free the C++ Builder Community Edition (CE) here: https://www.embarcadero.com/products/cbuilder/starter.

Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. You can download and use the trial version for one month with the same capabilities as the full version . Please visit https://www.embarcadero.com/products/cbuilder.

In this post we want to show you how you can printout a Mandelbrot set in text form in C++ Builder. While we are able to draw as shown above in graphics we want to have fun with printing it in text form.

1.Create a new Multi-Device C++ Builder Console project, Save all unit and project files into a folder.

2. Let’s do as simple as possible. Clear all codes, add these library includes.

1 2 3 4 |
#include <iostream> #include <complex> |

3. Now let’s create our own **text_mandelbrot(…)** function to draw a Mandelbrot set in text form as a console.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
void text_mandelbrot( int maxW, int maxH, int max_it) { for(auto y = 0; y < maxH; ++y) { for(auto x = 0; x < maxW; ++x) { std::complex<float> z,c = { 2.*x/maxW-1.5f, 2.*y/maxH-1}; int iteration = 0; while(abs(z) < 2 && ++iteration < max_it) z = z*z + c ; std::cout << (iteration == max_it ? 'O' : '.'); } std::cout << '\n'; } } |

As given in here we defined two complex numbers

1 2 3 |
std::complex<float> z, c = { ... , ... }; |

We loop in row and column as x and y coordinates, and we calculate height of this function with complex numbers related with these row and column,

1 |
z = z*z + c ; |

4. Finally we need to define our main function to run our **text_mandelbrot(…)** function.

1 2 3 4 5 6 7 8 |
int main(void) { text_mandelbrot(78,22,20); getchar(); return 0; } |

5. Hit **F9 **to run your codes or run your code from **Run** button.

That’s all. This simple code compatible with most of C++ compilers, you may need few changes in some compilers. You can improve this code by drawing a part of this function (like zooming to a portion of it). Or you can use Bitmaps and directly draw it with full details. Don’t worry about calculations and speed about to draw these kind of functions in math. Because C++ is faster to calculate and draw these things. You can also use parallel programming methods to use all of your CPU and GPU cores draw anything multiple times faster.

Full Lines of VCL Console Application in C++ Builder should be like this;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <vcl.h> #include <stdio.h> #include <iostream> #include <complex> void text_mandelbrot( int maxW, int maxH, int max_it) { for(auto y = 0; y < maxH; ++y) { for(auto x = 0; x < maxW; ++x) { std::complex<float> z,c = { 2.*x/maxW-1.5f, 2.*y/maxH-1}; int iteration = 0; while(abs(z) < 2 && ++iteration < max_it) z = z*z + c ; std::cout << (iteration == max_it ? 'O' : '.'); } std::cout << '\n'; } } int main(void) { text_mandelbrot(78,22,20); getchar(); return 0; } |

And the result will be like this;

Design. Code. Compile. Deploy.

Start Free Trial

Free C++Builder Community Edition