Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python: Exercise Write a Polygon class that is a sequence of 2D points represented by named tuples, so that each point is given a name
Python:
Exercise Write a Polygon class that is a sequence of 2D points represented by named tuples, so that each point is given a name e.g., point A' is (4, 5). Thus. Point = nameduple(Point, ['name', 'x', 'y') The class defines the following a. constructors, setter and getter functions 1. A constructor that takes 3 or more points as arguments e.g., Polygon( (A',5,0), (B. 10,5)..5, 10), (D,2,8) ), and initializes the polygon accordingly. 2. A setter function that appends to the polygon a new point from given name and x, y coordinates. It should throw a user defined exception ExistingPointError if the point exists. Check the name of the point as well as x and y coordinates 3. A getter function that allows retrieving a point given its name. The function should throw a user defined exception PointNotFoundError if such point does not exist. 4. A getter function that takes index as an argument and returns the point at the given index. Throw IndexOutofBound exception. b. Update, delete and calculate functions 1. A function that allows updating the x and y coordinates of an existing point specified by its name. Throws PointNotFoundError 2. A function that deletes a point by name. Throws PointNotFoundError 3. A function that calculates and returns the perimeter of the polygon. c. Length, print and comparison functions 1. A function that returns the number of points in the polygon, so that len(poly) works 2. A function that implements comparison operator. E.g. polygonlpolygon2 3. A function that allows using print(poly) to print a polygon's points, in the following format: A: (5,6) ->B: (6,7) >C: (12,15) d. A function that draws a polygon on the screen, using Turtle graphics. Below is an example that draws a single line; adapt as necessary. For more info about Turtle graphics, see //docs turtle.html on import turtle def draw_line(pl, p2, speed-2, color-blue) turtle.speed(speed) turtle.hideturtle0 turtle.penupO turtle.goto(p1) turtle.pendownO turtle.color(color) turtle.write(A') turtle.goto(p2) turtle.write(B) turtle.exitonclick0 draw_line( (0,50), (300,150)) Exercise Write a Polygon class that is a sequence of 2D points represented by named tuples, so that each point is given a name e.g., point A' is (4, 5). Thus. Point = nameduple(Point, ['name', 'x', 'y') The class defines the following a. constructors, setter and getter functions 1. A constructor that takes 3 or more points as arguments e.g., Polygon( (A',5,0), (B. 10,5)..5, 10), (D,2,8) ), and initializes the polygon accordingly. 2. A setter function that appends to the polygon a new point from given name and x, y coordinates. It should throw a user defined exception ExistingPointError if the point exists. Check the name of the point as well as x and y coordinates 3. A getter function that allows retrieving a point given its name. The function should throw a user defined exception PointNotFoundError if such point does not exist. 4. A getter function that takes index as an argument and returns the point at the given index. Throw IndexOutofBound exception. b. Update, delete and calculate functions 1. A function that allows updating the x and y coordinates of an existing point specified by its name. Throws PointNotFoundError 2. A function that deletes a point by name. Throws PointNotFoundError 3. A function that calculates and returns the perimeter of the polygon. c. Length, print and comparison functions 1. A function that returns the number of points in the polygon, so that len(poly) works 2. A function that implements comparison operator. E.g. polygonlpolygon2 3. A function that allows using print(poly) to print a polygon's points, in the following format: A: (5,6) ->B: (6,7) >C: (12,15) d. A function that draws a polygon on the screen, using Turtle graphics. Below is an example that draws a single line; adapt as necessary. For more info about Turtle graphics, see //docs turtle.html on import turtle def draw_line(pl, p2, speed-2, color-blue) turtle.speed(speed) turtle.hideturtle0 turtle.penupO turtle.goto(p1) turtle.pendownO turtle.color(color) turtle.write(A') turtle.goto(p2) turtle.write(B) turtle.exitonclick0 draw_line( (0,50), (300,150))Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started