Click here to Skip to main content
12,823,976 members (44,060 online)
Click here to Skip to main content
Add your own
alternative version

Stats

88.4K views
2.9K downloads
22 bookmarked
Posted 23 Jun 2004

Mandelbrot Set for C#

, 23 Jun 2004
Rate this:
Please Sign up or sign in to vote.
This program draws the mandelbrot set in C#. It also uses XmlSerialization to save settings, copy the picture to the clipboard, or save it to a bmp file.

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

Share

About the Author

MichaelCoder
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralRun dir Pin
stoops1234528-Oct-09 7:28
memberstoops1234528-Oct-09 7:28 
GeneralSpeed improvements Pin
Suzetta5-Nov-06 4:47
memberSuzetta5-Nov-06 4:47 
GeneralJulia and Alpha Pin
tonkozorlako15-Jun-05 14:21
membertonkozorlako15-Jun-05 14:21 
GeneralAbout Zooming Pin
sreejith ss nair13-Jun-05 4:29
membersreejith ss nair13-Jun-05 4:29 
GeneralNice job Pin
Profox Jase30-Jun-04 10:14
memberProfox Jase30-Jun-04 10:14 
GeneralLink broken Pin
jmw24-Jun-04 18:58
memberjmw24-Jun-04 18:58 
GeneralRe: Link broken Pin
MichaelCoder25-Jun-04 8:16
memberMichaelCoder25-Jun-04 8:16 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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