FSEM 131 Fall 2016
Lab 5 |
Many Loops
|
Due Tuesday November, 12th, at 10:00 P.M.
Using the Processing language/environment answer the following three questions.
1. [4 pts] Consider the following program that uses a while
loop. Before running the program
- trace the loop execution,
- draw on a paper the coordinates that define each roof drawn and
- determine how many roofs are drawn.
size(600,200); background(240, 220, 120); fill(220, 30, 30); noStroke(); int roofHeight = 50; int roofBaseY = 80; int startX = 40; int houseX = startX; int houseSize = 100; int houseDistance = houseSize + startX; while(houseX+houseSize < width) { triangle(houseX, roofBaseY, houseX+houseSize/2, roofBaseY-roofHeight, houseX+houseSize, roofBaseY); houseX = houseX + houseDistance; } // reset the value of houseX // houseX = boolean boring = false; if (boring) { fill(130, 130, 220); } else fill(random(128, 255), random(128, 255), random(128, 255)); rect(houseX, roofBaseY, houseSize, houseSize);
Now paste the code in a file roofRow.pde
and do the following.
- Run the program and check if the loop output corresponds to your prediction.
- The resulting image does not contain the rectangle drawn by the last line of code.
Uncomment and complete the line that is used to assign a value tohouseX
. The goal is to make visible the rectangle for the front of the first house. - Change the boolean value of the variable
boring
to execute a different branch of theif
structure. Run the program many times: does the output make sense? - Add a
while
loop around the last line of the program to produce an image similar to the following one. - Change the code to use only one
while
loop since the two sequential loops can be collapsed together. Submit this code version.
2. [4 pts] Write a program palette
that creates an output similar to this image.
Hint: The colors are assigned randomely. Another run of the program will produce different colors.
How can you assure that no random color is too close to the background white?
3. [10 pts] The goal is to write a program that draw many times an object made of multiple shapes.
- Draw your object on paper first with relative coordinates to encode the sizings of the simple shapes.
- Write the body of the loop (not having the declaration of the loop): your object is drawn once. Make sure it works.
- Use loop declarations to make a pattern from your object.