# Help plotting Mandelbrot set

Page 1 of 1

## 4 Replies - 771 Views - Last Post: 13 January 2013 - 10:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307029&amp;s=0b156b52e11acbd7f608d63d92139246&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 3
• 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

```        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

Reputation: 5856
• Posts: 12,754
• 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.

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

Reputation: 3
• 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.

### #4 tlhIn`toq

Reputation: 5856
• Posts: 12,754
• 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.

Reputation: 3
• 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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }