B CUSP 110C – Digital Thinking Exercise 7 +
Homework 10: Loops and Conditionals |
|
Goal: To get practice working with loops and conditions.
Recall our owl function from Exercise 6 (Exercise6.Owl.txt):
Notice once again that the owl is a 70 x 100 figure.
Part 0: setup. Modify the given Processing code such that:
1. frameRate is at 30;
2. size is 600x600
3. clear background per each draw().
Now, recall we wrote an owl8() function to draw 8 owls in a row:
Part 1: Make A Flock with for loop. Re-implement the owl8() function by use a for loop. You do not need to worry about changing color or the owl’s claws, just write a for loop to draw the 8 owls in a row.
Part 2: Make a variable number of owls in a row. Make a function with following specification:
Name: drawOwlsInARow(int y, int num)
Parameters:
int
y, // the beginning y location of the row of owls
int num
// the number of owls to be drawn
Returned type: void
This function draws num number of consecutive owls horizontally starting from x=35, and at the specify y position. For example:
drawOwlsInARow(100, 4); // draws 4 owls in a row beginning at y=100 with first at (35, 100)
drawOwlsInARow(200,
5); // draws 6 5 owls in a row beginning at y=200 with first at
(35, 200)
will draw:
Part 3: Hide and seek with owls in a row:
recall that mouseX and mouseY tell us the location of your mouse position.
Now, use these two variables to test (hint: use if statement) the
position of the mouse and draw a row of 8 owls if the mouse Y position is less
than 200, or:
if (mouseY< 200)
drawOwlsInARow(200, 8);
Now, draw a row of 6 owls at y=400 if mouseY position is greater than 400.
Part 4: drawOwlsInAColumn: Make a function with following specification:
Name: drawOwlsInAColumn(int x, int num)
Parameters:
int x, // the beginning x location of the
column of owls
int num
// the number of owls to be drawn
Returned type: void
This function draws num number of consecutive owls vertically starting from y=100, and at the specify y position. For example:
drawOwlsInAColumn(60, 4); // draws 4 owls in a column beginning at x=60 with first at (60, 100)
drawOwlsInAColumn(145, 6); // draws 6 owls in a row beginning at x=145 with first at (145, 100)
will draw:
Part 5: Hide and seek with owls in a column: similar to Part 3, we want to draw columns of owls when:
· Mouse X position is less than 200, draw a column of 4 owls at x=200
·
Mouse X position is more than 400, draw a column
of 6 owls at x=600 x=400
Now, when you put everything together (and put a red square in the middle), this is what you have: Kelvin’s attempt.
Remember to comment your program!!
Wrap Up: You have practiced working with loops and conditional statements.
· Exercise 7: Post the applet to your Part5 on your personal web-site and send Jack an email.
· Homework 10: e-submit your source code to the class dropbox.
Challenge (5 pt extra credit): Warning, work on extra credit only after you are completely done with the regular assignment. The following program involves very small number of lines of code, but can be rather tricky.
· Remember we can test for if the mouse is within an area by testing for:
o if (mouseX >50 && mouseX<500) ß test for if mouse’s X position is between 50 and 500
· Remember we can find the hundredth digit of a number by integer division of the number by 100, e.g.
o int n = mouseX / 100; //ß n is the hundredth digit of mouseX, e.g., if mouseX is 248, n will be 2, if mouseX is 168, n is 1, etc.
· Now modify your program such that:
o Draw a red rectangle at (50,50) with size 500x500
o When your mouse is inside this area, draw column/row owls with number representing the hundredth mouseX and mouseY digit.
§ the row/column must be drawn at the current mouse position
§ Examples:
· If mouseX=100, mouseY=200, you will draw
o a column of one owl (because mouseX’s hundredth digit is “1”)
o a row of two owls (because mouseY’s hundredth digit is “2”)
· if mouseX=352, mouseY=428, you will draw
o a column of 3 owls (because mouseX’s hundredth digit is “3”)
o a row of 4 owls (because mouseY’s hundredth digit is “4”)
· Here is my solution.