Overview
Writing the Code: You must add two functions:
void CircleTrack(void); void Spiral(void);(1) Add the function prototypes below the prototype for main.
void CircleTrack(void){ }(3) Add calls to your functions in the main program.
Run your program to make sure it compiles.
(4) Draw the track
In the sample, the center of the track is 150 pixels from the center of the drawing window and the ball has radius 13. You can make a track like the one in the sample by calling:
PaintRect to color the whole drawing window EraseCircle to clear everything inside the outer edge of the track PaintCircle to fill in everything inside the track(5) Place the ball
AwaitClick();Nothing will happen until the mouse button is pressed and released.
(6) Roll the ball around the track.
You need some trigonometry to do this task. In the demo application, the angle theta that the ball has traveled around the circle starts at 0” and increases in 1” increments. The following picture should remind you of the conversion from polar coordinates to x, y coordinates:
The formulas:
x = R*cos(angle), y = R*sin(angle)
where R is the track radius, assume that angle is measured in radians. Our angle is measured in degrees so you must first do the conversion:
angle = theta*2*pi/360; // pi is an already defined constantAlso, remember that the center of the track is at (200, 200) so you must add 200 to x and y. Use PaintCircle to place the ball and EraseCircle to remove it before you draw the next ball.
Use Delay(1) to slow down the ball so it moves at a reasonable speed. Insert the delay after you draw the ball and before you erase it. This makes a ball present on the screen most of the time and fools us into believing itÕs always there.
(7) How long should the loop run?
To let the ball continue moving until the mouse button is pressed, use a while loop:
while (not Button()){ }
(8) First draw the spiral in black.
A good range for theta is: 0 <= theta < 1000. Use the same conversions from polar to x, y coordinates as described above. This time, however, the distance r of the ball from the center of the window changes with theta. The formula:
r = theta/5makes r = 200 when theta = 1000 and thatÕs just the right distance to place the ball at the edge of the window.
(9) Now make the color vary with theta.
Call SetForeColor(red, green, blue) before the PaintCircle call. Let the color values red, green, and blue vary with theta but make sure they stay in the range 0 to 255.
(10) Erase the spiral by using EraseCircle and running the loop from theta = 1000 to theta = 0.
(11) Finally, add the little ball that runs around the spiral. Put this code before the code that erases the spiral.
(12) In the solution program, a click on the mouse will end the spiral. This is accomplished by adding the call
if (Button()) return;to each of the three loops. If the button is pressed, the control returns to the main program, after the call to Spiral().
Due Date: Turn in at your Lab class, Tuesday or Wednesday, October 7 or 8.
Turn in a print-out of your code and a diskette with your project (.µ), your source code (.cp), and a working application program.
Last Updated: September 28, 1997 12:46 am by