Finding If A Triangle Is Right-angled Or Not
Solution 1:
Your function is completely wrong.
You cannot find angle as ratio of a side and perimeter.
Expression if one and two
does not calculate sum - and
here is logical (boolean) operator.
To find whether rectangle is right, you can exploit Pythagorean theorem
def right_angled(a, b, c):
if (a*a+b*b==c*c) or (c*c+b*b==a*a) or (a*a+c*c==b*b) :
return "The triangle is right-angled."
else:
return "The triangle is not right-angled."
Or just return boolean result
return (a*a+b*b==c*c) or (c*c+b*b==a*a) or (a*a+c*c==b*b)
Solution 2:
I suggest using the Pythagorean theorem to achieve this (a^2+b^2=c^2
) by testing the 3 combinations of side lengths. To compensate for floating point imprecision, compare within a range:
def right_angled(a, b, c, e):
return abs(a*a+b*b-c*c)<e or abs(b*b+c*c-a*a)<e or abs(c*c+a*a-b*b)<e
However, the range depends on the scale of the side lengths, i.e., small triangles pass the test more easily than big triangles. For example, any triangle with side length ~0.01
will pass the test if e=0.01
. For this reason, it is safer (but more expensive) to normalize the side lengths using the formula (a^2+b^2)/c^2=1
def right_angled(a, b, c, e):
return c>0 and abs(1-(a*a+b*b)/(c*c))<e or \
a>0 and abs(1-(b*b+c*c)/(a*a))<e or \
b>0 and abs(1-(c*c+a*a)/(b*b))<e
Post a Comment for "Finding If A Triangle Is Right-angled Or Not"