# Mandelbrot and Julia-Fatou help

Sorry if this is in the wrong folder, and if it is, could the mods be kind enough to move it to the right place?

This is a homework question, but I’m not looking for someone to give me an answer.

We’ve been tasked with having to generate some fractals in Java, I’ve already completed Menger’s Sponge and Sierpinski’s triangle, but the generation of the Mandelbrot and Julia-Fatou sets are giving me a bit of trouble (I have actually generated a Mandelbrot set before, but it was so long ago I’ver forgotten how I did it, although I think I used the Cartesian form, not with actual complex numbers).

I’ve tried to write the code myself, based on the limited information given to us in the notes, and some other online articles. Here’s the relevant code (I’ve only tried the Mandelbort set so far):

for(int x = 0; x < screensize.width; ++x) {
for(int y = 0; y < screensize.height; ++y) {
if(julia) {
} else {
Complex z0 = new Complex(0, 0);
Complex cons = new Complex(x, y);
int i = 0;
do {
z0.multiply(z0);
++i;
double mag = z0.magnitude();
if(mag > 2) {
offscreen.setColor(new Color(i, i, i));
offscreen.drawRect(x, y, 1, 1);
}
} while(i < iterations);
}
}
}

However, when this is plotted, all I get is a big black line running from the top left corner to the bottom right.

What I’m asking is for a hint as to where I’m going wrong. I’m not looking for someone to give me an answer (as that would count as plagiarism). Is my code close to what it should be?

The basic algorithm is essentially correct, though I would make it specifically plot a black point if it hits the maximum number of iterations. I’ll assume you’ve got the class Complex written correctly.

Things to consider:
a) What complex numbers is it really putting in for “cons” as you have it written now?
b) What does the do-loop do if the orbit leaves the critical circle (radius 2 centered at the origin) on the first iteration? i.e. follow your code manually for the point 3+0i and see what it does.

Also, I have no idea what “julia” does. Is it basically a flag to say you’re trying to plot a Julia set instead of the Mandelbrot set? If so, check to make sure it’s false.

Thanks.

As the algorithms for generating the two sets are essentially nearly identical, much of the same code can be used for both. When I get the Mandelbrot set generate correctly, I can just switch between the two algorithms by turning the “julia” flag on an off.

I’ve had it suggested to me elsewhere that the set only generates the familiar picture for |z| < 2, I take it that that is what you are hinting at with your first point, no?

I’ll have a fiddle with it based on the suggestions you gave and those given to me by others. Thanks Mathochist.

Well, the Mandelbrot set is contained in that circle. Now, how many of the points you’re looking at are in there?

You have a code problem, not necessarily a math problem.

1. x and y are integer screen coordinates, not coordinates in the “problem space”. As-is (unless something funky is going on in Complex), the Mandelbrot set would be contained in the upper-left four pixels (from 0,0 to 2,2). You need some conversion from screen coordinates to coordinates in the complex plane.

float min_x = -2.0, max_x = 2.0, min_y = -2.0, max_y = 2.0;
...
x_space = x * ( max_x - min_x ) / screensize.width;
y_space = y * ( max_y - min_y ) / screensize.height;
Complex cons = new Complex (x_space, y_space);

What you were doing was plotting into the positive quadrant of the complex plane, mirrored around the x-axis since 0,0 in screen coordinates is the top-left corner.

1. You need to break out of the while loop when the magnitude exceeds 2. Otherwise, you’ll keep overwriting that pixel with larger and larger colors, eventually reaching (255, 255, 255), which I’m assuming is also being used for points in the set.

2. you never draw the pixel if the repetitions reach “iterations”. I’m not sure if you’re intending the “default” pixel color to indicate an “in-set” point, but you may want to explicitly do a setColor() and drawRect just before going into the do-while loop. If the point doesn’t fall into the set, the pixel will get overwritten.

Thanks all for the replies. I’d figured out the problem-space error yesterday, and now my program is actually drawing something.

It starts out looking like a mandelbrot set (i.e. horizontal line from the left with blip about three quarters down) but then quickly turns into something else. I guess I’ll just have to fiddle with it a little more.

Thanks all.