Jump to content

Mandelbrot Set

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
thomasw234

thomasw234

    Newbie

  • Members
  • Pip
  • 1 posts
Hi!

I'm trying to implement a very basic fractal (the Mandelbrot set) generator, and from looking on the internet I've come to the conclusion that this is the correct code. However, all I am left with is a black image, with a few pixels in the top left corner that are white. I've written my own Complex Number class, and proved it works, so I don't think it's worth posting on here (but correct me if I'm wrong).

Anyway, this is the code to generate a Bitmap:

public Bitmap CalculateFractal()

        {

            Bitmap bImage = new Bitmap(640, 480);

            int nMaxIterations = 100;


            for (int y = 0; y < bImage.Height; y++)

            {

                for (int x = 0; x < bImage.Width; x++)

                {

                    ComplexNumber Z = new ComplexNumber(0, 0);

                    ComplexNumber C = new ComplexNumber(x, y);


                    if (x == 5 && y == 5)

                    {

                        ;

                    }


                    int nCount = 0;

                    for (nCount = 0; Z.Modulus <= 2.0m && nCount < nMaxIterations; nCount++)

                    {

                        Z = (Z * Z) + C;

                    }


                    if (nCount < nMaxIterations)

                    {

                        bImage.SetPixel(x, y, Color.Black);

                    }

                    else

                    {

                        bImage.SetPixel(x, y, Color.White);

                    }

                }

            }

            return bImage;

        }

Any help is appreciated! I realise this is more of a maths question than C#, as I understand the code part!

Thanks,

Thomas.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You've got a couple problems.
1) The Mandelbrot set lives inside -1-i to 1+i (approximately). Your code is almost entirely outside the Mandelbrot set.
2) Your code doesn't evaluate whether abs(Z) exceeded a value or not. This is what is usually used to determine the color.

You would do well to look at Fractint. It's an open source fractal program that will go into a LOT of detail on how to create the Mandelbrot set.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog