Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Mandelbrot set!

  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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);
    			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

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

    Picture for anyone interested:
    Mandelbrot set!-mandelbrot.png

    Thanks in advance.
    Last edited by sunde887; 05-11-2012 at 07:06 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Mandelbrot set!

    Quote Originally Posted by Fubarable View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default 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
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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

    Mandelbrot set!-z2-z-c.png

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Mandelbrot set!

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 23
    Last Post: 03-23-2012, 06:05 PM
  2. In honor of Benoît Mandelbrot. May he rest in peace.
    By Fubarable in forum Forum Lobby
    Replies: 0
    Last Post: 10-17-2010, 10:31 PM
  3. How to draw Mandelbrot in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:21 AM

Posting Permissions

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