Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt
- """
- This function is based on the Triangle Inequality Theorem, which states
- that the sum of two side lengths of a triangle is always greater
- than the third side. If this is true for all three combinations of added
- side lengths, then the sides will form a triangle.
- """
- def Is_a_Triangle(a, b, c):
- result = ( (a+b) > c) and ( (a+c) > b) and ( (b+c) > a)
- return(result)
- """
- This function is based on Heron's Formula
- https://en.wikipedia.org/wiki/Heron%27s_formula
- It uses the numerically stable version. Point 5 in the above link.
- This, expressed in terms of the variables used is
- Area = Sqrt(w*x*y*z)/4
- """
- def Area_of_a_Triangle(a, b, c):
- w = (a + (b+c))
- x = (c - (a-b))
- y = (c + (a-b))
- z = (a + (b-c))
- return(sqrt(w * x * y * z) / 4.0)
- print("Calculating the area of a triangle.")
- print("Input the length of each side, a,b,c, in consistent units.")
- print("This means all sides in cm for example.")
- Finished = False
- while not Finished:
- a = input("Length of side a (0 to exit) : ")
- Finished = (int(a) == 0)
- if not Finished:
- a = float(a)
- b = input("Length of side b : ")
- b = float(b)
- c = input("Length of side c : ")
- c = float(c)
- if Is_a_Triangle(a, b, c):
- Area = Area_of_a_Triangle(a, b, c)
- print("Area of the triangle is : %.2f"% (Area))
- else:
- print(str(a) + ", " + str(b) + ", " + str(c) + " is not triangle")
- print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement