Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Help plotting Mandelbrot set

$
0
0
I have the following code for plotting the set, but it's not working. It would be great if someone could look over my code and see where I am going wrong. Any tips for optimizing the code will be greatly appreciatedd also. (I'm afraid I can't post an image, I don't know why but the uploader isn't working)
Many thanks
:boat:

        int BlockSize = 1;  // resolution
        // set of colours to color the set.
        Color[] ColourSet = { Color.White, Color.Yellow, Color.Orange, Color.Red, 
                                Color.Black, Color.Green, Color.Gray, Color.Gold,
                            Color.Fuchsia, Color.DarkViolet, Color.DarkBlue};
        float Limit = 0f;   //I wasn't sure if the set had to be bounded by 2, could someone answer this also?

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Limit = 2f;
            float AVal = 0, BVal = 0; // real (A) and imaginary (B)/>/>/> parts of C.
            for (float xpos = 0; xpos < this.Height; xpos += BlockSize)
            {
                // Set real part between -Limit and Limit depending on location.
                AVal = (((2 * Limit) / (this.Height - 1)) * xpos) - (Limit);

                for (float ypos = 0; ypos < this.Height; ypos += BlockSize)
                {
                    // Set imaginary part between -Limit and Limit depending on location.
                    BVal = (((2 * Limit) / (this.Height - 1)) * ypos) - (Limit);

                    // Draw at location using colour defined by iterations taken to escape set
                    e.Graphics.FillRectangle(
                        new SolidBrush(ColourSet[EscapeIterations(AVal, BVal) - 1]),
                        new Rectangle((int)xpos, (int)ypos, BlockSize, BlockSize));
                }
            }
        }

        /// <summary>
        /// Returns iterations taken to escape the set
        /// </summary>
        /// <param name="a">real part of C</param>
        /// <param name="b">imaginary part of C</param>
        /// <returns></returns>
        private int EscapeIterations(float a, float B)/>/>/>
        {
            int returnVal = 0;
            float p = 0, q = 0; // real and imaginary starting points of Z
            do
            {
                p = (p * p) - (q * q) + a; // update real part of Z
                q = (2 * p * q) + b; // update imaginary part of Z

                if (returnVal == 11) break; // break loop if not brokenm by 11 iterations
                returnVal++;
            } while (((p * p) + (q * q)) <= Limit * Limit); // while abs(Z) is less than 2

            return returnVal;
        }
    }


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>