-
Notifications
You must be signed in to change notification settings - Fork 11
Assignment 1
Please read the Assignment Instructions wiki page before you begin.
Read the file input.txt and store each line of text from it as a string in a python list.
Implement a function that will parse the lines of the text file in order and print different outputs depending on what they are.
The first line of the input file will be of the form:
canvas <W> <H>
Which should result in the following line of output:
Canvas size is <W> x <H>
Any further lines will correspond to one of the following patterns:
circle <T> <R> <X> <Y>
line <T> <X1> <Y1> <X2> <Y2>
polyline <T> [<X1> <Y1> ... <Xn> <Yn>]
polyfill [<X1> <Y1> ... <Xn> <Yn>]
Giving these output lines respectively:
Circle of thickness <T> and radius <R> at (<X>, <Y>)
Line of thickness <T> from (<X1>, <Y1>) to (<X2>, <Y2>)
Polygon Line of thickness <T> passing through, [(<X1>, <Y1>), ... (<Xn>, <Yn>)]
Polygon Fill for lines passing through, [(<X1>, <Y1>), ... (<Xn>, <Yn>)]
As an example, the following input.txt:
canvas 150 700
polyline 2 10 60 40 600 30 340
line 4 15 99 68 342
Should result in exactly the following output:
Canvas size is 150 x 700
Polygon Line of thickness 2 passing through, (10, 60), (40, 600), (30, 340)
Line of thickness 4 from (15, 99) to (68, 342)
One mark is awarded for each pattern that can be handled correctly, and one more is given for handling arbitrary pattern orders.
Add a line to the end of the list containing the number of pixels in the canvas (width times height). Then write the lines in the list to a new file named output.txt. You will need to convert strings to integers to perform the multiplication.
So the input given above would result in the following output.txt being written:
canvas 150 700
polyline 2 10 60 40 600 30 340
line 4 15 99 68 342
pixels 105000
One mark is given for adding the new line, and one for writing the file.
You will likely want to consult the Python 3 API documentation to find the library functions you will need to implement the above behaviour.
These pages in particular may be helpful:
The ability to read technical documentation and identify relevant information is a critical skill in computer programming.
canvas 950 440
polyfill 10 10 50 90 90 10 10 10
line 2 20 20 80 43
polyline 1 30 30 50 40 80 20
circle 3 10 50 50