COS 126

Recursive Graphics
Programming Assignment 4

Due: Wednesday, 11:59pm

Write a program that can create an H-tree pattern, on the back of this page. Then develop a program that prints recursive patterns of your own design. The H-tree is another example of a fractal pattern like the Mandelbrot set. It also of practical interest in circuit design. For example, imagine that we want to lay out circuit components on a microprocessor chip so that the length of wire connecting each component to the clock is the same. (This property is desirable to achieve uniform wiring delays.) The H-tree pattern gives a practical solution: position the clock in the center of the biggest H, and put each circuit component at one of the exposed endpoints of the smallest H's.

Part 1.   Though the H-tree pattern looks complex, it can be generated with a recursive program very similar (in fact, identical!) to the one that draws the pattern in Fig 5.12 in Algorithms in C. Your task is to make a file htree.c with a recursive function and a main() driver that will call the recursive program once and output a PostScript program (in the manner to which we have become accustomed) to produce the desired effect. Think recursively: write a program that draws an H and calls itself 4 times (and includes a termination condition).

Your program will read in an integer n using scanf() that controls the depth of the recursion. First, make sure that your program can draw a single H, with n set to 1. Then, check that it works for 2 levels of recursion (draws five H's). That is, your program should produce the following PostScript code when n is 2.
%!
50 50 translate
2 setlinewidth
128 256 moveto 256   0 rlineto stroke
128 128 moveto   0 256 rlineto stroke
384 128 moveto   0 256 rlineto stroke
 64 384 moveto 128   0 rlineto stroke
 64 320 moveto   0 128 rlineto stroke
192 320 moveto   0 128 rlineto stroke
320 384 moveto 128   0 rlineto stroke
320 320 moveto   0 128 rlineto stroke
448 320 moveto   0 128 rlineto stroke
 64 128 moveto 128   0 rlineto stroke
 64  64 moveto   0 128 rlineto stroke
192  64 moveto   0 128 rlineto stroke
320 128 moveto 128   0 rlineto stroke
320  64 moveto   0 128 rlineto stroke
448  64 moveto   0 128 rlineto stroke
showpage
        

Your program will be nearly (or completely) debugged when you get to this point. Check that it works with n set to 6.

Part 2.   In this part you will create your own recursive design. First, do something different for your basic graphic design. For example, you can have the regions represented by the recursive calls be rectangles of varying size, or you can have them overlap. Or, work with X's or S's instead of H's. Or, work with filled shapes like boxes (to fill a box, trace its boundary, then use fill instead of stroke. Or, use five recursive calls and pentagons, or whatever. Try using color or introducing some randomness. Originality and creativity in the design will be a factor in your grade.

Submission.   Submit the files htree.c, art.c, and input.txt, along with a readme file. The file input.txt should contain the input data with which you want us to run your art.c program.

Extra Credit.   Write a program to make the recursive H pattern using a bottom-up method like Program 5.9. Or, write a recursive PostScript program to produce your pattern.

 


Copyright © 2000 Robert Sedgewick