# Mandelbrot set!

• 05-11-2012, 05:58 AM
sunde887
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:

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);                         zNext = zNext.add(input);                         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

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)){                                 set.add(current);                         }                 }         } }```
Once the set is generated I simply draw an oval for each item in the set.

Picture for anyone interested:
Attachment 3791

• 05-11-2012, 08:03 AM
JosAH
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
• 05-11-2012, 08:56 PM
Fubarable
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.
• 05-11-2012, 09:16 PM
JosAH
Re: Mandelbrot set!
Quote:

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:

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
• 05-11-2012, 09:49 PM
JosAH
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
• 05-12-2012, 03:01 AM
sunde887
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

Attachment 3796
• 05-12-2012, 10:12 AM
JosAH
Re: Mandelbrot set!
Quote:

Originally Posted by sunde887
I've still not sped it up as much as I'd like

Have a look here.

kind regards,

Jos