CS010 Introduction to Computer Science I
Term Project
Deliverable One


Swarm and Flocking Behavior:
Basic Infrastructure
(last updated 3/25/2006)

For the first deliverable, you will implement a variation of the Moving-balls example and exercise from Section 25.1.  The main goal at this stage is to create the infrastructure that you will use to accomplish the larger scope described in the project overview.  For now, that means you need to be able to create, update, and animate a swarm of critters.  As a graphical application, you will be using many of the techniques you have encountered in earlier exercises.  Make sure you create and then refer to global constants for things such as the width and height of your canvas so that they can be changed easily later.

We will use a list of critters as our representation of a swarm; you should include this in your file as an appropriate data definition.  Then you will write functions to display-critter, clear-critter, update-critter based on speed and heading.  The first two are self-explanatory.  The update-critter function should consume a critter and return a new critter with the location updated but with the same velocity (dx and dy).  Finally, write swarm processing functions such as draw-swarmclear-swarm, update-swarm, and draw-and-clear-swarm.  You should be able to animate a swarm by writing an animate-swarm function; it should consume a swarm and a natural number, and then draw, clear, update the swarm as many times as specified by the given number.  You will probably want at least one utility, for example, a function to create a semi-random swarm.  However, you may encounter other needs and you should create helper functions as appropriate.  Organize your functions in your file in an orderly fashion.

For now, the data definition of a critter is basically the same as a ball introduced in Figure 66.  A critter is a structure: (make-critter n1 n2 n3 n4), where n1 through n4 are numbers representing the x and y locations and the change in x and y for each timestep, respectively.  Use the structure definition: (define-struct critter (x y dx dy)).  To display a single critter, draw a dot at its location and a line about 5 pixels long in the direction of its heading.  (The direction it is headed can be determined based on the dx and dy values.  Then draw a line between the critter's current location and a location 10 pixels (units) along the path in the direction it is traveling.)

You should use the following standard for drawing and orientation.  When talking about the standard compass headings, N, S, E & W, the directions should correspond to up, down, right and left respectively.  For your reference, when using trigonometric functions, radians start at 0 heading East, and increases positively clockwise (negatively counter-clockwise).

There is one twist to your update-critter function.  When a critter moves off the canvas in any direction, it should re-appear on the opposite side.  For example, if my canvas is 100 x 100, and I have a critter at location (97,50) moving west five units per time-step, it should re-appear on the left side of the canvas at location (2,50).  You will find the DrScheme function, modulo, helpful in this.

To grade this deliverable, I will review your code (including contracts, purpose statements, indentation, etc.), and then call your animate-swarm function using a swarm of my own.   Therefore, it is important that you follow the data definitions carefully.