...L 50 130 L 50 90 L 10 90 L 10 50 L 50 50 z" fill="brown"/>
and which displays as
is another such example.
Contrary to pieces, shapes are not assumed to be convex polygons. Still they are assumed to be simple polygons
(the boundary of a simple polygon does not cross itself; in particular, it cannot consist of at least 2 polygons
that are connected by letting two of them just “touch” each other at one of their vertices—e.g., two rectangles
such that the upper right corner of one rectangle is the lower left corner of the other rectangle; that is not
allowed).
Whereas you will have to check that the representation of the pieces in an .xml file satisfies our constraints,
you will not have to do so for the representation of a shape; you can assume that any shape we will be dealing
with satisfies our constraints.
2.3. Tangrams. The first shape can be built from our set of pieces, in many ways. Here is one, given by the
file tangram_A_1_a.xml whose contents is
and which displays as follows.
5
Here is another one, given by the file tangram_A_1_b.xml whose contents is
and which displays as follows.
The second shape can also be built from our set of pieces, in many ways. Here is one, given by the file
tangram_A_2_a.xml whose contents is
and which displays as follows.
6
Here is another one, given by the file tangram_A_2_b.xml whose contents is
and which displays as follows.
7
3. First task (4 marks)
You have to check that the pieces represented in an .xml file satisfy our constraints. So you have to check
that each piece is convex, and if it represents a polygon with n sides (n ≥ 3) then the representation consists
of an enumeration of the n vertices, either clockwise or anticlockwise. Here is the expected behaviour of your
program.
$ python3
...
>>> from tangram import *
>>> file = open('pieces_A.xml')
>>> coloured_pieces = available_coloured_pieces(file)
>>> are_valid(coloured_pieces)
True
>>> file = open('pieces_AA.xml')
>>> coloured_pieces = available_coloured_pieces(file)
>>> are_valid(coloured_pieces)
True
>>> file = open('incorrect_pieces_1.xml')
>>> coloured_pieces = available_coloured_pieces(file)
>>> are_valid(coloured_pieces)
False
>>> file = open('incorrect_pieces_2.xml')
>>> coloured_pieces = available_coloured_pieces(file)
>>> are_valid(coloured_pieces)
False
>>> file = open('incorrect_pieces_3.xml')
>>> coloured_pieces = available_coloured_pieces(file)
>>> are_valid(coloured_pieces)
False
>>> file = open('incorrect_pieces_4.xml')
>>> coloured_pieces = available_coloured_pieces(file)
>>> are_valid(coloured_pieces)
False
Note that the function are_valid() does not print out True or False, but returns True or False.
8
4. Second task (4 marks)
You have to check whether the sets of pieces represented in two .xml files are identical, allowing for pieces to
be flipped over and allowing for different orientations. Here is the expected behaviour of your program.
$ python3
...
>>> from tangram import *
>>> file = open('pieces_A.xml')
>>> coloured_pieces_1 = available_coloured_pieces(file)
>>> file = open('pieces_AA.xml')
>>> coloured_pieces_2 = available_coloured_pieces(file)
>>> are_identical_sets_of_coloured_pieces(coloured_pieces_1, coloured_pieces_2)
True
>>> file = open('shape_A_1.xml')
>>> coloured_pieces_2 = available_coloured_pieces(file)
>>> are_identical_sets_of_coloured_pieces(coloured_pieces_1, coloured_pieces_2)
False
Note that the function are_identical_sets_of_coloured_pieces() does not print out True or False, but
returns True or False.
9
5. Third task (5 marks)
You have to check whether the pieces represented in an .xml file are a solution to a tangram puzzle represented
in another .xml file. Here is the expected behaviour of your program.
$ python3
...
>>> from tangram import *
>>> file = open('shape_A_1.xml')
>>> shape = available_coloured_pieces(file)
>>> file = open('tangram_A_1_a.xml')
>>> tangram = available_coloured_pieces(file)
>>> is_solution(tangram, shape)
True
>>> file = open('tangram_A_2_a.xml')
>>> tangram = available_coloured_pieces(file)
>>> is_solution(tangram, shape)
False
Note that the function is_solution() does not print out True or False, but returns True or False.