# Thread: Mandelbrot set!

1. ## Mandelbrot set!

Hey guys,

I haven't been doing any interesting projects lately (besides class projects, which aren't very interesting). So, as an exercise I decided to tackle mandelbrot sets. It's actually been quite simple and the majority of my code was representing Complex numbers. My program is the inefficiency I am running into.

I am wondering how to make a quicker method to test for convergence. Here is what I've got:

Java Code:
```private boolean converges(ComplexNum input){
int count = 0;
ComplexNum start = input;
ComplexNum zCurrent = new ComplexNum();
ComplexNum zNext = new ComplexNum();
while(zCurrent.getReal() < 2 && zCurrent.getImaginary() < 2
&& count < 45){
zNext = zCurrent.mult(zCurrent);
zCurrent = zNext;
++count;
}

if(zCurrent.getReal() < 2 && zCurrent.getImaginary() < 2){
return true;
}

return false;
}```
To actually construct the set, I've used this simple piece of code

Java Code:
```public void generateSet(){
for(double i = -2; i < 2; i += .001){
for(double j = -2; j < 2; j+= .001){
ComplexNum current = new ComplexNum(i, j);
if(converges(current)){
}
}
}
}```
Once the set is generated I simply draw an oval for each item in the set.

Picture for anyone interested:

Last edited by sunde887; 05-11-2012 at 06:06 AM.

2. ## Re: Mandelbrot set!

There's a horizontal axis of symmetry (i.e. if (x,y) is an element of your set so is (x,-y)); this speeds up the entire thing by a factor of 2.

kind regards,

Jo

3. ## Re: Mandelbrot set!

I don't know about speeding up the algorithms, but I do know that the images of this and the corresponding Julia sets get a whole lot more interesting when you assign different colors for different speeds of divergence (not sure if that is the right word), as my forum image demonstrates.

4. ## Re: Mandelbrot set!

Originally Posted by Fubarable
I don't know about speeding up the algorithms, but I do know that the images of this and the corresponding Julia sets get a whole lot more interesting when you assign different colors for different speeds of divergence (not sure if that is the right word), as my forum image demonstrates.
Especially when you do it incorrectly, like I once did:

Java Code:
```private Color getIterationColor(int i) {
if (i < 255) return new Color(0x100100120*i);
else return Color.BLACK;
}```
The result is psychedelic ;-)

kind regards,

Jos

5. ## Re: Mandelbrot set!

b.t.w. your stop condition in the while loop isn't correct; it should be |c| < 2, i.e. for c == a+b*i, a*a+b*b < 4

kind regards,

Jos

6. ## Re: Mandelbrot set!

I've still not sped it up as much as I'd like, but I've added color, and made it a bit more general.

Are Julia sets anytime it is iterating a function other than z^2+c.

So far the most interesting I've gotten is z^2+z+c, which gives

7. ## Re: Mandelbrot set!

Originally Posted by sunde887
I've still not sped it up as much as I'd like
Have a look here.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•