comp9021 olygons Python
Assignment 2 , Trimester 3, 2024
1. General matter
1.1. Aims. The purpose of the assignment is to:
· design and implement an interface based on the desired behaviour of an application program;
· practice the use of Python syntax;
· develop problem solving skills.
1.2. Submission. Your program will be stored in a file n amed p olygons.py. A fter y ou h ave d eveloped andtested your program, upload it using Ed (unless you worked directly in Ed). Assignments can be submittedmore than once; the last version is marked. Your assignment is due by November 11, 9:00am.
1.3. Assessment. The assignment is worth 13 marks. It is going to be tested against a number of input files.For each test, the automarking script. will let your program run for 30 seconds.Assignments can be submitted up to 5 days after the deadline. The maximum mark obtainable reduces by5% per full late day, for up to 5 days. Thus if students A and B hand in assignments worth 12 and 11, bothtwo days late (that is, more than 24 hours late and no more than 48 hours late), then the maximum markobtainable is 11.7, so A gets min(11.7, 11) = 11 and B gets min(11.7, 11) = 11. The outputs of your programsshould be exactly as indicated.1.4. Reminder on plagiarism policy. You are permitted, indeed encouraged, to discuss ways to solve theassignment with other people. Such discussions must be in terms of algorithms, not code. But you mustimplement the solution on your own. Submissions are routinely scanned for similarities that occur when studentscopy and modify other people’s work, or work very closely together on a single implementation. Severe penaltiesapply.
1. General presentationYou will design and implement a program that will
· extract and analyse the various characteristics of (simple) polygons, their contours being coded andstored in a file, and
– either display those characteristics: perimeter, area, convexity, number of rotations that keep thepolygon invariant, and depth (the length of the longest chain of enclosing polygons)– or output some Latex code, to be stored in a file, from which a pictorialrepresentation of thepolygons can be produced, coloured in a way which is proportional to their area.
Call encoding any 2-dimensional grid of size between between 2 × 2 and 50 × 50 (both dimensions can bedifferent) all of whose elements are either 0 or 1.Call neighbour of a member m of an encoding any of the at most eight members of the grid whose value is 1and each of both indexes differs from m’s corresponding index by at most 1. Given a particular encoding, weinductively define for all natural numbers d the set of polygons of depth d (for this encoding) as follows. Let anatural number d be given, and suppose that for all d 0 < d, the set of polygons of depth d 0 has been defined.Change in the encoding all 1’s that determine those polygons to 0. Then the set of polygons of depth d isdefined as the set of polygons which can be obtained from that encoding by connecting 1’s with some of theirneighbours in such a way that we obtain a maximal polygon (that is, a polygon which is not included in anyother polygon obtained from that encoding by connecting 1’s with some of their neighbours).12>>> polys.display()The effect of executing polys.display() is to produce a file named polys_4.tex that can be given asargument to pdflatex to produce a file named polys_4.pdf that views as follows.20
1. Detailed description
4.1. Input. The input is expected to consist of ydim lines of xdim 0’s and 1’s, where xdim and ydim are atleast equal to 2 and at most equal to 50, with possibly lines consisting of spaces only that will be ignored andwith possibly spaces anywhere on the lines with digits. If n isthe x th digit of the y th line with digits, with
0 ≤ x < xdim and 0 ≤ y < ydim , then n is to be associated with a point situated x × 0.4 cm to the right andy × 0.4 cm below an origin4.2. Output. Consider executing from the Python prompt the statement from polygons import * followedby the statement polys = Polygons(some_filename). In case some_filename does not exist in the workingdirectory, then Python will raise a FileNotFoundError exception, that does not need to be caught. Assumthat some_filename does exist (in the working directory). If the input is incorrect in that it does not containonly 0’s and 1’a besides spaces, or in that it contains either too few or too manylines of digits, or in thasome line of digits contains too many or too few digits, or in that two of its lines of digits do not contain thesame number of digits, then the effect of executing polys = Polygons(some_filename) should be to generate
a PolygonsError exception that reads
Traceback (most recent call last):
...polygons.PolygonsError: Incorrect input.If the previous conditions hold but it is not possible to use all 1’s in the input and make them the contoursof polygons of depth d, for any natural number d, as defined in the general presentation, then the effect ofexecuting polys = Polygons(some_filename) should be to generate a PolygonsError exception that readsTraceback (most recent call last):
...polygons.PolygonsError: Cannot get polygons代写comp9021 olygons Python as expected.If the input is correct and it is possible to use all 1’s in the input and make them the contours of polygonsof depth d, for any natural number d, as defined in the general presentation, then executing the statementpolys = Polygons(some_filename) followed by polys.analyse() should have the effect of outputting a firstline that readsPolygon N:with N an appropriate integer at least equal to 1 to refer to the N’th polygon listed in the order of polygons
highest point from smallest value of y to largest value of y, and for a given value of y, from smallest valueof x to largest value of x, a second line that reads one ofPerimeter: a + b*sqrt(.32)Perimeter: aPerimeter: b*sqrt(.32)
with a an appropriate strictly positive floating point number with 1 digit after the decimal point and b anappropriate strictly positive integer, a third line that readsArea:with a an appropriate floating point number with 2 digits after the decimal point, a fourth line that reads oneofConvex: yesConvex: nofifth line that readsNb of invariant rotations: N21with N an appropriate integer at least equal to 1, and a sixth line that readsDepth: Nwith N an appropriate positive integer (possibly 0)Pay attention to the expected format, including spaces.If the input is correct and it is possible to use all 1’s in the input and make them the contours of polygons of depth d, for any natural number d, as defined in the general presentation, then executing the statement polys = Polygons(some_filename) followed by .display() should have the effect of produc ing a file named some_filename.tex that can be given as argument to pdflatex to generate a file namesome_filename.pdf. The provided examples will show you what some_filename.tex should contain.
· Polygons are drawn from lowest to highest depth, and for a given depth, the same ordering as previouslydescribed is used.
· The point that determines the polygon index is used as a starting point in drawing the line segmentsthat make up the polygon, in a clockwise manner.
· A polygons’s colour is determined by its area. The largest polygons are yellow. The smallest polygonsare orange. Polygons in-between mix orange and yellow in proportion of their area. For instance, apolygon whose size is 25% the difference of the size between the largest and the smallest polygon willreceive 25% of orange (and 75% of yellow). That proportion is computed as an integer. When the valueis not an integer, it is rounded to the closest integer, with values of the form. z.5 rounded up to z + 1.Pay attention to the expected format, including spaces and blank lines. Lines that start with % are comments.The output of your program redirected to a file will be compared with the expected output saved in a file (of adifferent name of course) using the diff command. For yourprogram to pass the associated test, diff shouldsilently exit, which requires that the contents of both files be absolutely identical, character for character,including spaces and blank lines. Check your program on the provided examples using the associated .tex files,renaming them as they have the names of the files expected to be generated by your program.