Goals

  • Learn about the Mandelbrot set and fractals.

  • Use functions and arrays.

  • Use turtle graphics.

  • Write a program that produces another program.

  • OS Tip of the Week

    Use the more command to view the contents of a file.

  • To quickly see the contents of a file from the command line one screenful at a time, type:
    more mand16.txt
    

  • The more command is extremely useful in conjunction with piping to view the output of your program one screenful at a time:
    a.out | more
    
    Piping can also be combined with redirection of standard input:
    mandcolor < mand16.txt | more
    
  • This works on all of the supported operating systems (Unix, OS X, Windows). If your system does not have the more command, try the less command instead.


    Windows shortcut

    To avoid having to type the full pathname c:\lcc\bin\lcc126 to run the lcc compiler, you can shortcut it to lcc126. To do this you need to put the directory c:\lcc\bin in your path. You can check your path at the command line with the commmand

    C:\> path
    
    Unfortunately, there is no uniform way to add directories to your path across different flavors of Windows. We recommend consulting a lab TA for assistance.

  • Windows NT. Use Start -> Control Panel -> System -> Environment -> User Variables. Choose the variable named PATH. If the field is empty, enter "c:\lcc\bin". If the field is nonempty, append ";c:\lcc\bin" to the end. Then click the Set button and click OK so that the settings will take effect the next time you open a command prompt.

  • Windows XP. Use Start -> Control Panel -> System -> Advanced -> Environment Variables -> User Variables. Choose the variable named PATH. If the field is empty, enter "c:\lcc\bin". If the field is nonempty, append ";c:\lcc\bin" to the end. Then click the OK button three times so that the settings will take effect the next time you open a command prompt.

  • Windows 98. You need to muck with your autoexec.bat file.

  • Input Format

    The input file begins with an integer n, followed by four real values: xmin, ymin, width, and height in this order. It is followed by a 256-by-3 table of real-valued color gradations. (There is no need to even read in the last 768 values for the part of the assignment that deals with the grayscale image.) For example, the file mand4.txt is:

    4
    -1.5
    -1.0
     2.0
     2.0
    0.000 0.000 0.734
    0.000 0.300 0.734
    . . .
    0.000 0.000 0.000
    
    If you use scanf() it won't matter whether the numbers are on the same line or not.


    Checking Your Work and Hints

  • Here is the correct sequence of Mandelbrot iterates for the point (0.125, 0.750). Your function should return 6, not 5 or 7.

    # 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

  • Run your program on some of the data files in ftp://ftp.cs.princeton.edu/pub/cs126/mandel.

  • We provide reference solutions for Windows, OS X, and arizona systems in the ftp directory.

  • Step-by-step instructions for getting started and additional hints are available here. If you are new to program and/or are struggling, this may be the best place to start. If you have some programming experienced you are probably better off by not peeking.

  • Compilation, Stdin, Stdout

  • Download and compile turtle.c is: with the following command:
    Unix:  gcc126 turtle.c -o turtle
    DOS:   lcc126 turtle.c
    

  • Compile your program as follows:
    Unix:  gcc126 mandgray.c -o mandgray
    DOS:   lcc126 mandgray.c
    

  • To read input from the file mand4.txt and send the output to the file mandgray4.ps, use the command:
    mandgray < mand4.txt | turtle > mandgray4.ps
    
    Do not use C file functions like fopen().

  • Now, you can view the PostScript program with the command:
    Unix:     gs mandgray4.ps
    Windows:  launch gsview by double-clicking mandgray4.ps
    
    You may need to download a PostScript viewer first.

  • Do not print any PostScript file unless it views properly. You could get an endless stream of gibberish.

  • Submission and readme

  • If your program doesn't produce valid turtle graphics commands or they are outside the 512 by 512 box, then rename your files to mandgray-bad.c or mandcolor-bad.c. Otherwise your grader may waste paper and effort trying to print the output. You will receive a deduction of up to 20 points if you don't follow these instructions.

  • Submit the files mandgray.c, mandcolor.c, and readme.txt using the course submission system as usual. Do not use different file names. Do not submit turtle graphics or PostScript files.

  • If you choose to submit the extra credit, submit only the input file, and give it a suggestive name, e.g., purple-seahorse256.txt. Set n to be 512 or less.

  • The readme.txt file should contain

  • Name, precept number.

  • High level description of code as specified in the readme.txt template.

  • Describe any serious problems you encountered. List whatever help that you received (including using the hints link).

  • Use at most 80 characters per line to ensure that your grader can view it properly.
  • Here's a readme file template.


  • Enrichment Links

  • There is a huge amount of information on the Web about fractals, including a Mandelbrot tutorial, a Mandelbrot applet, another Mandelbrot applet, and a Mandelbrot generator.

  • Check out the PostScript section of the COS 126 FAQ List for more information on PostScript (although you shouldn't need it for this assignment).



  • Kevin Wayne