CSCI 150: PreLab 3 Answer Key

Visualizations

In this optional prelab (you do not have to turn this in, it is just an additional resource), we will formulate some of the ideas necessary to complete Lab 03.


Picture This

In this week's lab, you will be using graphical primitives (rectangles, circles, lines, etc.) to create an image of your own choosing.

Q1. For now, sketch a picture that you'll use as a blueprint for your eventual picture program. Some past projects: a hippo birthday party; a cityscape; a unicorn fighting a robot; happy flowers. Your sketch should be on it's own sheet of paper and should indicate color as well as form. You can use markers, pens, pencils, watercolors, etc. You aren't expected to be Michelangelo (or any other TMNT for that matter), but you shouldn't just make a smiley face and call it a day. Unless it's an undeniably awesome smiley face.

Answer: whatever you would like to draw!

Walk Like an Egyptian

Describe the Problem

In lab you'll be creating a program that draws a pyramid of bricks based on user input.

Input: An integer for the width of the image (width) and the height in bricks of the pyramid (n).
Output: An image of a pyramid that is n bricks tall in a square canvas width wide (and thus width tall).

Understand the Problem

Here are three sample outputs for your reference. Notice that the pyramid doesn't necessarily fill the entire canvas to the right and to the top; if the canvas width is not evenly divisible by the number of bricks, then there will be extra blank space. (A question for you to ponder: why is there so much blank space in the third example? Seems like you could fit lots of extra bricks both to the right and up top...)

400 x 400, 3 bricks.
3 bricks

400 x 400, 17 bricks.
17 bricks

400 x 400, 123 bricks.
123 bricks

Suppose you have a canvas that is 7 units wide and 7 units high. It may be useful to imagine that the canvas is made of graph paper and is 7 squares wide by 7 squares tall. Suppose that you want a pyramid that is 3 bricks high (that is, n=3). You'd like the bricks to be as large as possible, but they should all be of the same size, and they must use a whole number of squares.

Q2. What is the side-length of each brick? That is, how many square wide and tall is each brick?

Answer: 2

Q3. How many bricks are:
  • in the 0th (bottom) row? Answer: 3
  • in the 1st (middle) row? Answer: 2
  • in the 2nd (top) row? Answer: 1
Q4. More generally, suppose your canvas is width wide and width high, and that your pyramid is n bricks tall. What is the side-length s of a brick, in terms of width and n? Use Python's integer division if necessary.

Answer: width // n

Q5. If our pyramid is n bricks tall, then it is also n bricks wide at its base. Thus we have that
  • Row 0 contains n bricks
  • Row 1 (just above row 0) has n-1 bricks
  • Row 2 (just above row 1) has n-2 bricks
  • Row n-1 (the top row) has 1 brick.
How many bricks does row i contain, in terms of n and i?

Answer: n - i

Q6. If our pyramid is n bricks tall and each brick has side-length s, then
  • The first brick of row 0 starts at x-coordinate 0.
  • The first brick of row 1 starts at x-coordinate s//2.
  • The first brick of row 2 starts at x-coordinate s.
What is the x-coordinate of the first brick of row i, in terms of s and i?

Answer: (i * s) // 2

Q7. If our pyramid is n bricks tall, our canvas width wide and tall, and each brick has side-length s, then
  • The bricks of row 0 have y-coordinate width-s.
  • The bricks of row 1 have y-coordinate width-2s.
  • The bricks of row 2 have y-coordinate width-3s.
What is the y-coordinate of row i, in terms of width, i, and s?

Answer: width - (i+1) * s

Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.

I affirm that I have adhered to the Honor Code in this assignment.