Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Problem!!! Instances of this class are simple polygons. Methods for this

ID: 3697310 • Letter: P

Question

Python Problem!!!

Instances of this class are simple polygons. Methods for this class are described below: 1._init_: The constructor takes an arbitrary number of points as parameters, where the points are listed in counter-clockwise order about the boundary. The parameter to the constructor should be of the form *vertices. Store the points in a list. You are not required to check the polygon for simplicity, i.e., that no two segments of the polygon intersect. 2. translate: Translates the simple polygon by (s,t). This is equivalent to translating every vertex of the polygon by (s,t). 3. rotate: Rotates the simple polygon by angle theta. This is equivalent to rotating every vertex of the polygon by theta. 4._iter_: Return an iterator object. (This may be the simple polygon itself, or may be a new iterator object. The choice is yours.) 5._next_: Return the next vertex from the simple polygon. If there are no further vertices, raise the StopIteration exception. 6._len_: This method allows us to use the built-in function len with simple polygon instances (just as str allows us to use the built-in function str). It returns the number of vertices in the polygon. 7._getitem_: Overload the index operator. If the parameters of this method are self and i, it returns the i-th vertex of the convex polygon. If the index is out of range (less than zero or greater than or equal to the number of vertices of the polygon), raise the IndexError exception. 8._str_: Produces a string representation of the polygon. One obvious way to do this is to return a string containing the vertices of the polygon in counter-clockwise order. 9. perimeter: Return the perimeter of the polygon.

Explanation / Answer

1)

Ans 3

import math
def rotatePolygon(polygon,theta):
#Rotates the given polygon which consists of corners represented as (x,y), around the ORIGIN, clock-wise, theta degrees

theta = math.radians(theta)
rotatedPolygon = []
for corner in polygon :
rotatedPolygon.append(( corner[0]*math.cos(theta)-corner[1]*math.sin(theta) , corner[0]*math.sin(theta)+corner[1]*math.cos(theta)) )
return rotatedPolygon


my_polygon = [(0,0),(1,0),(0,1)]
print rotatePolygon(my_polygon,90)

Ans 9

def perimiter(points):
""" returns the length of the perimiter of some shape defined by a list of points """
distances = get_distances(points)

length = 0
for distance in distances:
length = length + distance

return length


def get_distances(points):
""" convert a list of points into a list of distances """
i = 0
distances = []
for i in range(len(points)):
point = points[i]
next_point = points[i+1]
x0 = point[0]
y0 = point[1]
x1 = next_point[1]
y1 = next_point[1]

point_distance = get_distance(x0, y0, x1, y1)
distances.append(point_distance)


def get_distance(x0, y1, x1, y1):
""" use pythagorean theorm to find distance between 2 points """
a = x1 - x2
b = y1 - y2
c_2 = a*a + b*b

return c_2 ** (1/2)