# Python Program to Solve Polygon Problems Assignment Solution.

## Instructions

Objective
Write a program to solve polygon problems in python.

## Requirements and Specifications

Source Code

```CONV POLY from SimplePoly import * class ConvPoly(SimplePoly): def __init__(self, *vertices): vertices = list(vertices) # Check that the vertices form a convex polygon # Check that all points are to the left of the other # Create a copy of the vertices verts_copy = vertices.copy() # Append the first point at the end verts_copy.append(vertices[0]) for i in range(len(verts_copy)-2): p = verts_copy[i] q = verts_copy[i+1] r = verts_copy[i+2] # If the point does not lies to the left of q and r, then the polygon is not convex if not r.left_of(p,q): raise Exception # If we reach this line it is because the given vertices form a convex polygon super().__init__(*vertices) EQUILATERAL TRIANGLE from ConvPoly import * from Point import * class EquiTriangle(ConvPoly): def __init__(self, length): self.length = length # We define the first vertex which is the origin vertices = [Point(0,0)] # Now, we take the next point to the right of the origin with a length l vertices.append(Point(length, 0)) # The third point is found by taking the previous point and rotating it 60 degrees p = Point(vertices[1].x, vertices[1].y) p.rotate(60) vertices.append(p) # Now, call the parent constructor super().__init__(*vertices) def area(self): """ This function calculates the area of an equilateral triangle :return: area """ return (np.sqrt(3)/4.0)*self.length**2 POINTS import numpy as np class Point: def __init__(self, x: float = 0, y: float = 0): self.x = x self.y = y def translate(self, s: float, t: float): """ Translates the x point by 's' units and the y point by 't' units :param s: x translation :param t: y translation :return: """ self.x += s self.y += t def rotate(self, degree): """ Rotates the point counter-clockwise direction by the given degrees :param degree: degrees :return: """ # Conver the degrees to radians rad = degree*np.pi/180.0 # Rotate newX = self.x*np.cos(rad) - self.y*np.sin(rad) newY = self.x*np.sin(rad) + self.y*np.cos(rad) self.x = newX self.y = newY def distance(self, other): """ Computes the distance to another point :param other: Point object :return: distance """ return np.sqrt((self.x - other.x)**2 + (self.y - other.y)**2) def left_of(self, q, r): # p is the point we want to check if it lies to the left of qr px = self.x py = self.y rx = r.x ry = r.y qx = q.x qy = q.y return (rx*py - px*ry) + (qx*ry - qx*py) + (qy*px - qy*rx) > 0 def right_of(self, q, r): # p is the point we want to check if it lies to the right of qr px = self.x py = self.y rx = r.x ry = r.y qx = q.x qy = q.y return (rx * py - px * ry) + (qx * ry - qx * py) + (qy * px - qy * rx) < 0 def __str__(self): return "({:.2f}, {:.2f})".format(self.x, self.y) def __repr__(self): return "Point with coordinates ({:.2f}, {:.2f})".format(self.x, self.y) RECTANGLE from ConvPoly import * from Point import * class Rectangle(ConvPoly): def __init__(self, length, width): self.length = length self.width = width # Create the first vertex at the origin p0 = Point(0,0) # Second vertex at the right of origin p1 = Point(length, 0) # Third vertex at the top of p1 p2 = Point(length, width) # Fourth vertex at the top of the origin p3 = Point(0, width) vertices = [p0, p1, p2, p3] # Call parent constructor super().__init__(*vertices) def area(self): """ Returns the area of a rectangle :return: area """ return self.width*self.length SQUARE from Rectangle import * from Point import * class Square(Rectangle): def __init__(self, length): # Call Rectangle constructor super().__init__(length, length) TEST POLY from polys import * if __name__ == "__main__": print("\n*********************************************") print("Running test module for Problem 1, Homework 6") print("*********************************************") p1 = Point(1, 1) p2 = Point(3, 0) p3 = Point(4, 1) p4 = Point(3, 3) p5 = Point(2, 3) print("---------Testing the Point class---------\n") print("p1: ", p1) print("p2: ", p2) print("Distance b/w p1 and p2: ", p1.distance(p2)) p1.rotate(45) print("p1 rotated by 45 degrees: ", p1) p1.rotate(-45) print("p1 rotated by -45 degrees: ", p1) p2.translate(-2, 3) print("p2 translated by (-2, 3): ", p2) p2.translate(2, -3) print("p2 translated back by (2, -3): ", p2) print() print("---------Testing the SimplePoly class---------\n") S = SimplePoly(p2, p3, p4, p5) print("Simple polygon S has been created as follows: ") print(S) print("The perimeter of S is ", S.perimeter()) print() print("---------Testing the ConvPoly class---------") newp5 = Point(2, 1.75) try: P = ConvPoly(p1, p2, p3, p4, newp5) except: print("\nCORRECT: ConvPoly __init__ raises exception for violating convexity.\n") else: print("\nINCORRECT: ConvPoly __init__ NOT CHECKING FOR CONVEXITY.\n") P = ConvPoly(p1, p2, p3, p4, p5) print("Convex polygon P has been created as follows: ") print(P) print("The perimeter of P is ", P.perimeter()) P.translate(-2, 3) print("\nP translated by (-2, 3): ") print(P) P.rotate(45) print("\nNow P is rotated by 45 degrees: ") print(P) print("\nThe perimeter of P is ", P.perimeter()) print("(The answer should be the same as the previous one.)") print("\nThe vertices of P are (this checks __getitem__ and __len__): ") for i in range(len(P)): print(P[i]) print("\nChecking iter and next: ") i = iter(P) print(next(i)) print(next(i)) print(next(i)) print(next(i)) print(next(i)) try: print(next(i)) except StopIteration: print("A StopIteration was (correctly) caught.") print() print("---------Testing EquiTriangle, Rectangle, and Square---------\n") print() E = EquiTriangle(3) print("An equilateral triangle E with side length 3 has been created: ") print(E) print("The perimeter of E is ", E.perimeter()) print("The area of E is ", E.area()) E.rotate(90) print("E, after it has been rotated by 90 degrees: ") print(E) print() R = Rectangle(3, 4) print("A rectangle R with width 3 and height 4 has been created.") print("Its vertices are: ") for v in R: print(v) print("The perimeter of R is ", R.perimeter()) print("The area of R is ", R.area()) R.translate(-1, 2) print("R has been translated by (-1, 2). Its vertices are: ") for v in R: print(v) print() S = Square(5) print("A square S of side length 5 has been created: ") print("Its vertices are: ") for i in range(len(S)): print(S[i]) print("The perimeter of S is ", S.perimeter()) print("The area of S is ", S.area()) print() print("That's all for now. Goodbye!")```