C++ C++ C# C# ASP.NET Security ASP.NET Security ASM ASM Скачать Скачать Поиск Поиск Хостинг Хостинг  
  Программа для работы с LPT портом...
Язык: .NET — ©Alexey...
  "ASP.NET Atlas" – AJAX в исполнении Micro...
Язык: .NET — ©legigor@mail.ru...
  "Невытесняющая" Многопоточность...
Язык: C/C++ — ©...
  Update World C++: Сборник GPL QT исходников
  Весь сайт целиком можно загрузить по ссылкам из раздела Скачать

 Mandelbrot Set for C# / Graphics / C#


This program will draw the Mandelbrot set in C#. It allows you to zoom in to any measure. It also supports ColorMaps to style the set. Each ColorMap file has 256 lines, one for each RGB value calculated by the Mandel set.

The Calculation

Now, let me make this perfectly clear. This article is not about the actual calculation of the Mandelbrot set. I do not claim to have come up with this code. I just found a C++ version and converted it to C# (not that hard at all). Here is the function that calculates the Mandel set:

private void DrawMandel()
  // Holds all of the possible colors
  Color[] cs = new Color[256];
  // Fills cs with the colors from the current ColorMap file
  cs = GetColors(ColMaps[CurColMap]);
  // Creates the Bitmap we draw to
  Bitmap b = new Bitmap(this.Width,this.Height);
  // From here on out is just converted from the c++ version.
  double x, y, x1, y1, xx, xmin, xmax, ymin, ymax = 0.0;

  int looper, s, z = 0;
  double intigralX, intigralY = 0.0;
  xmin = Sx; // Start x value, normally -2.1
  ymin = Sy; // Start y value, normally -1.3
  xmax = Fx; // Finish x value, normally 1
  ymax = Fy; // Finish y value, normally 1.3
  intigralX = (xmax - xmin) / this.Width; // Make it fill the whole window
  intigralY = (ymax - ymin) / this.Height;
  x = xmin;

  for(s = 1; s < this.Width; s++)
    y = ymin;
    for(z = 1; z < this.Height; z++)
      x1 = 0;
      y1 = 0;
      looper = 0;
      while(looper < 100 && Math.Sqrt((x1 * x1) + (y1 * y1)) < 2)
        xx = (x1 * x1) - (y1 * y1) + x;
        y1 = 2 * x1 * y1 + y;
        x1 = xx;

      // Get the percent of where the looper stopped
      double perc = looper / (100.0);
      // Get that part of a 255 scale
      int val = ((int)(perc * 255));
      // Use that number to set the color
      y += intigralY;
    x += intigralX;
  bq = b; // bq is a globally defined bitmap
  this.BackgroundImage = (Image)bq; // Draw it to the form

I would recommend that you run this method on a different thread so it doesn't tie up your app.


The zooming concept was a hard one to get nailed down. I knew what had to be done, but it took me a while to figure out how to do it. What you have to do is change Sx, Sy, Fx, and Fy. If you change where the method starts and finishes, the function will decrease how much the value changes each time. I did this by getting the percent of where you clicked and multiplying it by the x and y integrals.

One of the only problems with zooming was keeping an aspect ratio. I solved this problem by setting a point where the mouse is first clicked and, on MouseMove and MouseUp, calculating the height of the rectangle using the width of the rectangle. This seems to work OK for me. It is a bit slow when making a large selection, but it works.

Side notes

My mouse has 3 buttons. I used the left one to zoom, the right one to reset back to normal values, and the middle one to change the color maps. The only problem is that there are more than 3 things you can do in this app. For example, you can also save settings and open old settings. Thus, I also had to incorporate keyboard commands.

Yes, I know my code isn't commented very well, but if you do not understand a piece of it, just post the question in the forum below.

One last note, you'll notice that there are no pictures with this article. You want to know why? Because the possibilities are endless with the Mandelbrot set. I couldn't show you just 1 picture and say, "That is the Mandelbrot set." because there is so much more to it. I will try to post some pictures I have gotten from this app somewhere on the web and put the address here.

On second thought, here is one picture of the Mandelbrot. This is with no zoom or anything.

Mandelbrot Set

Letyshops [lifetime]