CS21 HW3: Functions

Due 11:59pm Tuesday, October 2, 2007

You should save your program for this assignment in your cs21/homework/04 directory. A skeleton version of the program will appear in this directory when you run update21 in a terminal window. The program handin21 will only submit files in this directory.

Build a bug

Open the file busyBugs.py and write a function buildBug(center, radius, window) that draws a bug at a specific scale in a specific window at a specific location. Your bug must be drawn using only circles, but how many circles, their color, and their relative position is up to you. An example bug is shown below. The buildBug function has the following parameters:

  1. center: A Point object indicating the location in the window for the center of the bug
  2. radius: The radius of the largest circle in the bug. All other circles in the bug should be scaled in proportion to this radius. Thus a bigger radius draw a bigger bug
  3. window: A GraphWin object in which to draw the bug.
The buildBug function should draw the bug in the specified window and return a list of Circle objects that describe the bug. The first Circle object in the list should be centered at center.

For symmetrical features (such as eyes or ears), remember to use the clone method to make a copy of the original shape. Then draw the cloned shape in the window and move it to the desired location.

Once you have finished writing buildBug, write a main function that tests your buildBug function by creating and drawing a few bugs in a graphics window. You may want to use the setCoords method of the GraphWin object to adjust the coordinates of the window.

Animate a bug with translation

Write a function animateBug(bug, dx, dy, steps, wait) that moves a bug a specified amount for a specified number of steps. The animateBug function has the following parameters:

  1. bug: A list of Circle objects describing a bug
  2. dx: The distance to move in the x-direction in one step
  3. dy: The distance to move in the y-direction in one step
  4. steps: The number of total steps to take
  5. wait: The time in seconds to wait between steps
The animateBug function does not return any values. Once you have written this function, add some code to main to test your new function. Demonstrate that you can move at least two bugs at the same time.

Rotating bugs

Finally write a function rotateBug(bug, degrees) that rotates a bug a specified number of degrees about its center. NOTE: this rotation is not animated. This function should simply rotate all the features of the bug a fixed angle. The parameters for rotateBug are:

  1. bug: A list of Circle objects describing a bug
  2. degrees: The number of degrees to rotate the bug
This function should not return anything.

The bug should be rotated around the center of the first Circle object in the list bug. To rotate a point or circle with a center (x,y) by an angle t around a center point (xc, yc), you must move the center point (x,y) by an amount mx, my defined as folows:

mx = dx*(cos(t)-1)  -  dy*sin(t) 
my = dx*sin(t)      +  dy*(cos(t)-1)
where
dx= x-xc 
dy= y-yc

The python math functions sin and cos expect the angle in radians. To convert from degrees to radians, use rad=deg*pi/180.

Some rotated bugs are shown below.



Putting it all together

Once all of the required functions are working correctly, create an interesting animation with your bugs in the main function. Feel free to write additional functions as needed.

Submit

Once you are satisfied with your programs, hand them in by typing handin21 in a terminal window.