4 Replies - 702 Views - Last Post: 13 January 2013 - 10:55 AM Rate Topic: -----

#1 JizzaDaMan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 23-May 11

Help plotting Mandelbrot set

Posted 13 January 2013 - 05:31 AM

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;
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Help plotting Mandelbrot set

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,186
  • Joined: 02-June 10

Re: Help plotting Mandelbrot set

Posted 13 January 2013 - 07:45 AM

Quote

it's not working

doesn't give us anything to go on. We're not your debug monkeys.

  • You are getting an error? What does this error mean?
  • Your code isn't doing what you expect it to?
  • you want someone to design your algorithm for you because you can't think of a way it should work?


In other words... You have to actually tell us what is going wrong if you expect us to be able to help you. We aren't standing over your shoulder looking at your project.

: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 JizzaDaMan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 23-May 11

Re: Help plotting Mandelbrot set

Posted 13 January 2013 - 10:37 AM

Sorry.
the program compiles and runs perfectly well, but it doesn't plot the easily recognisable shape, so it is a logic error. I have looked over it many times and haven't been able to find anything wrong with my logic, but there is evidently something wrong, so I posted it here to see if anyone else could find the problem. I am assuming it's an error where I am iterating the formula (Z = Z^2 + C) in the EscapeIterations function.

Apologies for the ambiguity, it would have been obvious if I had been able to upload an image of the result.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,186
  • Joined: 02-June 10

Re: Help plotting Mandelbrot set

Posted 13 January 2013 - 10:46 AM

Posting of embedded of photos is down while management works out some bugs.
But you can post a photo elsewhere and link it here.
Was This Post Helpful? 0
  • +
  • -

#5 JizzaDaMan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 23-May 11

Re: Help plotting Mandelbrot set

Posted 13 January 2013 - 10:55 AM

This is the result of my code so far. As you can see, it does not resemble the Mandelbrot set.

http://tinypic.com/r/149bn08/6
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1