Part 0:  preparation  
  • Copy the following files from /u/cs126/files/mandel/ to an empty directory:
    mand4.txt     mand128.txt
    mand5.txt
    
    You may do this with the following command:
    mkdir mandel                        # make a new directory
    chmod 700 mandel                    # set directory permissions
    cd mandel                           # move to that directory
    cp /u/cs126/files/mandel/* .        # copy all files to current directory
    

  • Part 1:   Mandelbrot function

  • First, write the function mand(). Be sure this is completely debugged before continuing. As a check, the point (0.125, 0.75) is not in the set, since after the 6th iteration, r*r + s*s > 4. Thus, your mand function should return the integer 6.

    # 0 1 2 3 4 5 6
     r   0.125000 -0.421875 -0.575928  0.455010 -0.303564 -1.959975  3.945239
    s  0.750000  0.937500 -0.041016  0.797244  1.475509 -0.145822  1.321613

  • Be sure to update the variables r and s simultaneously, using the old values of r and s. Otherwise, your plot will look like a "squished" Mandelbrot set.

  • Be sure your function is returning the proper number of iterations, and is not committing an "off-by-one" error. This can reak havoc later on. The iteration count should always be between 0 and 255.

  • Part 2:   Gray-scale image

  • Read in the values n, xmin, ymin, xmax, ymax from standard input. Print them out to make sure you did this properly.

  • Before trying to plot the points, first write nested loops to print out the x and y coordinates of the points you will soon be plotting. The mand4.txt file data says to plot a 4-by-4 grid with lower left endpoint (-1.5, -1.0) and upper right endpoint (0.5, 1.0). Using this data file, you should get the following:
    (-1.25, -0.75)  (-1.25, -0.25)  (-1.25,  0.25)  (-1.25,  0.75)  
    (-0.75, -0.75)  (-0.75, -0.25)  (-0.75,  0.25)  (-0.75,  0.75)  
    (-0.25, -0.75)  (-0.25, -0.25)  (-0.25,  0.25)  (-0.25,  0.75)  
    ( 0.25, -0.75)  ( 0.25, -0.25)  ( 0.25,  0.25)  ( 0.25,  0.75)  
    
    Check that your code works properly with other data files as well.

  • Now call the mand() function and print out the number of iterations for each point. For mand4.txt you should get
    (-1.25, -0.75):   2   (-1.25, -0.25):   8   (-1.25,  0.25):   8   (-1.25,  0.75):   2   
    (-0.75, -0.75):   3   (-0.75, -0.25):  12   (-0.75,  0.25):  12   (-0.75,  0.75):   3   
    (-0.25, -0.75):  21   (-0.25, -0.25): 255   (-0.25,  0.25): 255   (-0.25,  0.75):  21   
    ( 0.25, -0.75):   4   ( 0.25, -0.25): 255   ( 0.25,  0.25): 255   ( 0.25,  0.75):   4 
    

  • Next, translate and rescale the x and y coordinates appropriately, so that they are between 0 and 512. Use pencil and paper (and some basic algebra) to determine the correct translation and scaling factors. You should get the following.
    ( 64.0,  64.0):   2   ( 64.0, 192.0):   8   ( 64.0, 320.0):   8   ( 64.0, 448.0):   2   
    (192.0,  64.0):   3   (192.0, 192.0):  12   (192.0, 320.0):  12   (192.0, 448.0):   3   
    (320.0,  64.0):  21   (320.0, 192.0): 255   (320.0, 320.0): 255   (320.0, 448.0):  21   
    (448.0,  64.0):   4   (448.0, 192.0): 255   (448.0, 320.0): 255   (448.0, 448.0):   4
    

  • The above gives the centers of the rectangles that you will draw, but the PostScript function rectfill needs the lower left endpoints. Modify your code so that it prints the lower left endpoint, but still does the Mandelbrot calculation using the center point.
    (  0.0,   0.0):   2   (  0.0, 128.0):   8   (  0.0, 256.0):   8   (  0.0, 384.0):   2   
    (128.0,   0.0):   3   (128.0, 128.0):  12   (128.0, 256.0):  12   (128.0, 384.0):   3   
    (256.0,   0.0):  21   (256.0, 128.0): 255   (256.0, 256.0): 255   (256.0, 384.0):  21   
    (384.0,   0.0):   4   (384.0, 128.0): 255   (384.0, 256.0): 255   (384.0, 384.0):   4   
    

  • Now figure out the length of each side of the rectangle to be plotted. It should depend on n, xmax-xmin and ymax-ymin. Also, convert the iteration count t into a gray-scale value using the formula 1.0 - t/255.0. You are now ready to produce the PostScript output.
    %!
    50 50 translate
    0 0 512 512 rectstroke
    0.992 setgray   0.000   0.000 128.000 128.000 rectfill
    0.969 setgray   0.000 128.000 128.000 128.000 rectfill
    0.969 setgray   0.000 256.000 128.000 128.000 rectfill
    0.992 setgray   0.000 384.000 128.000 128.000 rectfill
    
    0.988 setgray 128.000   0.000 128.000 128.000 rectfill
    0.953 setgray 128.000 128.000 128.000 128.000 rectfill
    0.953 setgray 128.000 256.000 128.000 128.000 rectfill
    0.988 setgray 128.000 384.000 128.000 128.000 rectfill
    
    0.918 setgray 256.000   0.000 128.000 128.000 rectfill
    0.004 setgray 256.000 128.000 128.000 128.000 rectfill
    0.004 setgray 256.000 256.000 128.000 128.000 rectfill
    0.918 setgray 256.000 384.000 128.000 128.000 rectfill
    
    0.984 setgray 384.000   0.000 128.000 128.000 rectfill
    0.004 setgray 384.000 128.000 128.000 128.000 rectfill
    0.004 setgray 384.000 256.000 128.000 128.000 rectfill
    0.984 setgray 384.000 384.000 128.000 128.000 rectfill
    
    showpage
    

  • Make sure that your program still works properly with other data files.

  • A few helpful tips.

  • To print a % sign, use printf("%%");

  • Because of integer arithmetic 2 / 32 is computed to be 0. To get a floating point approximation, you can use 2.0 / 32.

  • Part 3:  Color image

    In this part, you will create a glorious color image of the Mandelbrot set.

  • This program is very similar to gray-scale one, so use the file mandgray.c as a starting point. You can copy it to mandcolor.c with the command:
    cp mandgray.c mandcolor.c
    

  • Declare three 256-element arrays of doubles, say red[], green[], and blue[], and read in the color values stored in the data file using scanf(). Remember array indices start at 0! Print out their values in a table to make sure everything is working.

  • Once you have done this, it should not take much effort to finish off the color version.



  • Kevin Wayne