Thread: ugly mandelbrot set :(

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    ugly mandelbrot set :(

    hi i am a novice to c++ and have been doodling with borland graphics interface with dev 4.9.9.1
    below is a programme i have written which calculates the mandelbrot set (all complex number c such that |z(k)| remained bounded subject to endless iteration z(n+1)=(z(n))^2+c, z(0)=c)).

    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <winbgim.h>
    
    int main(){
        initwindow(800,600);
         
          
        for(int x=0; x<=800; x++)
        {
                          for(int y=0; y<=600; y++)
                          {
                                  
                                  long double x_re = -2.0 + x*0.005;
                                  long double y_im = 1.5 - y*0.005;
                                  long double x_0 = x_re, y_0 = y_im;
                                  short terminating=0, i=0;
                                  
                                  do{
                                       x_0=(x_0*x_0-y_0*y_0+x_re);
                                       y_0=(2.0*x_0*y_0+y_im);
                                       terminating++;
                                       
                                       if(x_0*x_0+y_0*y_0>4.0){
                                                               i=1; break;
                                                               }
                                                               
                                       }while(terminating<1000);
                                       
                                       if(i) continue;
                                       else putpixel(x,y,1);
                                       
                          }
        }
        while(!kbhit());
          closegraph();
          return 0;
    }
    the problem with it is that when the graphics is generated, it looks like the mandelbrot set, but a grossly inaccurate representation. my guess is the limited number of significant figures c++ truncates for each calculation. any idea on increasing the number of significant figures? (something like what Window's calculator is capable of) thanks 4 the assistance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    if(i) continue;
    else putpixel(x,y,1);

    1.
    Code:
    if ( !i ) putpixel(x,y,1);
    2. Shouldn't you be choosing a colour based on the final value?
    Because it looks like your code produces something monochrome.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    re: ugly mandelbrot

    ok i've uploaded a picture of the gross representation of the image (actual size). have used 64 shades of grey, the whiter the pixel the closer it is to the set (i.e it doesnt diverge that fast). as u can see, the edges are spiking away, which they shouldnt be. any way to increase the precision of the calculation?

    (an accurate mandelbrot image is provided as reference)
    Last edited by Horse22; 02-25-2005 at 11:26 PM. Reason: picture supplement

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Horse22
    x_0=(x_0*x_0-y_0*y_0+x_re);
    y_0=(2.0*x_0*y_0+y_im);
    Here's your problem. The formulas are correct, but you cannot use the updated version of x_0 when you're calculating y_0. You must use the same value of x_0 when updating the real and imaginary parts.

    Here' s a solution:
    Code:
    double x_0_temp = x_0;
    x_0=(x_0*x_0-y_0*y_0+x_re);
    y_0=(2.0*x_0_temp*y_0+y_im);
    Now the image should look like the real Mandelbrot set.

    Perhaps you should consider using the complex class in the STL. That way you'll avoid low-level bugs like this.
    Last edited by Sang-drax; 02-26-2005 at 03:58 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SystemParametersInfo set wallpaper issues
    By A10 in forum Windows Programming
    Replies: 5
    Last Post: 03-14-2008, 07:39 PM
  2. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  3. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  4. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  5. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM