Thread: MandelBrot

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    21

    MandelBrot

    Hi,

    I have an assignment to make a simple mandelbrot program. They have kindly given us the code to get started with and all we have to do is assign values a column position.

    I'm a bit lost thou as we haven't been shown how to do anything like this yet, your lecture today was all about scanf, so it seems a bit of a leap.

    I'd appreciate a point in the right direction for some good reading regarding C programming and column positions.

    Thanks in advanced.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    After a bit more google searching I'm thinking I should be looking at pointers and there use with rows and columns.

    I need to be able to give an x and y coordinate to each value within a grid 80x53.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    I'm not 100% about what your difficulty is, but I'll assume that my best guess is right.

    You are unsure how you get a position based on the mandelbrot transformation equation?

    When you draw a mandelbrot set, you are in fact drawing an Argand Diagram. That means that your x and y positions are determined by the complex value you calculate from the iterative sequence used to generate the Mandelbrot Set. I.e. you take a complex number in and you do z(n+1) = z(n)^2 + c where the '(n)' represents the iteration number. Because the numbers you are dealing with are in the form a + bi, then most of the time you will end up with a number of the form c + di where c and d represent your x and y values respectively.

    Wikipedia even has some pseudocode for you to copy:
    Code:
    For each pixel on the screen do:
    {
      x0 = scaled x co-ordinate of pixel (must be scaled to lie somewhere in the interval (-2.5 to 1)
      y0 = scaled y co-ordinate of pixel (must be scaled to lie somewhere in the interval (-1, 1)
    
      x = 0
      y = 0
    
      iteration = 0
      max_iteration = 1000
    
      while ( x*x + y*y <= (2*2)  AND  iteration < max_iteration )
      {
        xtemp = x*x - y*y + x0
        y = 2*x*y + y0
    
        x = xtemp
    
        iteration = iteration + 1
      }
    
      if ( iteration == max_iteration )
      then
        color = black
      else
        color = iteration
    
      plot(x0,y0,color)
    }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    Thanks for the reply.

    Thats sort of what I'm after. I'm been asked to work out the column postion and row position. There must be a simple bit of maths to do this, as were only in our first few weeks, that looks way to complicated. I've got the code all working, but with out the right maths its drawing things that look like christmas trees. Also I'm outputting as either a character or a space to drawer the shape, not pixels.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    So to make it a bit more clear.

    I have the mandlebrot code, and I have to give it the column positions for a set amount of character that will be 50 characters wide by 30 lines high. These have to be in double format between -1.5 and 1.5 or the columns and -1 and 1 for the lines.

    I'd really appreciate some help on the maths to get this to work as I'm so very lost.

    Thanks.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please - work with the code that you've "kindly been given as a start", and post up what you have, altogether.

    Your description of the problem is almost void of any details or work that you've done. Have you read up on mandelbrot functions, at all? You have to give this assignment a start to get it rolling.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    Code:
    #include <stdio.h>
    
    int it;
    double r, i;
    double r2, i2;
    double tmp;
    double x, y;
    int column = 1;
    int row = 1;
    
    int main (int argc, char *argv[])
    {
    
    
    while((column < 80)&&(row < 54))
    {
            
            y = (row/ column );
            x = (column / row);
            
    
    it = 4096;
    r = i = 0; r2 = 0; i2 = 0;
    do
    {
            tmp = r2 - i2 + x;
            i = 2 * r * i * y;
            r = tmp;
            r2 = r*r; i2 = i * i;
    } while ((r2 + i2) <= 4.0 && --it);
    {
    
    }
            if(it == 0)
    {
            printf(" ");
    }
    else
    {
            printf("*");
    }
    
            column++;
    
            if(column == 81)
    {
            printf("\n");
            column = 1;
    		row++;
    }
    }
    
            if(row == 53)
    {
            return 0;
    }
    }
    Here's what I have, the part I'm struggling with is getting the columns/rows coordinates. For the mandelbrot to work I have to give it the (x) "column position" converted into a into a double between -1.5 and 1.5 and the row value (y) between -1 and 1.

    Depending on weather the code outputs a 1 or a 0 the program prints either an * or a space which then forms the picture.

    Like I said at the start this is an assignment and I'd really like your expertises on what it is I need to understand to make it work, because if you just do it for me I won't learn an I'm keen to crack it myself but have come to a dead end.

    Thanks again.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh, don't worry -- I won't do it for you -- I promise!!

    if you print on the 80th column, the screen will tear up your drawing, by automatically moving the cursor to the next row and first column. I would use if(col==79). or 78.

    I haven't done madelbrot's before, so I'm struggling right along with you.

    Are you on a windows rig, BSD'ish (Apple), or Linux?
    Last edited by Adak; 10-22-2010 at 08:45 PM.

  9. #9
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Ok, now I think I understand. Your problem is the transformation relations between the rows, columns and the x, y parameters?

    If that is indeed the problem you are having you just need to define your x and y coordinates as follows:
    Code:
    x = 1.5*( (2*c/c_m) - 1 )
    y = (2*r/r_m) - 1
    Where c_m and r_m is the maximum column and row values considered. In this case that would be 80 and 54. Note that if you want your x value to start at -1.5 exactly then you would have to count your columns from zero rather than one (similar argument applies for y).

    As far as I can tell that is your only problem. I now understand what you are trying to do. You should have posted the code from the start - it was much more helpful than your description of the problem.

    P.S. Also, listen to Adak's advice about the columns otherwise you will get 'sheering' in your output.
    Last edited by Swarvy; 10-23-2010 at 12:12 AM.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    Heres the updated code.

    I'm sure the maths is right now thanks to you, but I think perhaps the way I'm going about giving it the columns and rows is wrong? At the moment they both start at 0 and each time the loop runs it adds 1 to the column till it reaches 80 and then a new line is made and the row counter has 1 added. An on it goes till it reaches 53 rows when the program stops.

    Thanks for the help, would have annoyed me if I'd had to wait till monday!

    I believe the system is Unix running on Sun Solaris machines.

    Code:
    #include <stdio.h>
    
    int it;
    double r, i;
    double r2, i2;
    double tmp;
    double x, y, x1;
    int column = 0;
    int row = 0;
    
    int main (int argc, char *argv[])
    {
    
    
    while((column < 81)&&(row < 54))
    {
    
            x = 1.5*( (2*column/80) - 1 );
            y = (2*row/54) - 1;
    
    it = 4096;
    r = i = 0; r2 = 0; i2 = 0;
    do
    {
            tmp = r2 - i2 + x;
            i = 2 * r * i * y;
            r = tmp;
            r2 = r*r; i2 = i * i;
    } while ((r2 + i2) <= 4.0 && --it);
    {
    
    }
            if(it == 0)
    {
            printf(" ");
    }
    else
    {
            printf("*");
    }
    
            column++;
    
            if(column == 81)
    {
            printf("\n");
            column = 0;
            row++;
    }
    }
    
            if(row == 53)
    {
            return 0;
    }
    }

  11. #11
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    You need to be more specific about what you think the problem is. To you, the phrase 'I think perhaps the way I'm going about giving it the columns and rows is wrong?' may have a clear meaning but to the rest of us it seems very ambiguous. In what way do you think the way you are specifying the rows and columns is wrong?

    To make the code a bit clearer you could try doing it this way:
    Code:
    unsigned int column_max = 80;
    unsigned int row_max = 54;
    int i,j;
    
    for(i=0; i < column_max; i++)
    {
           for(j=0; j < row_max; j++)
           {
                   /* Do your fractal code here - I.e. do-while loop, and your if statements */
           }
    }
    Your while loops inside one another makes the program notation quite confusing. The advantage of doing it this way is that the program won't just terminate when it hits the 53rd row. It will do the whole row and then stop.

    Obviously if you use 'i' as your column index you can't use it in your fractal calculation loop so you would have to re-assign another index to that.

    Is that what you mean by the phrase: 'I think perhaps the way I'm going about giving it the columns and rows is wrong?'?

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Swarvy View Post
    You need to be more specific about what you think the problem is. To you, the phrase 'I think perhaps the way I'm going about giving it the columns and rows is wrong?' may have a clear meaning but to the rest of us it seems very ambiguous. In what way do you think the way you are specifying the rows and columns is wrong?

    To make the code a bit clearer you could try doing it this way:
    Code:
    unsigned int column_max = 80;
    unsigned int row_max = 54;
    int i,j;
    
    for(i=0; i < column_max; i++)
    {
           for(j=0; j < row_max; j++)
           {
                   /* Do your fractal code here - I.e. do-while loop, and your if statements */
           }
    }
    Your while loops inside one another makes the program notation quite confusing. The advantage of doing it this way is that the program won't just terminate when it hits the 53rd row. It will do the whole row and then stop.

    Obviously if you use 'i' as your column index you can't use it in your fractal calculation loop so you would have to re-assign another index to that.

    Is that what you mean by the phrase: 'I think perhaps the way I'm going about giving it the columns and rows is wrong?'?
    Bip Bip Bip Crrr Crrr
    compiler warning:

    Comparison between signed and unsigned integer.


    Bip Bip Bip

    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    My latest hash up. Still not getting any pretty pictures. I much prefer this way or running the loops, seems less convoluted than before, so thank you for that.

    Code:
    #include <stdio.h>
    
    int it;
    double r, i;
    double r2, i2;
    double tmp;
    double x, y, x1;
    unsigned int column_max = 80;
    unsigned int row_max = 54;
    int col, rwo;
    
    int main (int argc, char *argv[])
    {
    for(col=0; col < column_max; col++)
    {
           for(rwo=0; rwo < row_max; rwo++)
           {
                    x = 1.5*((2*col/column_max) - 1);   //column_max = maximum column considered
                    y = ((2*rwo/row_max) - 1);                //row_max = row maximum considered
    
    
    it = 4096;
    r = i = 0; r2 = 0; i2 = 0;
    do
    {
            tmp = r2 - i2 + x;
            i = 2 * r * i * y;
            r = tmp;
            r2 = r*r; i2 = i * i;
    } while ((r2 + i2) <= 4.0 && --it);
                    }
            if(it == 0)
    {
            printf(" ");
    }
    
    else
    {
            printf("*");
    }
    
            if(col == 80)
    {
            printf("\n");
    }
    
    }
    
    }

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    After playing around with this for 4 days I've got to this point. I'm 100% sure it should print out the mandlebrot but it doesn't. The column and row index increases nicely and the counts work to make a new line at 80 characters and the program stops at 53 lines.

    Do any genius' out there know why its not working as its driving my nuts!


    Code:
    #include <stdio.h>
    int it;
    double r, i;
    double r2, i2;
    double tmp;
    double x, y, x1;
    int column;
    int row;
    int max_row;
    int zero;
    
    int main (int argc, char *argv[])
    {
    
    x = -1.5;
    y = -1.0;
    column = 0;
    row = 0;
    max_row = 53;
    zero = 0;
    
    while((x <= 1.5)&&(y <= 1))
    {
    it = 4096;
    r = i = 0; r2 = 0; i2 = 0;
    do
    {
            tmp = r2 - i2 + x;
            i = 2 * r * i * y;
            r = tmp;
            r2 = r*r; i2 = i*i;
    } while ((r2 + i2) <= 4.0 && --it);
    
    x = x + (3.0/79.0);
    column++;
    
    if(it <= 0)
    {
            printf(" ");                                
    }
    else
    {
            printf("*");                                
    }
    
    if(column > 79)
    {
    	printf("\n");	
    	column = 0;
        x = -1.5;
        y = y + (2.0/53.0);
        row++;
    }
    
    }
    }

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    i = 2 * r * i * y;
    ... I think this should be +

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mandelbrot set program improvements
    By mad_muppet in forum Game Programming
    Replies: 3
    Last Post: 07-14-2010, 05:58 AM
  2. Problems with timeval
    By ErMenGoL in forum C Programming
    Replies: 3
    Last Post: 04-22-2010, 10:59 AM
  3. Mandelbrot tutorial?
    By PseudoSane in forum C Programming
    Replies: 7
    Last Post: 11-07-2006, 02:02 PM
  4. I want to draw a mandelbrot set ..
    By mad_muppet in forum C Programming
    Replies: 16
    Last Post: 08-18-2006, 12:43 AM
  5. ugly mandelbrot set :(
    By Horse22 in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2005, 03:55 PM