Click here to Skip to main content
Click here to Skip to main content

Mandelbrot Set for C#

By , 23 Jun 2004
 

Introduction

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)
      {
        looper++;
        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
      b.SetPixel(s,z,cs[val]);
      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.

Zooming

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MichaelCoder
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRun dirmemberstoops1234528 Oct '09 - 6:28 
Hi
 
There is a folder saying copy all these files to the run dir. Where is the run dir?
GeneralSpeed improvementsmemberSuzetta5 Nov '06 - 3:47 
You can dump the square root, by comparing to 4.
You can also get a major speed up by using LockBits and using (unsafe) pointers.
 
As an aside, it doesn't really let you zoom in as far as you like, as you claim, because of floating point errors. But without an arbitrary precision maths package, I guess we all ahve to live with that!
GeneralJulia and Alphamembertonkozorlako15 Jun '05 - 13:21 
Very good article which presents good use of bitmap.
I know that the Julia set is a bit like the Mandelbrot set but how can it be caclulated in c#?
PS: If I use the value of percent to assign an Alpha value for the pixel of the bitmap on which the set is drawn, what effect does it have on the colour of the particular pixel?
GeneralAbout Zoomingmembersreejith ss nair13 Jun '05 - 3:29 
I am little confused with my issue. confusion is i nevar came across any where in my professional life.
let me conzise, I have a panel control with n number of movable label controls. That perticular panel cotains somewhere around 500 - 600 controls at a time. If user want to see anycontrols which is not in panel's visible area, then he need to scroll and try to fis where is is locating in panel. But my client want a facility ,which have zooming functionality. So he can see as much possible controls while using zooming option.
 
Eg: In ms Excel we can see a set of cells at a time. Suppose if user want to see some more cells than which he have, then he can reduce the zoom value and can see more cells without scrolling.
 
My client is asking the same facility.
 
Please direct me, how to come out from this problem with a solution .D'Oh! | :doh:
 
Sreejith Nair
[ My Articles ]
GeneralNice jobmemberProfox Jase30 Jun '04 - 9:14 
Hello
 
This looks very nice, its something I have wanted to get to grips with for awhile now, so thanks for that. I will have a play with it when I get the time, and the tip about using another thread may well come in handy in my asteroids game.
 
Take it easy, cheesey!Rose | [Rose]
 
Jason King
 
jason.king@profox.co.uk
Feel the love at www.profox.co.uk
GeneralLink brokenmemberjmw24 Jun '04 - 17:58 
The download link is broken. And yeah, a picture would be good for those who may not even know what a mandelbrot is without seeing one...
GeneralRe: Link brokenmemberMichaelCoder25 Jun '04 - 7:16 
Thanks. I fixed the link and I also added 1 picture of the Mandelbrot set. It is using the ColorMap file called GreenEdge.ColorMap
 
Make sure that after you download the source that you copy all of the files in "COPY THESE FILES TO THE RUN DIR" into the run dir.
 
-Michael

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 24 Jun 2004
Article Copyright 2004 by MichaelCoder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid