Question: Need Help urgently! Starting with assignment XX code ( XX code is supplied at the end) Add a superclass named Polygon . Make Triangle a
Need Help urgently!
Starting with assignment XX code (XX code is supplied at the end)
Add a superclass named Polygon. Make Triangle a subclass of Polygon. Add another subclass named Quad.
Quad will deal with simple, convex quadrilaterals. The input file will list the vertices consecutively (either clockwise or counterclockwise), so you do not have to do any calculations to decide which of the six segments are sides and which are diagonals. (How nice is that?)
Note that some methods should now be in the superclass. For example, sideLength(p1,p2) will return the distance between two points. This is used by both Triangle and Quad, so it should be in Polygon.
You can also have a compareSides(s1,s2) method in Polygon, as this can then be used by both Triangle and Quad to see if two sides have equal lengths. [Be sure to use epsilon rather than ==, with the constant EPSILON = 0.0001.] (very important) .Here is a good set of vertices for testing if the sides are of equal length:
(-2.161,-3.366),(2.161,3.366),(-5.83, 3.743)
In addition, you probably should have Herons Formula as a method in the Polygon, and perhaps there will be other methods there also.
main
read in a line of data from polyInput.txt
this data will be used to create three or four points
the triangle constructor will be called if there are three points
the quadrilateral constructor will be called if there are four points
each triangle will be placed in a list of triangles (named myTri)
each quadrilateral will be placed in a list of quadrilaterals (named myQuad)
Now that we have a list of triangle objects and a list of quad objects;
go through myTri and for each triangle print out the vertices, the area, and identify it with two words (acute, obtuse or right; and scalene, isosceles, or equilateral)
go through myQuad and for each quadrilateral print out the vertices, the area, and identify it by the most specific name on the next page.
sample output will look like this:
Vertices: (1.4, 2.3), (3.2, 4.1), (5.0, 6.9)
Area: 0.90
The triangle is: obtuse and scalene.
Vertices: (0,0),(0,1),(1,1),(1,0)
Area: 1.00
This quadrilateral is a square.
sample input file (Note: The data file will have one space to separate items.)
1.4 2.3 3.2 4.1 5.0 6.9
0 0 0 1 1 1 1 0
The input file will be named polyInput.txt (Hard-code the name into your program.)
The output should be displayed on the screen **and** written to the file called XXmyPoly.txt
VERY IMPORTANT!!! - You will submit (at least) five files XXProg7.py, XXPoint.py, XXTriangle.py, XXQuad.py and XXPoly.py

XX code
.......................
XXmain.py
from XXpoints import point # import the point for the XXpoints from XXtriangles import triangle # import the triangle from XXtriangles
of = open(r'XXoutput6.txt', 'w') # write the output to the XXoutput6.txt f = open(r'input6.txt') # read the input myTriangles = [] # myTriangles list for line in f.readlines(): # read the lines all_points = line.split() # split the points tri_points = [] # tri-points list for i in (0, 2, 4): x = all_points[i] # all points in i y = all_points[i + 1] # append 1 tri_points.append(point(float(x), float(y))) # convert to floating point myTriangles.append(triangle(*tri_points)) # append tri points
def get_vertices_in_string(tri): # def the vertices in the string vertices = "" # empty string for p in tri.getVertices(): vertices += "({0}, {1}),".format(p.x, p.y) # format the p in vertices return vertices[:-1] # return the vertices
valid_triangles = [] # valid triangles list for each_tri in myTriangles: # defining every triangle in my triangles if each_tri.duplicatePts(): of.write("Deleting triangle with points : {0} because it contain duplicate points. ".format( get_vertices_in_string(each_tri))) # essentially gets rids of duplicate point triangles since illegal continue if each_tri.collinear(): # repeat similar process for collinear of.write("Deleting triangle with points : {0} because points are collinear. ".format( get_vertices_in_string(each_tri))) continue valid_triangles.append(each_tri) # continue with the valid triangles
myTriangles = valid_triangles for each_tri in myTriangles: # for all the valid triangles of.write(" Vertices: {0}".format(get_vertices_in_string(each_tri))) # for vertices of.write("\tShortest side is {0:.2f}".format(each_tri.findShortest())) # formatter to write to output the shortest side of.write(" Perimeter is {0:.2f}".format(each_tri.perimeter())) # formatter to write to output the perimeter of.write("\t\tArea is {0:.2f}".format(each_tri.area())) # formatter to write to output the area tri_type = None # tri type is none if each_tri.scalene(): tri_type = "Scalene" # for scalene triangles elif each_tri.isosceles(): tri_type = "Isosceles" # for isosceles triangles elif each_tri.equilateral(): tri_type = "Equilateral" # for equilateral triangles of.write(" {0}".format(tri_type)) tri_type = None if each_tri.right(): tri_type = "Right" # for right angled triangles elif each_tri.acute(): tri_type = "Acute" # for acute angled triangles elif each_tri.obtuse(): tri_type = "Obtuse" # for obtuse and angled triangles of.write("\t\t{0} ".format(tri_type)) # set the formatter for the tri_type
of.close() # closing the files f.close()
XXpoints.py
class point(object): # creating class point def __init__(self, x, y): # initialize the self x and y self.x = x self.y = y def getX(self): # defining the getX return self.x def getY(self): # defining the getY return self.y
XXtriangles.py
from math import sqrt # import square root from math class triangle: # defining the class triangle def __init__(self, p1, p2, p3): self.p1 = p1 # p1 self.p2 = p2 # p2 self.p3 = p3 # p3 def sideLength(p1, p2): length = sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2) # formula for finding the side length return length self.side1 = sideLength(p1, p2) self.side2 = sideLength(p2, p3) self.side3 = sideLength(p3, p1) def getVertices(self): return [self.p1, self.p2, self.p3] # formula for getting the vertices def getSides(self): return [self.side1, self.side2, self.side3] # formula for the getting the sides def duplicatePts(self): if ((self.p1.x == self.p2.x) and (self.p1.y == self.p2.y)): # checking to search for duplicate points return True if ((self.p1.x == self.p3.x) and (self.p1.y == self.p3.y)): return True if ((self.p3.x == self.p2.x) and (self.p3.y == self.p2.y)): return True # return if true or false return False def collinear(self): if self.area(): # if collinear then return either true or false return False else: return True def findShortest(self): # find the shortest return min(self.getSides()) def perimeter(self): # find the perimeter return sum(self.getSides()) def area(self): s = sum(self.getSides()) / 2 # formula for triangle area area = (s * (s - self.side1) * (s - self.side2) * (s - self.side3)) ** 0.5 return area def right(self): # formula to figure if the triangle is right tr_type = self.typeObtuseRightAcute(self.side1, self.side2, self.side3) if tr_type == "right": return True else: return False def acute(self): # formula to figure if the triangle is acute tr_type = self.typeObtuseRightAcute(self.side1, self.side2, self.side3) if tr_type == "acute": return True else: return False def obtuse(self): # formula to figure out if the triangle is obtuse tr_type = self.typeObtuseRightAcute(self.side1, self.side2, self.side3) if tr_type == "obtuse": return True else: return False def scalene(self): # formula to figure out whether the triangle is scalene tri_type = self.typeScaleneIsoscelesEquilater(self.side1, self.side2, self.side3) if tri_type == "scalene": return True else: return False def isosceles(self): # isosceles triangle tri_type = self.typeScaleneIsoscelesEquilater(self.side1, self.side2, self.side3) if tri_type == "isosceles": return True else: return False def equilateral(self): # equilateral triangle tri_type = self.typeScaleneIsoscelesEquilater(self.side1, self.side2, self.side3) if tri_type == "equilateral": return True else: return False def typeScaleneIsoscelesEquilater(self, side1, side2, side3): totl_equal_sides = 0 # defining class for whether is scalene isosceles triangle if (side1 == side2): totl_equal_sides += 1 if (side2 == side3): totl_equal_sides += 1 if (side3 == side1): totl_equal_sides += 1 if totl_equal_sides == 3: return "equilateral" elif totl_equal_sides == 2: return "isosceles" else: return "scalene" def typeObtuseRightAcute(self, side1, side2, side3): # defining class for whether obtuse, right and acute. epsilon = 0.0001 [var1, var2, largest] = sorted([side1, side2, side3]) if abs((largest) ** 2 - ((var1 ** 2 + (var2) ** 2))) ((var1 ** 2 + (var2) ** 2)): return "obtuse" else: return "acute"
PLEASE MODIFY THE EXISITING CODE PROVIDED TO FIT THE REQUIREMENTS OF THE NEW ASSIGNMENT.
Thanks! Any help will be appreciated as this is due soon.
Quadrilateral C. Kite Trapezoid (Us) Parallelogram Isosceles Trapezoid Rectanqle Rhombus Square Quadrilateral C. Kite Trapezoid (Us) Parallelogram Isosceles Trapezoid Rectanqle Rhombus Square
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
