Objectives

User-Defined Methods

Methods allow programs to add new procedural abstractions to the programming language. In this lab exercise, for example, you write a method to draw a composite stick figure.

Exercise 4b.1:

Create a program that renders the (non-animated) stick figure shown to the right. There are a number of approaches to doing this; for the sake of this exercise, use the following algorithm, which includes a method that renders the stick figure:

  • Setup:
    1. Set the output panel size;
    2. Turn on smoothing;
    3. Turn off animation looping.
  • Draw:
    1. Call the method that renders the stick figure.
  • Render figure (renderFigure()):
    1. Set float variables x and y to the center of the output window;
    2. Set diameter (the diameter of the figure’s head) to 24;
    3. Draw the figure’s trunk;
    4. Draw the figure’s arms;
    5. Draw the figure’s two legs;
    6. Draw the figure’s head.

This routine returns nothing (i.e., void) to its calling program.

We suggest that you center the figure’s head at coordinates (x, y) and draw all the rest of figure relative to these coordinates. For example, the trunk is a single vertical line from (x, y + diameter * 0.5) to (x, y + diameter * 1.5). This scaling will help you in the next exercise.

Save this program for use in the next exercise.

One nice feature of the code in the previous exercise is that it groups all the figure-drawing code in one appropriately named method. This improves the readability of the program and encourages reuse of that method.

Exercise 4b.2:

Modify your previous program so that you can draw two stick figures in different locations (as shown on the right). You can do this as follows:

  • Modify renderFigure() method so that it receives its x and y coordinates from its calling program rather than declaring them locally.
  • Modify draw() so that it calls renderFigure() twice, passing different x-y coordinates as arguments to each call (eg, renderFigure(50, 50));

Save this program for use in the next exercise.

Methods can also return values. This is useful when your program must compute a certain value frequently.

Exercise 4b.3:

Modify your previous program so that it animates the two stick figures using Brownian motion (as shown on the right). You can do this as follows:

  • Add global variables x1, y1, x2 and y2 to store the current coordinates of each of your two stick figures;
  • Modify draw() so that it changes the values of the new global variables by some small amount (e.g., 5 pixels);

When you have this code working, let it run for a while. There is a good chance that at least one of the figures will wander “off” the output window. Fix this by constraining each of your coordinate values to be on the visible output window (using constrain() as you did last week), but rather than writing the same complicated constrained, random value computation for each of the four coordinates, implement and use the following method:

  • changeCoordinate():
    1. Receive from the calling program: a coordinate value value, a minimum legal value lowerBound and a maximum legal value upperBound;
    2. Compute and return a new value that is randomly modified from value but constrained by lowerBound and upperBound.

This method can receive either an x coordinate or a y coordinate, and returns a new, legal value based on its arguments. For example, if currentX and currentDiameter refer to the value of the current x coordinate and the figure’s diameter respectively, then changeCoordinate(currentX, currentDiameter/2, WIDTH-currentDiameter/2) will return a new, properly constrained value for currentX.

Save this program so that you can turn it in later.

Methods are valuable for building useful procedural abstractions. We don't need to restrict ourselves to parameterizing just the x-y coordinates of the stick figure.

Exercise 4b.4:

Modify your program to animate a growing (or shrinking) stick figure. The example on the right shows two stationary figures, each growing at different rates. Implement this as follows:

  • Start with your non-animated stick figure implementation from exercise 4b.2;
  • Add global variables to represent the diameters of your two stick figures;
  • Add a third parameter to renderFigure() that specifies the diameter of the stick figure and modify the method to draw a scalable stick figure based on this new parameter.
  • Modify draw() to pass the new diameter values as arguments to renderFigure() and to increment the diameter values on each frame.

Save the final version of your program so that you can turn it in later.

Exercise 4b.5: Extra Credit

Modify your program so that the color of the stick figure is based on how far it is from the other figure. For example, the image to the right shows two animated stick figures that get redder the closer they get to the sun. To do this, you’ll need to use a distance method such as the one we developed in the lecture and the text.

Checking In

Submit all the code and supporting files for the exercises in this lab.