CPSC 120 Principles of Computer Science Fall 2024

Lab 8
Repetition

Due: Fri 11/1 at the start of class

Labs are due at the start of class. It is OK if you show up and copy your files to the handin directory at the very beginning of class, but this should take at most a couple of minutes and you should not spend the next lab period finishing up the previous week's lab. I will check timestamps, and files handed in more than five minutes after the start of lab will be considered late.

See the policy on late work and extensions.


Introduction

Loops provide a powerful shortcut for repetition — instead of cutting and pasting (with lots of small, tedious, error-prone edits) to get multiple copies of something, a loop allows you to repeat things as often as you want with only a few extra lines of code.

Quilts once again provide a good source of inspiration, since quilt block patterns often utilize repetition in their designs and the quilt itself is often constructed from multiple copies of individual blocks. This lab features three traditional quilt blocks.


Successfully completing this lab means that you are able to:

Academic Integrity and Collaboration

The short version:

Review the discussion in previous lab handouts and the full collaboration policy for more details.

Handin

To hand in your work:


Preliminaries

Getting Started

Review the repeat-as-long-as and repeat n times (counting loop) patterns for loops — the slides and the in-class exercises handouts contain the loop questions to ask yourself to identify the elements, the template for the code to use a loop, and examples for how the answers to the loop questions slot into the code templates. It is also useful to review the posted examples to see how a loop fits into a whole sketch, and the posted solutions for the in-class exercises for additional examples and to practice identifying the elements of the loop pattern for yourself.

Also read through all of each exercise before starting it — there's important information about how to do the task after the initial statement.

"Any Size Window"

The exercises ask you to design your sketches so that they work with any size window. Remember that this means to use the system variables width and height instead of specific numbers when referring to the drawing window dimensions.


Exercises

Put your name and a description of the sketch in comments at the beginning of each sketch. Also don't forget to Auto Format your code before handing it in. (Refer back to lab 2 for more on Auto Format if needed.)

You must use loops wherever it is appropriate to do so! (Reminder: a loop when something repeats more than a few times and there is predictable change from one copy to the next.) You will receive very little credit for producing the right drawing without using loops.

Exercise 1

Create a sketch named lab8a which displays a strip of right-pointing flying geese, as shown. Each "goose" is a triangle whose long dimension is twice the length of the short dimension; for a horizontally-oriented pattern, this means the height of the triangle is twice the width.

Make the drawing window much wider than it is tall (as shown in the illustration). Each triangle should span the full height of the drawing window, and the pattern should repeat as many times as can fit fully in the window — leave blank space on the right side if there isn't room for a whole triangle. You don't need to have exactly the same number of triangles as shown in the illustration. The triangles can be any color.

Design your sketch so that it works with any size window.

To create this sketch:


Exercise 2

Create a sketch named lab8b which displays a single rail fence block, as shown. There should be four stripes within each quadrant, with a smooth change in the color of the stripes from left to right and top to bottom.

Make the drawing window square. The block should fill the entire drawing window, and the stripes should be sized so that four stripes exactly fill each quadrant. It is OK to vary just one color component, such as going from red to black, or you can do something more complex than involves varying two or more color components, like the blue-to-green color scheme shown in the example.

Design your sketch so that it works with any size window.

To create this sketch:


Exercise 3

Create a sketch named lab8c which displays a single log cabin block, as shown. Pay careful attention to the example so you get details of the pattern correct!

Make the drawing window square. The block should fill the entire drawing window, and each rectangle should be 20 pixels wide (vertical rectangles) or high (horizontal rectangles). While the quilt block is built from the inside out, yours will be built outside in — include as many rectangles as fit, leaving a blank space in the middle if there isn't enough room to have another complete set of rectangles. Use four different colors as shown, but you can pick any colors you want.

Design your sketch so that it works with any size window.

To create this sketch:

Hint: the keep-going condition for the loop(s) can be a little tricky to get right. Use the strategy recommended earlier in the semester — draw a picture and label what you know so that you can figure out expressions for the values you need but don't have directly. Going from the outside in, you only want to draw another rectangle if there's room for another whole set — this means that the rectangle needs to fit entirely within its half of the window.


Exercise 4

Create a sketch named lab8d which uses the blocks you created in #1-3 to make a quilt as shown — there should be a strip of flying geese at the top, then a row of 5 rail fence blocks, a row of 5 log cabin blocks, another row of 5 rail fence blocks, and a final strip of flying geese. (You can choose your own colors, but otherwise your quilt should match the example.) Choose a size for your drawing window so the blocks fit nicely without distortion or extra space on one edge (in the case of the flying geese).

Your sketch should contain a drawing function for each kind of block — a strip of flying geese, a single rail fence block, a single log cabin block. (This means three drawing functions, in addition to the drawing functions used in #2.) Each drawing function should have parameters specifying the position and size of the block, and should draw the pattern filling that region. Don't forget to include comments describing each function and its parameters.

Writing the body of each function should largely be a matter of copying the relevant code from #1-3, with two changes: within each function, the parameters and loop variables must have different names (so you might need to rename some things), and the pattern now needs to be drawn within the region specified by the parameters rather than filling the whole drawing window.

Once you have a drawing function for each kind of block, use the functions to draw the whole quilt.


Extra Credit

You can earn extra credit by going substantially beyond the required elements. Some possibilities:

Include a brief description of what you've done for extra credit in a comment at the beginning of your sketch. More creative and challenging elements will earn more points.