How to resolve the algorithm Mandelbrot set step by step in the C# programming language
How to resolve the algorithm Mandelbrot set step by step in the C# programming language
Table of Contents
Problem Statement
Generate and draw the Mandelbrot set.
Note that there are many algorithms to draw Mandelbrot set and there are many functions which generate it .
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Mandelbrot set step by step in the C# programming language
The C# code presented here is designed to generate and display an image of the Mandelbrot set, a well-known mathematical fractal characterized by complex patterns and intricate shapes. The program first calculates the set using a multi-threaded approach and then presents the resulting image within a form window.
The Mandelbrot set is defined as the collection of complex numbers c for which the sequence z(n+1) = z(n)^2 + c does not diverge to infinity. A complex number c is thus in the Mandelbrot set if, starting with z(0) = 0, the sequence z(n) remains bounded for all positive integers n.
Here's how the code works:
-
The
CalcMandelbrotSetColor
function calculates a color value for a given complex number c. This value is based on the number of iterations required for the sequence z(n) = z(n)^2 + c to diverge to infinity or reach a maximum number of iterations. -
The
GenerateBitmap
function generates a bitmap image of the Mandelbrot set. It iterates over each pixel in the bitmap, calculates the corresponding complex number c, and assigns the pixel a color based on the number of iterations required for the sequence z(n) = z(n)^2 + c to diverge to infinity. -
The
GetColor
function converts a value representing the number of iterations into a color. The value is converted to a brightness using a power function, and then used to create a color. -
The
MandelbrotSetForm
class is the main form of the program. It contains the code to create the form, generate the Mandelbrot set image, and display it within the form. -
The
thread_Proc
function is the main thread that generates the Mandelbrot set image. It starts by generating a small image to provide instant feedback to the user, and then generates the final image at a higher resolution. -
The
SetNewBitmap
function is called when the image is generated. It sets the background image of the form to the generated image. -
The
Main
function is the entry point of the program. It creates an instance of theMandelbrotSetForm
class and runs the application.
The resulting program generates an image of the Mandelbrot set and displays it within a form window. The user can interact with the image to zoom in and out, and change the center of the image.
Overall, this code provides a clear and concise way of generating and displaying the Mandelbrot set, allowing users to explore this fascinating mathematical object in an interactive manner.
Source code in the csharp programming language
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;
/// <summary>
/// Generates bitmap of Mandelbrot Set and display it on the form.
/// </summary>
public class MandelbrotSetForm : Form
{
const double MaxValueExtent = 2.0;
Thread thread;
static double CalcMandelbrotSetColor(ComplexNumber c)
{
// from http://en.wikipedia.org/w/index.php?title=Mandelbrot_set
const int MaxIterations = 1000;
const double MaxNorm = MaxValueExtent * MaxValueExtent;
int iteration = 0;
ComplexNumber z = new ComplexNumber();
do
{
z = z * z + c;
iteration++;
} while (z.Norm() < MaxNorm && iteration < MaxIterations);
if (iteration < MaxIterations)
return (double)iteration / MaxIterations;
else
return 0; // black
}
static void GenerateBitmap(Bitmap bitmap)
{
double scale = 2 * MaxValueExtent / Math.Min(bitmap.Width, bitmap.Height);
for (int i = 0; i < bitmap.Height; i++)
{
double y = (bitmap.Height / 2 - i) * scale;
for (int j = 0; j < bitmap.Width; j++)
{
double x = (j - bitmap.Width / 2) * scale;
double color = CalcMandelbrotSetColor(new ComplexNumber(x, y));
bitmap.SetPixel(j, i, GetColor(color));
}
}
}
static Color GetColor(double value)
{
const double MaxColor = 256;
const double ContrastValue = 0.2;
return Color.FromArgb(0, 0,
(int)(MaxColor * Math.Pow(value, ContrastValue)));
}
public MandelbrotSetForm()
{
// form creation
this.Text = "Mandelbrot Set Drawing";
this.BackColor = System.Drawing.Color.Black;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.ClientSize = new Size(640, 640);
this.Load += new System.EventHandler(this.MainForm_Load);
}
void MainForm_Load(object sender, EventArgs e)
{
thread = new Thread(thread_Proc);
thread.IsBackground = true;
thread.Start(this.ClientSize);
}
void thread_Proc(object args)
{
// start from small image to provide instant display for user
Size size = (Size)args;
int width = 16;
while (width * 2 < size.Width)
{
int height = width * size.Height / size.Width;
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
GenerateBitmap(bitmap);
this.BeginInvoke(new SetNewBitmapDelegate(SetNewBitmap), bitmap);
width *= 2;
Thread.Sleep(200);
}
// then generate final image
Bitmap finalBitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
GenerateBitmap(finalBitmap);
this.BeginInvoke(new SetNewBitmapDelegate(SetNewBitmap), finalBitmap);
}
void SetNewBitmap(Bitmap image)
{
if (this.BackgroundImage != null)
this.BackgroundImage.Dispose();
this.BackgroundImage = image;
}
delegate void SetNewBitmapDelegate(Bitmap image);
static void Main()
{
Application.Run(new MandelbrotSetForm());
}
}
struct ComplexNumber
{
public double Re;
public double Im;
public ComplexNumber(double re, double im)
{
this.Re = re;
this.Im = im;
}
public static ComplexNumber operator +(ComplexNumber x, ComplexNumber y)
{
return new ComplexNumber(x.Re + y.Re, x.Im + y.Im);
}
public static ComplexNumber operator *(ComplexNumber x, ComplexNumber y)
{
return new ComplexNumber(x.Re * y.Re - x.Im * y.Im,
x.Re * y.Im + x.Im * y.Re);
}
public double Norm()
{
return Re * Re + Im * Im;
}
}
You may also check:How to resolve the algorithm Emirp primes step by step in the Haskell programming language
You may also check:How to resolve the algorithm Search a list step by step in the Euphoria programming language
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the Aikido programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the F# programming language