Thursday, March 22, 2012

C# Mandelbrot set Fractal

Welcome back!

Today I'm going to show you how to build a program that does this:


















(image from Wikipedia)

But What is this?
This is a fractal based on the Mandelbrot Set. I don't really know anything on the Mandelbrot Set except what i read from Wikipedia, so here is a link on the Mandelbrot Set - Look at the "for programmers" section, this is what i looked at.

So How we Program This?
Ok, From what I understood from Wikipedia the fractal we are trying to represent is based on the following formula:





when c is a complex number.
(info from Wikipedia)

Which means we need to programmatically consider the real and imaginary values of x and y.
As far as i understood, we are checking if the x and y values that we put in the formula above is not going above infinity, if they do they are not in the set.

So the whole program I built is based on the pseudocode on Wiki.
I added an extra thing that i found on the web, to make it more interesting I'm taking the number of times the loop went for every pixel and if it's not the max value(not in the set) i'm using the following code to paint it:

You can play with the colors if you change this code.

Code Explanation:
Ok, so what I'm doing in my code is as follows:
1. I define a bitmap that is representing the real axis and imaginary axis, which means that the middle of the bitmap is actually the (0,0) point of the grid, and as written in Wiki "for programmers" section i define the left edge as -2, the right edge as 2, the upper edge as 2 and the down edge as -2.
now we defined a grid.

2. I'm running on every pixel on the bitmap, calculating it's x and y value and then starting the loop that checks if it's in the Mandelbrot Set.

3.Calculating and printing the color of the pixel and then starting again with the next pixel.

The Program Output's this:


Now I added a zooming function, to zoom in you just click on the bitmap.
How Does It Work?
I just define new borders to the bitmap, something less than two.
So If you zoom in you can get quite astonishing pictures, as so:



Now you have to remember that this program isn't accurate!
Why?
The program relies on "double" type numbers, that has limited accuracy, so the more zoomed in you get The more it is not accurate (at first it's accurate but after zooming in it won't be).

I added another couple of things:
Save Button - saves the current bitmap image to desktop.
Reset Button - to start from the beginning.

So here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Fractals
{
    public partial class Form1 : Form
    {
        static double currentmaxr = 0;
        static double currentminr = 0;
        static double currentmaxi = 0;
        static double currentmini = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Bitmap img = MandelbrotSet(pictureBox1, 2, -2, 2, -2);
            pictureBox1.Image = img;
        }
        static Bitmap MandelbrotSet(PictureBox pictureBox1, double maxr, double minr, double maxi, double mini)
        {
            currentmaxr = maxr;
            currentmaxi = maxi;
            currentminr = minr;
            currentmini = mini;
            Bitmap img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            double zx = 0;
            double zy = 0;
            double cx = 0;
            double cy = 0;
            double xjump = ((maxr - minr) / Convert.ToDouble(img.Width));
            double yjump = ((maxi - mini) / Convert.ToDouble(img.Height));
            double tempzx = 0;
            int loopmax = 1000;
            int loopgo = 0;
            for (int x = 0; x < img.Width; x++)
            {
                cx = (xjump * x) - Math.Abs(minr);
                for (int y = 0; y < img.Height; y++)
                {
                    zx = 0;
                    zy = 0;
                    cy = (yjump * y) - Math.Abs(mini);
                    loopgo = 0;
                    while (zx * zx + zy * zy <= 4 && loopgo < loopmax)
                    {
                        loopgo++;
                        tempzx = zx;
                        zx = (zx * zx) - (zy * zy) + cx;
                        zy = (2 * tempzx * zy) + cy;
                    }
                    if (loopgo != loopmax)
                        img.SetPixel(x, y, Color.FromArgb(loopgo % 128 * 2, loopgo % 32 * 7, loopgo % 16 * 14));
                    else
                        img.SetPixel(x, y, Color.Black);

                }
            }
            return img;

        }


        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            int ex = e.X;
            int ey = e.Y;
            double currentxjump = ((currentmaxr - currentminr) / Convert.ToDouble(pictureBox1.Width));
            double currentyjump = ((currentmaxi  - currentmini) / Convert.ToDouble(pictureBox1.Height));

            int zoomx = pictureBox1.Width/5 ;
            int zoomy = pictureBox1.Height/5;
            Bitmap img = MandelbrotSet(pictureBox1,((ex +zoomx) * currentxjump) -Math.Abs(currentminr) , ((ex-zoomx) * currentxjump) -Math.Abs(currentminr) , ((ey+zoomy ) * currentyjump) - Math.Abs(currentmini) , ((ey- zoomy) * currentyjump) - Math.Abs(currentmini));
            pictureBox1.Image.Dispose();
            pictureBox1.Image = img;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           Bitmap img = MandelbrotSet(pictureBox1, 2, -2, 2, -2);
            pictureBox1.Image.Dispose();
            pictureBox1.Image = img;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //string folderf = @"\Fractals";
            string filename = @"\Fractals";
            string filetype = @".jpeg";
            int mone=0;
            
            while (File.Exists(path + filename + mone.ToString() + filetype))
            {
                mone++;
            }
           
            pictureBox1.Image.Save(path + filename + mone.ToString() + filetype);
            MessageBox.Show("Saved To Desktop");

            
        }
    }

}





And here is the downloading link:
Download This Project (rar file)

Run without compiling: Go to the project folder -> bin -> debug -> Fractals.exe
Have Fun!
And Remember
Questions = Comments!

4 comments:

  1. Hey this is fantastic. Thanks

    ReplyDelete
  2. I don't get the color explaination..

    ReplyDelete
  3. the code seems correct but it doesnt load in the picturebox even after defining the load function. shouldnt there be a draw button? (just my opinion tho... your code helped me a lot as this is my project :) )

    ReplyDelete
  4. The link for the code no longer works. Is there a Github?

    ReplyDelete