CIS 110 Fall 2012 - Introduction to Computer Programming

Java | /r/compsci (reddit) | PennPortal | | directories | van pelt library | CETS | engineering library
computer graphics & game dev (SIGGRAPH @ Penn) | dining philosophers (DP) | science & tech wing (STWING) | women in cs (WICS)
CETS Answers

Programming Assignment Checklist: Recursive Graphics

Frequently Asked Questions

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

I forget how to do geometry. Any hints? Here are the coordinates of the critical endpoints. Click the image for a bigger version.

Sierpinski triangle geometry

How do I draw a filled equilateral triangle? Use StdDraw.filledPolygon() with appropriate parameters.

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 submissions from past semesters. 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 TA for advice if you're unsure.

How do I make sure my artistic creation stays inside the unit box? The easiest way to check this is to draw a unit box. The way StdDraw sets up the window, (0, 0) and (1, 1) are a little inside the lower left and upper right corners, so you will be able to see the box on the screen. Your drawing should stay inside of it.

What will cause me to lose points on the artistic part? We will deduct points if your picture is too similar to Htree, Sierpinski or Brownian. To be "different enough" from those algorithms, you need to change the recursive part of the program. For example, it is not sufficient to simply substitute squares for triangles in Sierpinski. You will also lose points if your artwork can be more easily created without recursion. This is indicated by a tail-recursive function, i.e., one that could be generated by a single recursive call in the function.

Can 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_sierpinski.txt what part of the design is yours and what part is borrowed from the image file.

My function for 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 ...

Can I use the mouse to control input values? Check with your TA.

Testing and Submission

Submission. If you don't follow these instructions, you risk annoying your grader and losing a significant number of points.

  • You are encouraged to use colors by calling StdDraw.setPenColor() in and

  • Don't call in either program.

  • Don't call StdDraw.setCanvasSize() in either program.

  • Your program must take one integer command-line argument N (expect it to be between 1 and 7).

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 program should not be very long (no longer than Htree, not including comments and blank lines).

  • Review from the textbook and lecture.

  • Download and compile HTree. If it doesn't compile, you may need to download to your working directory.

  • Write a (nonrecursive) function solidTriangle() that takes three real-valued inputs (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 solidTriangle() a few times, with different parameters. You will be able to use this function without modification in

  • Write a recursive function sierpinski() that takes 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). Excepting the spacing, 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 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.

    • Make sure to remove any debug and testing code from your program before submitting.