Swarm Project: Part 1

For the first deliverable, you will implement a variation of the moving bots exercises we have done in class. The main goal at this stage is to create the infrastructure that you will use to accomplish the swarming that is the larger scope of our project as 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 data definition: a swarm is a [List-of bug]. You will first write functions that process bugs; then, using these and the abstract functions presented in class (and Chapter 18, Figure 62, of HtDP/2e) 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. You may want to implement additional utilities but at a minimum you must write the functions (specified below) 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, we will be using posns to represent vectors in 2-space. I expect at least functions: posn+ that adds two posns returning a new posn; posn- that substracts the second posn from the first, returning a new posn; posn-mag that returns the magnitude of a given posn; posn-scale that consumes a posn and a number n, returning a posn with magnitude that is n times that of the given posn.
  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 magnitudes of the velocities should be in a range -v to v, centered at zero, where v is relatively small (say less than 10). However, you should ensure that none of your critters actual have a zero vector (i.e., [0,0]).
  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 refer to the documentation associated with the 2htdp/image teachpack to determine the appropriate primitives for this operation.
  5. update-bug: bug -> bug. Given the representation of a bug and a swarm, 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 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 update-swarm and draw-swarm functions, together with a swarm of my choosing. (However, I will also test it with 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.