Python代写:COMP9021 Tangram

Introduction

用Python解一个Tangram,也就是七巧板问题,作业给基本框架,往里面填写逻辑即可。

Introduction

The assignment consists of three parts, from easiest to more difficult. Each part is worth 3 marks.
For each test, the automarking script will let your program run for 30 seconds.
As all output is True or False, each test will actually consist of a number of subtests of similar difficulty, and passing one test will mean passing all associated subtests; failing at least one of the subtests of a test will result in scoring 0 to that test.
Up to one mark will reward good comments, good choice of names for identifiers and functions, readability of code, simplicity of statements, compactness of functions. This will be determined manually.
One extra mark will be awarded for making available to the class a good test case, consisting of two similar sets of pieces and two solved tangrams built from such a set, which can then be used by everyone for testing purposes.
Late assignments will be penalised: the mark for a late submission will be the minimum of the awarded mark and 10 minus the number of full and partial days that have elapsed from the due date.

Background

The game of tangram consists in creating shapes out of pieces. We assume that each piece has its own colour, different to the colour of any other piece in the set we are working with. Just for reference, here is the list the colours that are available to us

Pieces

Here is an example of the contents of the file pieces A.xml, typical of the contents of any file of this kind (so only the number of pieces, the colour names, and the various coordinates can differ from one such file to another - we do not bother with allowing for variations, in the use of space in particular).

1
2
3
4
5
6
7
8
9
<svgversion ="1.1"xmlns ="http://www.w3.org/2000/svg">
<pathd="M 50 50 L 50 90 L 90 90 z"fill="red" />
<pathd="M 160 170 L 160 130 L 120 130 z"fill="green" />
<pathd="M 200 30 L 180 30 L 180 50 L 220 50 z"fill="blue" />
<pathd="M 40 100 L 40 140 L 60 140 L 60 120 z"fill="yellow" />
<pathd="M 210 70 L 230 90 L 270 90 L 270 50 L 230 50 z"fill="purple" />
<pathd="M 180 130 L 180 170 L 220 210 L 240 190 z"fill="olive" />
<pathd="M 100 200 L 120 180 L 80 140 L 80 180 z"fill="magenta" />
</svg>

Note that the coordinates are nonnegative integers. This means that the sets of pieces we consider rule out those of the traditional game of tangram, where 2 is involved everywhere…
We require every piece to be a convex polygon. An .xml file should represent a piece with n sides (n > 3) by an enumeration of n pairs of coordinates, those of consecutive vertices, the first vertex being arbitrary, and the enumeration being either clockwise or anticlockwise.
The pieces can have a different orientation and be flipped over. For instance, the file pieces_AA.xml whose contents is

1
2
3
4
5
6
7
8
9
<svgversion ="1.1"xmlns ="http://www.w3.org/2000/svg">
<pathd="M 50 50 L 50 90 L 90 90 z"fill="red" />
<pathd="M 160 170 L 160 130 L 120 130 z"fill="green" />
<pathd="M 200 30 L 180 30 L 180 50 L 220 50 z"fill="blue" />
<pathd="M 40 100 L 40 140 L 60 140 L 60 120 z"fill="yellow" />
<pathd="M 210 70 L 230 90 L 270 90 L 270 50 L 230 50 z"fill="purple" />
<pathd="M 180 130 L 180 170 L 220 210 L 240 190 z"fill="olive" />
<pathd="M 100 200 L 120 180 L 80 140 L 80 180 z"fill="magenta" />
</svg>

Shapes

A representation of a shape is provided thanks to an .xml file with the same structure, storing the coordinates of the vertices of just one polygon.
The file shape_A_1.xml whose contents is

1
2
3
<svgversion ="1.1"xmlns ="http://www.w3.org/2000/svg">
<pathd="M 30 20 L 110 20 L 110 120 L 30 120 z"fill="grey" />
</svg>

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.

First Task

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.

Second Task

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.

Third Task

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.