Implement the Triangle class as follows: Triangle(PointA, PointB, PointC) instantiates the Triangle object, where Point# is
Question:
Implement the Triangle class as follows:
Triangle(PointA, PointB, PointC) instantiates the Triangle object, where Point# is of the given Point class. In each input, two triangles are given.
- Each triangle contents three vertices.
- Triangle.area() returns the area of the Triangle as a float. The following formula to compute the area may be used:
- | [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]/2 |
- (x1, y1), (x2, y2), (x3, y3) are 3 vertices of a triangle; | ... | is the absolute function.
- Return float.
- Triangle.is_triangle() returns whether or not the 3 Points form a valid triangle with a tolerance of TOL, which is given in the file.
- Return True if it is a valid triangle; False otherwise. (boolean)
- "triangleA == triangleB" compares whether the two triangles are equal. Use Side-Side-Side (SSS) congruence check.
- SSS congruence states that two Triangles are congruent if every side of one Triangle is of equal length to a corresponding side of another Triangle.
- For example, a triangle of lengths 3, 4, 5 will be congruent to a triangle of lengths 5, 3, 4 but not to a triangle of 5, 5, 5.
- Return True if the two triangles are equal; False otherwise. (boolean)
- Note: The file read-in and print statements are already completed. Just fill out the Triangle class.
- The output will be as follows.
print(triangleA.area()) print(triangleB.area()) print(triangleA.is_triangle()) print(triangleB. is_triangle()) print(triangleA == triangleB) |
Template for this question:
import sys
import math
TOL = 0.01
class Point (object):
# constructor
def __init__(self, x = 0, y = 0):
# get the distance to another Point object
def dist (self, other):
class Triangle (object):
# constructor
def __init__(self, PointA, PointB, PointC):
# check congruence of Triangles with equality
# returns True or False (bolean)
def __eq__(self, other):
# returns whether or not the triangle is valid
# returns True or False (bolean)
def is_triangle(self):
# return the area of the triangle:
def area(self):
######################################################
# The code below is filled out, DO NOT EDIT. #
######################################################
# takes a string of coordinates and changes it to a list of Points
def get_points(coords_str):
coords = [float(c) for c in coords_str.split(" ")]
return [Point(c[0], c[1]) for c in zip(*[iter(coords)]*2)]
def main():
# read the two triangles
pointsA = get_points(sys.stdin.readline().strip())
pointsB = get_points(sys.stdin.readline().strip())
triangleA = Triangle(pointsA[0], pointsA[1], pointsA[2])
triangleB = Triangle(pointsB[0], pointsB[1], pointsB[2])
# Print final output
print(triangleA.area())
print(triangleB.area())
print(triangleA.is_triangle())
print(triangleB.is_triangle())
print(triangleA == triangleB)
if __name__ == "__main__":
main()
Auditing An International Approach
ISBN: 978-0071051415
6th edition
Authors: Wally J. Smieliauskas, Kathryn Bewley