Traffic Project: Part 1

Sample animation for deliverable 1

For the first deliverable, you will implement the building blocks for our term project. The main goal at this stage is to create the infrastructure that will support the larger scope of an urban traffic simulator described in the project overview. For now, that means you need to write code that creates, updates, and animates a single vehicle on a road-segment. As a graphical application, you will be using some of the same concepts you have encountered in earlier exercises using the (HtDP/2e) image and universe teachpacks. Make sure you create and then utilize global constants for things such as the width and height of your world so that they can be changed easily later.

Preliminaries and Definitions

What are the properties of an automobile that we would want to model if we want it to move about? To start with, we might represent the location, direction, and speed. Since we want to display an animation, we could also associate an image with a vehicle. Road segments would natually have a start and an end point (thinking of them as one-way segments). Anticipating that we will have more than one road segment in an urban road network, we might also want to provide a field for vehicles to store the particular road-segment on which it is currently traveling. Although development in subsequent deliverables may cause us to add or subtract features of vehicles and/or roadsegments, these will allow us to get started.

For the first project deliverable, you should use the structure definition: (define-struct vehicle (loc speed road img)) and its corresponding data definition: a vehicle is a structure: (make-vehicle p n rs i), where p is a posn representing the car's location, n is a number representing its speed, rs is a roadsegment on which the car is traveling, and i is the car's image.

The roadsegment structure definiton is as follows: (define-struct roadsegment (start end)), with its associated data-definition: a roadsegment (RS) is a structure: (make-roadsegment p1 p2), where p1 and p2 are both posns. Note that road segments can stretch between two arbitrary points, and the vehicles must travel on them accordingly. Be sure to write your code in a general way to support this requirement.

Basic Infrastructure

First, you will want a collection of utilities. Since we will mainly be using posns to represent both locations and directions for the various operations on vehicles that we will be performing, you should create a number of utility functions that operate on posns. I will prescribe some utilities but you should write additional functions as you have use for them. Minimally, you will want functions that add and subtract two posns, scales a posn by a factor, and finds the magnitude of a posn.

To display a single vehicle, you should either locate and insert an overhead image of a car or create an image with the shape primitives in the image teachpack. The image will need to be rotated to the appropriate orientation of the roadsegment on which the car is traveling.


This is an individual project; working together must be limited to asking specific questions about Racket functionality.

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 may test your program with code of my own so errors at testing time will result in significant penalties even if your code ‘works’ with your own tests.

  1. Templates. Near the top of your file, include a template for functions that process vehicles. Likewise, include a template for functions that process roadsegments.
  2. Posn-as-vector Utilities. These will make your life simpler. As stated above, I expect at least the following functions: posn+ that adds two posns, posn- that substracts a second posn from the first, scale-posn that multiplies the components of a posn by a given scalar factor, and posn-magnitude that determines the magnitude of a posn. In each of these, we use a posn to represent a vector in 2-space with respect to the origin.
  3. place-roadsegment: RS image -> image. Add a graphical representation of a roadsegment to the given image, returning the resulting image. Naturally, the rendition of the roadsegment should reflect its start and end positions.
  4. init-vehicle: RS image number -> vehicle. This function should return a vehicle having the given image and speed, but located at the start of the given roadsegment. You will want to refer to the roadsegment for the direction that the vehicle will be moving.
  5. place-vehicle: vehicle image -> image. Add a given vehicle to the image at the vehicle's current location (and with appropriate orientation based on the current roadsegment).
  6. udate-vehicle: vehicle -> vehicle. Given the representation of a vehicle, create a new vehicle that reflects the movement along the roadsegment at the current speed.
  7. Defined constants. You should define and use constants to control the size of your scene and the width of roadsegments. (The size of your vehicle images should reflect the width of roadsegments so that only one constant need be changed to adjust the visual scale.
  8. Animate with big-bang. You should define a single roadsegment, initialize a vehicle at its start location, and animate its movement along the road until it reaches the end of the segment. For this first deliverable, the world-state for big-bang is simply a single vehicle. The simulation should stop when the car reaches the end.

Tips and Hints

You will need to rotate the vehicle's image according to the orientation of the current road segment. Rotating an image requires an angle in degrees. Using the end-points of a roadsegment you can compute the slope. Trigonometric functions can give you the angle (in radians) of a given line segment's slope.

Posns can represent vectors (arrows representing direction and magnitude) from the origin. The utilities you need to write support adding and subtracting vectors (represented as posns) as well as stretching and shrinking the lengths of vectors (again, represented as posns).


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 signatures, purpose statements, indentation, etc.), and then call big-bang with your your update-vehicle and place-vehicle functions, together with a roadsegment of my choosing.

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.