Swarm Project: Part 1

For the first deliverable, you will implement a variation of the Moving-balls example and exercise from Section 25.1 of HtDP. The main goal at this stage is to create the infrastructure that you will use to accomplish the swarming of the larger scope described in the project overview. For now, that means you need to write code that creates, updates, and animates a population of critters, which we will call bugs. As a graphical application, you will be using some of the same concepts you have encountered in earlier exercises but you will be learning new techniques for displaying and animating images. Make sure you create and then refer to global constants for things such as the width and height of your world so that they can be changed easily later.

Preliminaries and Definitions

For now, the data definition of a bug will consist of two posns representing the bug's position and velocity. Thus, use the structure definition: (define-struct bug (loc vel)) and its corresponding data definition: a bug is a structure: (make-bug p1 p2), where p1 and p2 are posns representing the bug's location and velocity, respectively.

Although we will not implement any actual swarming for this first deliverable, we will use the definition: a swarm is a (listof bug). You will first write functions that process bugs; then, using these and the abstract functions presented in class (and Section 21.2, Figure 57, of HtDP) you will write functions that process swarms.

Basic Infrastructure

First, you will want a collection of utilities. Since we will mainly be using posns to represent vectors for the various operations on bugs that we will be doing, you should create a number of utility functions that operate on posns. These are somewhat up to you, but at a minimum you will want functions that add and subtract two posns, scales a posn by a factor, finds the magnitude of a posn, and so forth.

For the term project and any subsequent homework exercises involving graphics, we will be using image.ss and universe.ss under the HtDP/2e Teachpacks of DrRacket. We will go over the basics of this in class and in the help sessions. You should also avail yourself of the documentation for both teachpacks.

To display a single bug, draw a solid circle at its location and a line that is ten pixels long in the direction of its heading. (The direction it is headed can be determined based on the dx and dy values of the velocity.)


As always, follow the design recipe and make sure your code conforms to the specifications. Function names and argument order must be followed precisely. I will test your program with code of my own so errors at testing time will result in significant penalties even if your code ostensibly ‘works’. For full credit, you should use abstract functions wherever appropriate.

  1. Templates. Near the top of your file, include a template for functions that process bugs. Likewise, include a template for swarm processing functions. (Include the list-processing template for the swarm even though I want you to use the abstract functions.)
  2. Utilities. These will make your life simpler. As stated above, I expect at least functions that add two posns, substract two posns, scales a posn by a factor, and determines the magnitude of a posn (where in each case, we let a posn represent a vector in 2-space).
  3. make-swarm: N -> swarm. Create a ‘swarm’ consisting of a given number of bugs having random locations and velocities. Obviously, the locations should be within the world defined by your width and height global variables. The velocities should be in a range -v to v, centered at zero, where v is relatively small (say less than 10).
  4. draw-bug: bug image -> image. Given a bug and an image of the world, add the rendered image of the given bug onto the world scene at the appropriate location. The bug's image should consist of a circle with line 10 pixels long pointing in the direction the bug is moving. You should use the documentation associated with the image.ss teachpack to determine the appropriate primites for this operation.
  5. udate-bug: bug -> bug. Given the representation of a bug, create a new bug that reflects the movement of the given bug according to the bug's current velocity.
  6. draw-swarm: swarm -> image. Draw each of the bugs in the given swarm on an initially empty scene.
  7. update-swarm: swarm -> swarm. Given a swarm, update each bug in the swarm using update-bug.

There is one twist to your update-bug function. When a bug moves off the world in any direction, it should re-appear on the opposite side. For example, if my canvas is 100 x 100, and I have a bug at location (97,50) moving due 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 Racket function, modulo, helpful in this. Similarly, a bug at location (25, 4) moving North ten units per time-step, should re-appear at the bottom of the canvas at location (25,94).

Animating a Swarm

Once you have written and tested the functions specified above, you should be able to animate the movement of a swarm with the function, big-bang, from the universe.ss teachpack. This function has many options, but you will want to focus on on-tick and to-draw. For our purposes, we are representing a WorldState as a swarm. That is, the bugs in a swarm hold all the necessary state information for locations and velocities, from which the the swarm may be displayed and updated.


Do not forget to include the standard acknowledgements header at the top of your file. To grade this deliverable, I will review your code (including contracts, purpose statements, indentation, etc.), and then call big-bang with your your update-swarm and draw-swarm functions, together with a swarm of my choosing. (However, I will also test your make-swarm function.)

Submit a single file with your username followed by “P1.rkt” as the name of the file, which should contain your code fulfilling the requirements as well as your acknowledgements.