CSC 111: Intro to Computer Science through Programming

Homework 8

Due: Tuesday, Apr. 11 at 11:59pm on Moodle

This assignment is part of our Liberal Arts Module, which focuses on collaborations between computer science and other fields. Specifically, we will be investigating maps as a way to tell stories and visualize complex datasets. For this assignment, you'll be "digitizing" the Smith College campus map. Digitizing data (map data in particular) is very important for downstream analysis. If we have a way to turn an image into text data, then the computer can read it later on. So in this assignment, you'll write a representation of a map to a file, then use another program to read in the map file and produce a digital map.

For this assignment, you are welcome to discuss your work with other students, but all code you submit should be your own original work. It should be produced and understood by you, without looking at anyone else's assignment.

Quiz 8

Please take the short Week 8 quiz on Moodle (also due by Tuesday night). This is to help me understand what areas need work, and also help you understand what concepts might need extra practice. It is not a large part of the overall homework grade. Just for the quiz part, do not discuss the questions with other students, TAs, etc. (Note: quiz usually posted Wednesday afternoon.)

Digitizing a Map

First, download the map image SmithMap.png or SmithMap.gif (whichever will work on your operating system) and the started code hw8.py and put them in the same folder (make sure this folder also contains graphics.py). Run the starter code and make sure you can see this image:

helper
Examine the starter code and make sure it makes sense. In the code, several toy "buttons" are created which will have various functions. The idea is that the user will first click "Start". Then they will click on all the corners of a particular building. After they are finished with that building, they will click "Stop". Then they can click "Start" again to create a new building and process repeats. Whenever they are done creating buildings, they'll click "Finish".

The following steps will take you through the process of digitizing this map. To start, I would suggest putting all your code in main and get it working there first, then using helper functions later on if desired (not necessary).

Step by Step

  1. First set up an empty list that will contain the points of each building (think about each building as a Polygon). When the user clicks on a building corner, we will add that Point to this "building list".

  2. Now set up a while loop that will run indefinitely (we'll see how to stop it later on, but for now just have it run forever using "while True:"). In each iteration of the while loop, we will wait for the user's mouse click (i.e. using win.getMouse()). We'll assign the click to a variable, and make sure to get the x and y coordinates of this click. After doing this, there are a few different cases we need to consider:

    • Start case

      If the user's click is very close to the "Start" button (determine closeness similar to Homework 7, it doesn't have to be perfect), we'll clear the building list and make it the empty list so we can start a new building. That's actually all we need to do in this case.

    • Stop case

      If the user's click is very close to the "Stop" button, we need to finish the current building. To give the user any idea of what they've clicked on, generate a Polygon out of the current list of building points, and draw that Polygon on the window. This is mostly for user feedback, but it's a very helpful step.

    • Building click case

      If the user has not clicked "Start" or "Stop" (we'll ignore "Finish" for now), then hopefully they clicked on a corner of a building. In this case, add the click point to the list of building points. That's all we need to doing in this case. As the user clicks more points of the building, those will be separate iterations through the while loop.

    Make sure these three cases are working first, and that you can get a picture that looks something like this:

    helper

  3. Now we have just a few steps left. First, the "Finish" case. Outside your while loop, create a boolean variable to keep track of whether or not we are finished creating buildings. Then instead of "while True:", change your while loop to only execute if we haven't finished yet. Then create a finish case to see if the user's click is very close to the "Finish" button. If so, change the boolean, which will stop our while loop.

  4. Now we will write the building coordinates to a file so that they can be used later on. This will require modifying the "Stop" case. Whenever the user clicks "Stop", write the (x,y) coordinates of the point list out to a file. The format of the file should be: one building on each line, and the x and y coordinates separated by spaces, so each line looks like a long list of numbers. For example, here are two points per line, for two different buildings:

    helper

    I would also recommend closing this text file in the "Finish" case.

  5. Finally, run the program map_draw.py to create your map! This program will read in your file of building coordinates and draw a map. Make sure to include at least 5 buildings, and then save this window as digital_map.png. It should look something like this:

    helper

Extensions

Optional variations:

  1. Label each building using an Entry object (described in Chap 4.7.2). When writing your buildings.txt file, have the first token on each line be the building name (i.e. "SeelyeHall"). When you click "Stop", make sure to also enter a building name to that it can be written to the file. Then modify the map_draw.py code to accommodate building names.

  2. Digitize not only the buildings, but other features as well including roads, the pond, etc. Devise a way to have these different features shown in different colors.

If you do a variation, email me a screenshot - I would love to show some examples in class!

Submit

You do not have to submit a transcript for this homework. Instead, take a screenshot of your map after running the program map_draw.py and submit that with your code. Make sure that you've thought about variable names and code structure, and that your code is well-commented. It is okay not to use helper functions for this assignment, but if you do, use a docstring for each one.

TO SUBMIT ON MOODLE:

  • hw8.py

  • digital_map.png