The main program will be named XXProg4.py. The file with the functions will be n
ID: 3730358 • Letter: T
Question
The main program will be named XXProg4.py. The file with the functions will be named XXtriFun.py. (XX, of course, represents your initials.) The main program will loop through a data file named input4.txt reading and processing each line of data. The data will be in this format: X1 Y1 X2 Y2 X3 Y3 It represents the (x,y) coordinates of the three vertices of a triangle, such as these: 1.4 2.3 3.2 4.1 5.0 6.9 -5 3.1 3.0 3.1 -1 -1 For each triangle, your program will first check to make sure each point is unique (no duplicates) and that the three points are not collinear. If the triangle is not valid, print out the reason (duplicate points or collinear) and go to the next line of data. If the data is valid, report on the following (see sample below). The output will be printed from the main program, not from within the functions. List the vertices Length of the shortest side (2 decimal places) Perimeter (2 decimal places) Area (2 decimal places) Whether it is right, acute or obtuse Whether it is scalene, isosceles or equilateral (choose one only) Use an epsilon of 0.0001 when evaluating features. Function Names and return values. All will be called with the arguments (x1,y1,x2,y2,x3,y3) duplicatePts boolean collinear boolean findShortest float perimeter float area float right boolean acute boolean obtuse boolean scalene boolean isosceles boolean equilateral Boolean And then there will be one more … sideLength … that will have four arguments (e.g. x1,y1,x2,y2) The output will appear on both the screen and in an output file named XXOut04.txt, following this format: Vertices: (1.4, 2.3), (3.2, 4.1), (5.0, 6.9) Shortest side is 2.55 Perimeter is 11.72 Area is 0.90 Scalene Obtuse If the data is invalid, write the appropriate message. (Not a triangle: The points are collinear. or Not a triangle: There are duplicate points.)
Explanation / Answer
import math
def sideLength(x1,y1,x2,y2):
return math.sqrt((x1-x2)**2+(y1-y2)**2)
def duplicatePts(x1,y1,x2,y2,x3,y3):
if( ((x1==x2)and(y1==y2)) or ((x1==x3)and(y1==y3)) or ((x2==x3)and(y2==y3))):
return True
else:
return False
def collinear(x1,y1,x2,y2,x3,y3):
if((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))==0):
return True
else:
return False
def findShortest(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
return min(l1,l2,l3)
def perimeter(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
return l1+l2+l3
def area(x1,y1,x2,y2,x3,y3):
return (x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))*0.5
def right(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
if((l1*l1==l2*l2+l3*l3) or (l2*l2==l1*l1+l3*l3) or (l3*l3==l2*l2+l1*l1) ):
return True
else:
return False
def acute(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
if((l1*l1<l2*l2+l3*l3) or (l2*l2<l1*l1+l3*l3) or (l3*l3<l2*l2+l1*l1) ):
return True
else:
return False
def obtuse(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
if((l1*l1>l2*l2+l3*l3) or (l2*l2>l1*l1+l3*l3) or (l3*l3>l2*l2+l1*l1) ):
return True
else:
return False
def scalene(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
if(l1!=l2 and l2!=l3 and l3!=l1):
return True
else:
return False
def isosceles(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
if(l1==l2 or l2==l3 or l3==l1):
return True
else:
return False
def equilateral(x1,y1,x2,y2,x3,y3):
l1 = sideLength(x1,y1,x2,y2)
l2 = sideLength(x1,y1,x3,y3)
l3 = sideLength(x2,y2,x3,y3)
if(l1==l2 and l2==l3):
return True
else:
return False
def main():
f = open('input4.txt', 'r')
for line in f:
coord = []
for word in line.split():
coord.append(float(word))
if(duplicatePts(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])):
print("Duplicate Points")
elif(collinear(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])):
print("Points are collinear")
else:
minside = findShortest(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])
per = perimeter(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])
are = area(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])
print('Vertices is: ({},{}),({},{}),({},{}) Shortest side is {}'.format(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5],round(minside,2)))
print('Perimeter is: {} Area is {}'.format(round(per,2),round(are,2)))
if(equilateral(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])):
print("Equilateral",end=' ')
elif(isosceles(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])):
print("Isosceles",end=' ')
else:
print("Scalene", end = ' ')
if(right(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])):
print("Right",end=' ')
elif(obtuse(coord[0],coord[1],coord[2],coord[3],coord[4],coord[5])):
print("Obtuse",end=' ')
else:
print("Acute", end = ' ')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.