### Programming Assignment Checklist: Recursive Graphics

What preparation do I need before beginning this assignment? Read Sections 2.1–2.3 of the textbook.

What is the layout of the initial equilateral triangle? By using the definition in the assignment and the Pythagorean theorem, you can prove that the top vertex lies at (1/2, √3/2). We illustrate the coordinates of the initial iterations below to help you double-check. How do I draw a filled equilateral triangle? Use StdDraw.filledPolygon() with appropriate parameters.

How do I avoid "magic numbers" like √3/2 in my program? A constant that is used only once can simply have an accompanying comment. A constant that is used more than once deserves a descriptive name. A constant that is used in several methods can be declared and initialized as a class constant (a.k.a. static constant). (E.g., √3/2, the ratio for computing the altitude of an equilateral triangle may be useful in several methods in your Sierpinski class.) Inside the class block, but before any of the methods, place a statement to declare and initialize this class constant.

``` private static double ALTITUDE = . . . ;
```
(You didn't think we would give you the whole line of code did you?) NOTE: Do NOT ever declare static variables in your COS126 programs. Your static CONSTANTS must be initialized immediately. They must not be read in from standard input or the command-line.

I get a StackOverflowError message even when N is a very small number like 3. What could be wrong? This means you are running out of space to store the function call stack. Often, this error is caused by an infinite recursive function. Be sure you have correctly defined your base case and your base case values so that your recursive method returns.

May I use a different color from black to fill in the triangles? Yes, you may use any color that contrasts with the white background.

How can I create colors that aren't predefined in standard drawing? It requires using objects that we'll learn about in Chapter 3. In the meantime, you can use this color guide.

How should I go about doing the artistic part of the assignment? This part is meant to be fun, but here are some guidelines in case you're not so artistic. A very good approach is to first choose a self-referential pattern as a target output. Check out the graphics exercises in Section 2.3. Here are some of our favorite student submissions from last semester. See also the Famous Fractals in Fractals Unleashed for some ideas. Here is a list of fractals, by Hausdorff dimension. Some pictures are harder to generate than others (and some require trig); consult a preceptor for advice if you're unsure.

What will cause me to lose points on the artistic part? We consider three things: the structure of the code; the structure of the recursive call tree; and the art itself.

For example, the Quadricross looks very different from the in-class examples, but the code to generate it looks extremely similar to HTree, so it is a bad choice. On the other hand, even though the Sierpinski Curve eventually generates something that looks identical to the Sierpinski Triangle, the code is very different (probably including an "angle" argument in the recursive method) and so it would earn full marks.

Possible ideas include:

• not having the same number of recursive calls per level or having not all recursive branches terminate at the same depth
• not using "level" or using "level" for a secondary purpose
• mutual recursion (recursive functions that call one another)
• interesting overlapping effects based on changes to your code
• interesting shapes using new StdDraw API methods
Contrast this with the examples Htree, Sierpinski, and NestedCircles which have very similar structures to one another.

You will also lose points if your artwork can be created just as easily without recursion (like Factorial for example). If the recursive call tree for your method is a straight line, it probably falls under this category.

May I use .gif, .jpg, or .png in my artistic creation? Yes. If so, be sure to submit them along with your other files. Make it clear in your readme.txt what part of the design is yours and what part is borrowed from the image file.

My function for Art.java takes several parameters, but the assignment says that I can only read in one command-line argument N. What should I do? Choose a few of the best parameter values and do something like the following:

```if      (N == 1) { x = 0.55; y = 0.75; n = 3; }
else if (N == 2) { x = 0.55; y = 0.75; n = 5; }
else if (N == 3) { x = 0.32; y = 0.71; n = 8; }
else if ...
```

 Possible Progress Steps

These are purely suggestions for how you might make progress. You do not have to follow these steps. Note that your final Sierpinski.java program should not be very long (no longer than Htree, not including comments and blank lines).

• Review Htree.java from the textbook and lecture.

• Write a (nonrecursive) function filledTriangle() that takes three real-valued arguments (x, y, and size), and draws a filled equilateral triangle (pointed downward) with bottom vertex at (x, y) and side length size. To debug and test your function, write main() so that it calls filledTriangle() a few times, with different parameters. You will be able to use this function without modification in Sierpinski.java.

• Write a recursive function sierpinski() that takes four (4) arguments (n, x, y, and size) and plots a Sierpinski triangle of order n, whose largest triangle has side length size and bottom vertex (x, y).

• Write a recursive function sierpinski() that takes one argument n, prints the value n, and then calls itself three times with the value n-1. The recursion should stop when n becomes 0. To test this function out, write main() so that it reads one integer N from the command line and calls sierpinski(N). Except for the spacing and line breaks, you should get the following output when you call sierpinski() with N ranging from 0 to 5. Make sure you understand how this function works, and why it prints the numbers in the order it does.

• Modify sierpinski() so that in addition to printing n, it also prints the size of the triangle to be plotted. Your function should now take two arguments: n and size. The initial call from main() should be to sierpinski(N, 0.5) since the largest triangle has side length 0.5. Each successive level of recursion halves the length. Your function should produce the following output.

• Modify sierpinski() so that it takes four (4) arguments (n, x, y, and size) and plots a Sierpinski triangle of order n, whose largest triangle has side length size and bottom vertex (x, y). Start by drawing Sierpinski triangles with pencil and paper. Use the picture in the Q+A above to figure out the geometry of where the smaller Sierpinski triangles should go.

• Remove all print statements before submitting.

• Your Art.java program must take exactly one integer command-line argument N (which will be between 1 and 7).

• Don't call StdDraw.save(), StdDraw.setCanvasSize(), or StdDraw.setXscale() (or Y). These interfere with grading. (However, setting the scale in Art.java is okay.)

• Be sure to include a comment just above each method definition explaining what the method does — this will be required in all future assignments as well. Leaving a comment for main is not required since the header already does basically the same thing.

• Continue to follow the guidelines from previous weeks.

 Challenge for the bored

After you have completed the assignment, consider using our StdDraw3D library instead of StdDraw to create a three-dimensional fractal design! (Note: You will get NO credit for submitting an Art.java that uses StdDraw3D. This is not for extra credit nor for submission.) You can find documentation on the booksite.

To check that you have downloaded StdDraw3D when running the installer, go to the command prompt and type:

```java-introcs StdDraw3D
```

 Enrichment