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.
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.
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.
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).
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.