CS2023 Assignment 3

Winter 2004

Due Friday, February 6, 10:30 AM, in the assignment bin on E-Level

Background

The scene is an urban park; a cat watches a mouse run around the base of a statue of Dennis Ritchie. Over the course of a minute, the somewhat witless mouse moves one meter counterclockwise around the statue's base, which is circular and two meters in diameter. Every sixty seconds, the cat pursues the mouse as follows: If the cat can see the mouse, the cat moves one meter toward the statue. If the cat can't see the mouse, the cat circles 1.25 meters counterclockwise around the statue. The cat plans eventually to get close enough to the mouse to make a juicy lunch of it. If the cat can't catch the mouse after 30 minutes (a very patient cat!) then it gives up.


cat and mouse diagram

Problem

Write a C program to simulate this situation and determine if the cat catches the mouse. The program should ask the user for the initial positions of the mouse (angle) and the cat (both radius and angle). It should then simulate the chase using the rules listed above to determine how the cat and mouse move, with the cat moving first. The program should print the position of the cat and mouse after each move, followed by the results of the chase (either the mouse becomes lunch, or the cat gives up after 30 minutes).

 

Problem representation

Because both the cat and mouse are running in circles around the statue, the best way to represent the positions of both animals is with polar coordinates. Since the radius of the mouse is always 1, you only need to keep track of the angle of the mouse. While you need to keep track of both the angle and the radius of the cat, it is still easier to work with polar coordinates because the cat only moves in only one direction (either angle or radius) per minute.

cartesian and polar coordinates 
Useful Facts

In this simulation, the mouse will be considered caught if: 1) the cat is at the same radius as the mouse, and 2) the cat passes the mouse while running along the base of the status. Condition 1 is easy; the radius of the cat has to be 1. It turns out that condition 2 is true exactly when the following conditions hold:
either
  1. Mouse Angle = Cat Angle
or
  1. cos (Mouse Angle - Old Cat Angle) > cos (Cat Angle - Old Cat Angle) , and
cos (Cat Angle - Mouse Angle) > cos (Cat Angle - Old Cat Angle).
A similar problem is determining whether or not the cat can see the mouse. It turns out that the proper expression for that is
(cat radius) * cos (cat angle - mouse angle) is at least 1.0

The movement of the cat when it doesn't see the mouse can be calculated from the relationship between arc length and radius
arc length and radius

The need to deal with radians in fact occurs everywhere in the program because the trigonometric functions built into C expect angles in radians, not degrees. You may write functions to convert from degrees to radians and back again so that they can enter values in a more familiar notation. Should you choose to do that, it is recommended that you keep all your variables in radians and change values only when doing input/output. That greatly decreases the risk of bugs resulting from angles in the wrong format. In any case you should write a function that reduces angles when they become greater than 2*pi. This function is needed because trig functions become less accurate for angles that are out of the range 0 to 2*pi.

Requirements

The program must read in the positions of the cat and mouse and check to make sure that the entered radius of the cat is greater than 1, otherwise you could place the cat inside the statue! The program must include the following functions:
You may add other functions if you wish. Since we haven't covered pointers yet, you don't have to return values from functions by reference through the parameters, but can use the return value instead.

After reading in the positions, checking them, and converting the angles to radians (if necessary), your program should enter the simulation loop. Each minute, you should do the following things: check if the cat has caught the mouse (before the mouse has moved; remember that cat moves first, then the mouse); if not, move both animals based on the rules given above (and encoded in your movement functions); finally, print out the status of the simulation. The information printed should include the current time, the current angle of the mouse (in degrees), the current angle of the cat (in degrees), and the current radius of the cat. This output should be in a columnar table format.

Input Format

Your program must accept input of the parameters in the following order, and separated by spaces: cat radius, cat angle, mouse angle. For example, for a cat radius = 8.1 m, cat angle = 0.0 degrees, mouse angle = 240.0 degrees, the input would be specified as follows:
8.1 0.0 240.0

Marking Scheme

2 marks will be deducted if your program does not compile (using gcc on a Linux/Intel machine) and 2 marks will be deducted if execution of your program does not terminate. Make sure you follow the C Language Coding Standard, format your code clearly, and choose meaningful variable names: these are all worth 8 marks. Also use preprocessor macros to represent constants (2 marks). The remaining marks will be assigned for program correctness, handling of incorrect input, and implementation of the functions as required above.

Deliverables

A hardcopy of your assignment in the bin on E-level, which must include source code of your program and electronic submission of the program, which must be called cat_and_mouse.c  (lower case only!) to the "Submit your assignment" link on the CS2023 home page.
 
 
 
Eric Aubanel, Jan. 30 2004