XXgetData.py will read in lines of data formatted like this, with the triangle’s
ID: 3833336 • Letter: X
Question
XXgetData.py will read in lines of data formatted like this, with the triangle’s name and then the six coordinates: Jamie -2.161 -3.366 2.161 3.366 -5.83 3.743
The data will be placed in a list of three lists:
name=[]
vertices=[]
myList=[name,vertices]
name.append("Billy")
vertices.append([[-2.161,-3.366],[2.161,3.366],[-5.83,3.743]])
name.append("Sally")
vertices.append([[0,4],[0,0],[4,0]])
print(myList)
print()
print("Name: ",name[0])
print("List of vertices: ",vertices[0])
print("Vertex 1: ",vertices[0][0])
print("Vertex 2: ",vertices[0][1])
print("Vertex 3: ",vertices[0][2])
print()
print("Name: ",name[1])
print("List of vertices: ",vertices[1])
print("Vertex 1: ",vertices[1][0])
print("Vertex 2: ",vertices[1][1])
print("Vertex 3: ",vertices[1][2])
Output:
[['Jamie', 'Stacey'], [[[-2.161, -3.366], [2.161, 3.366], [-5.83, 3.743]], [[0, 4], [0, 0], [4, 0]]]]
Name: Jamie
List of vertices: [[-2.161, -3.366], [2.161, 3.366], [-5.83, 3.743]]
Vertex 1: [-2.161, -3.366]
Vertex 2: [2.161, 3.366]
Vertex 3: [-5.83, 3.743]
Name: Stacey
List of vertices: [[0, 4], [0, 0], [4, 0]]
Vertex 1: [0, 4]
Vertex 2: [0, 0]
Vertex 3: [4, 0]
The basic program is to produce output similar to Program 4, but with some variations:
Name and vertices
Perimeter All side lengths
Equilateral/Isosceles/Scalene & Acute/Right/Obtuse Area
For example:
Jordan (-2.161,-3.366),(2.161,3.366),(-5.83,3.743)
Perimeter: 24.0 Side lengths: 8.0, 8.0, 8.0
Equilateral & Acute Area: 27.71
The functions should receive a list of three vertices, and each vertex is a list of two values (the X and Y coordinates) rather than six values as the parameters.
[[0, 4], [0, 0], [4, 0]]
and not this
0, 4, 0, 0, 4, 0
That means that a function call would be something like
if equilateral(vertices[4]):
Explanation / Answer
name=[]
vertices=[]
myList=[name,vertices]
name.append("Billy")
vertices.append([[-2.161,-3.366],[2.161,3.366],[-5.83,3.743]])
name.append("Sally")
vertices.append([[0,4],[0,0],[4,0]])
import math
from math import acos, degrees
def findall(vertices):
tmp_list = vertices
s1 = math.sqrt((tmp_list[1][0]-tmp_list[0][0])**2 + (tmp_list[1][1]-tmp_list[0][1])**2)
s2 = math.sqrt((tmp_list[2][0]-tmp_list[1][0])**2 + (tmp_list[2][1]-tmp_list[1][1])**2)
s3 = math.sqrt((tmp_list[2][0]-tmp_list[0][0])**2 + (tmp_list[2][1]-tmp_list[0][1])**2)
s1 = float(math.ceil(s1))
s2 = float(math.ceil(s2))
s3 = float(math.ceil(s3))
perimeter = s1 + s2 + s3
print("vertices: ",vertices)
print("All edges: ",s1, s2, s3)
print("Perimeter: ", perimeter)
if s1 == s2 == s3:
print("Equilateral")
elif s1 == s2 != s3 or s1 != s2 == s3 or s1 == s3 != s2:
print("Isosceles")
else:
print("Scalane")
a1 = int(degrees(acos((s1 * s1 + s2 * s2 - s3 * s3)/(2.0 * s1 * s2))))
a2 = int(degrees(acos((s2 * s2 + s3 * s3 - s1 * s1)/(2.0 * s2 * s3))))
a3 = int(degrees(acos((s1 * s1 + s3 * s3 - s2 * s2)/(2.0 * s1 * s3))))
if a1 < 90 and a2 < 90 and a3 < 90:
print("Acute")
elif a1 == 90 or a2 == 90 or a3 == 90:
print("right")
elif (a1 > 90 and a2 < 90 and a3 < 90) or (a2 > 90 and a1 < 90 and a3 < 90) or (a3 > 90 and a2 < 90 and a1 < 90):
print("obtuse")
p = perimeter/2
area = math.sqrt(p*(p-s1)*(p-s2)*(p-s3))
print("Area: ", round(area, 2))
for i in range(len(vertices)):
print("name: " , name[i])
findall(vertices[i])
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.