Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python: Exercise 1 Write a Polygon class that is a sequence of 2D points represented by named tuples , so that each point is

Using Python:

Exercise 1

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 = namedtuple('Point', ['name', 'x', 'y'])

The class defines the following:

constructors, setter and getter functions:

A default constructor that creates an empty polygon.

A constructor that takes 3 or more points as arguments e.g., Polygon( ('A',5,0), ('B',10,5), ('C',5,10), ('D',-2,8) ), and initializes the polygon accordingly.

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.

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.

A getter function that takes index as an argument and returns the point at the given index. Throw IndexOutofBound exception.

Update, delete and calculate functions:

A function that allows updating the x and y coordinates of an existing point specified by its name. Throws PointNotFoundError.

A function that deletes a point by name. Throws PointNotFoundError.

A function that calculates and returns the perimeter of the polygon.

Length, print and comparison functions:

A function that returns the number of points in the polygon, so that len(poly) works.

A function that implements comparison operator. E.g. polygon1 == polygon2.

A function that allows using print(poly) to print a polygons points, in the following format: A: (5,6) -> B: (6,7) -> C: (12,15)

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 https://docs.python.org/3/library/turtle.html

import turtle

def draw_line(p1, p2, speed=2, color='blue'):

turtle.speed(speed)

turtle.hideturtle()

turtle.penup()

turtle.goto(p1)

turtle.pendown()

turtle.color(color)

turtle.write('A')

turtle.goto(p2)

turtle.write('B')

turtle.exitonclick()

draw_line( (0,50), (300,150) )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

=+C&B (especially taxation) laws, regulations, and practices?

Answered: 1 week ago

Question

=+designing international assignment C&B packages.

Answered: 1 week ago