CS010 Introduction to Computer Science I
Term Project
Deliverable One


Swarm and Flocking Behavior:
Basic Infrastructure
(last updated 11/6/2007)

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 will consist of two posns representing the critter's position and velocity.  A critter is a structure: (make-critter p1 p2), where p1 and p2 are posns representing the location and the velocity, respectively.  Use the structure definition: (define-struct critter (loc vel)).  To display a single critter, draw a dot at its location and a line about 10 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.)

As discussed class, for the purposes of trigonometric functions, radians start at 0 heading East, and increases positively clockwise (negatively counter-clockwise).

There is one twist to your update-critterfunction.  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 East (or to the right) 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.  Similarly, a critter at location (25, 4) moving North ten units per time-step, it should re-appear at the bottom of the canvas at location (25,94).

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.