Project 6: Connect the Dots
You will be creating a program that draws pictures according to sequences of
coordinates read from an input file.
What you need to do
Write a program that does the following:
- Ask the user for a file to read coordinates from.
- Let the user type in the filename as a string.
- Open a 400 by
400 canvas and then draw the picture the file specifies (it is guaranteed that all the
pictures will fit in a 400 by 400 canvas).
- Wait for a click on the canvas so the user
can see the final picture, then close the canvas and end the program.
Your program will not receive full credit if there
are any error messages printed in your output, even if the picture is drawn correctly.
You should follow the guidelines for commenting your code
set forth in project 4.
The picture file format
The picture files your program will process are regular text files.
Each file is organized into "shapes," where each shape is a list of (x, y)
coordinates where line segments should be drawn between consecutive pairs
of coordinates. Each shape ends with the coordinates (-1, -1), which is not
part of the shape and is only used in the file to signal the end of the shape.
For instance, say you have the file "shapes.txt" that looks like this:
50 50
50 100
100 50
50 50
-1 -1
100 100
150 150
150 100
-1 -1
Notice that there are two lines with "-1 -1" which means there are two shapes in the
file. The first shape is bounded by the coordinates (50, 50), (50, 100), (100, 50), and (50, 50),
in other words, lines 1-4 in the file.
The second shape is bounded by the coordinates (100, 100), (150, 150), and (150, 100),
or lines 6, 7, and 8 in the file.
These shapes would be drawn as shown below. Notice how whenever -1 -1 is reached,
the current shape is ended (not necessarily at the same point it began)
and a new shape may begin. Every shape ends with -1 -1.
How to draw the shapes
You do not need to draw an entire shape at once. By reading the file line-by-line and
using the sliding window technique,
you should be able to draw lines between the "previous point" and the "current point" by
using draw_line from the simplegraphics library.
Sample interaction
What the program prints is in bold, what the user types is in italics, and brackets describe what
the program is doing.
Run 1:
What file do you want to read? shapes.txt
[ program opens window and draws picture ]
Click the canvas to close the window and end the program.
[ user clicks the canvas and the program ends ]
Run 2:
What file do you want to read? homer.txt
[ program opens window and draws picture ]
Click the canvas to close the window and end the program.
[ user clicks the canvas and the program ends ]
You should test your program with
shapes.txt (corresponding picture),
two-triangles.txt (corresponding picture),
stars.txt (corresponding picture),
panda.txt (corresponding picture), and
homer.txt (corresponding picture).
The picture files are also available in my public folder.
Hints
- Don't try to write the entire program all at once. Break it down into pieces that
build on each other. For instance, first write a program that just reads in a file
of coordinates and prints (to the screen) the (x, y) coordinates as they are read.
- Don't forget to convert the x- and y-coordinates to integers (separately).
- Even though the -1 -1 lines seem like sentinels (and in truth they are), don't end
the file-reading loop when -1 -1 is reached (like when we wrote programs to read in
numbers until you typed -1). The reason for this is that -1 -1 is only
a sentinel for ending a shape, not a sentinel for ending the entire file.
Challenges
- Let the user enter two numbers (a, b) that the drawn image will be shifted by.
The image will be shifted "a" pixels in the x direction, and "b" pixels in the y direction.
- Let the user enter a scaling factor for the drawing. For instance, if
the user enters a scaling factor of 2, the picture should be drawn twice as big.
If they enter 0.5, it should be drawn 50% smaller.
- Let the user enter a number of degrees the image should be rotated. For instance,
if the user enters 15, the image should be rotated 15 degrees clockwise.
- Open the file and read all the information to find the geometric "boundaries" of the image
(the largest and smallest x- and y-coordinates). Then re-open the file, read all the
information again, and display the image scaled so it fills up the entire 400 by 400 canvas
window.
- Read the material in section 7.1 on writing to files (pgs 244-246). Use this knowledge
to create a program where the user can create a connect-the-dots drawing on
the screen by clicking points, and the program will save the drawing to a file similar
to the ones your regular program reads.
What to turn in
Through Moodle, turn in your code as a file called dots_yourLastName_yourFirstName.py.