To determine if a triangle is right-angled in Python and identify which angle is the right angle, you primarily use the Pythagorean theorem. This fundamental geometric principle states that in a right-angled triangle, the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the lengths of the other two sides (legs).
The most straightforward approach in Python involves comparing the square of each side length to the sum of the squares of the other two, checking for equality.
Understanding the Pythagorean Theorem
The Pythagorean theorem is expressed as:
$a^2 + b^2 = c^2$
Where:
a
andb
are the lengths of the two shorter sides (legs) of the right-angled triangle.c
is the length of the longest side (hypotenuse), which is always opposite the right angle.
If this condition holds true for any combination of two sides squaring and summing to the third side squared, the triangle is right-angled.
Implementing a Right-Angle Check in Python
You can create a Python function that takes the three side lengths as input and returns whether the triangle is right-angled. The key is to check all possible permutations for the hypotenuse.
Here's how you can implement this in Python:
def is_right_angled(a, b, c):
"""
Checks if a triangle with given side lengths is right-angled using the Pythagorean theorem.
Args:
a (float): Length of the first side.
b (float): Length of the second side.
c (float): Length of the third side.
Returns:
tuple: A tuple containing (boolean, str) indicating if it's right-angled
and which side is the hypotenuse (or 'none' if not right-angled).
"""
# Sort the sides to easily identify potential legs and hypotenuse,
# or check all permutations as per the core logic.
# For robustness with floating point numbers, a small epsilon is often used,
# but for exact checking, direct equality can be used.
# Check permutations for the hypotenuse
if (a*a + b*b == c*c):
return True, f"Side c ({c}) is the hypotenuse. The right angle is opposite side c."
elif (c*c + b*b == a*a):
return True, f"Side a ({a}) is the hypotenuse. The right angle is opposite side a."
elif (a*a + c*c == b*b):
return True, f"Side b ({b}) is the hypotenuse. The right angle is opposite side b."
else:
return False, "The triangle is not right-angled."
# Example Usage:
print("--- Example 1: Right-angled triangle (3-4-5) ---")
is_right, info = is_right_angled(3, 4, 5)
print(f"Is right-angled: {is_right}. {info}")
print("\n--- Example 2: Right-angled triangle (5-12-13) ---")
is_right, info = is_right_angled(5, 12, 13)
print(f"Is right-angled: {is_right}. {info}")
print("\n--- Example 3: Non-right-angled triangle (2-3-4) ---")
is_right, info = is_right_angled(2, 3, 4)
print(f"Is right-angled: {is_right}. {info}")
print("\n--- Example 4: Right-angled triangle with different hypotenuse order (10-6-8) ---")
is_right, info = is_right_angled(10, 6, 8)
print(f"Is right-angled: {is_right}. {info}")
Explanation of the Logic
The function is_right_angled(a, b, c)
directly incorporates the core logic:
if (a*a + b*b == c*c)
: Checks ifc
is the hypotenuse.elif (c*c + b*b == a*a)
: Checks ifa
is the hypotenuse.elif (a*a + c*c == b*b)
: Checks ifb
is the hypotenuse.
This comprehensive check ensures that no matter which side is the longest (hypotenuse), the theorem is correctly applied. If any of these conditions are met, the function identifies the triangle as right-angled and specifies which side is the hypotenuse, thus indicating the location of the right angle (opposite that hypotenuse).
Dealing with Floating-Point Precision
When working with floating-point numbers (e.g., side lengths like 3.0
, 4.0
, 5.0
or calculated values), direct equality (==
) checks can sometimes be unreliable due to how computers store these numbers. A more robust approach often involves checking if the difference between the two sides of the equation is very small, within a defined tolerance (epsilon).
import math
def is_right_angled_robust(a, b, c, epsilon=1e-9):
"""
Checks if a triangle is right-angled with tolerance for floating-point numbers.
"""
sides_squared = sorted([x*x for x in [a, b, c]])
s1_sq, s2_sq, s3_sq = sides_squared[0], sides_squared[1], sides_squared[2]
if math.isclose(s1_sq + s2_sq, s3_sq, rel_tol=epsilon):
# The largest side squared is the sum of the other two squares
# The hypotenuse is the side corresponding to sqrt(s3_sq)
hypotenuse = math.sqrt(s3_sq)
return True, f"The hypotenuse is {hypotenuse}. The right angle is opposite this side."
else:
return False, "The triangle is not right-angled."
print("\n--- Example 5: Floating-point right-angled triangle ---")
is_right, info = is_right_angled_robust(3.0, 4.0, 5.0)
print(f"Is right-angled: {is_right}. {info}")
print("\n--- Example 6: Floating-point non-right-angled triangle ---")
is_right, info = is_right_angled_robust(3.1, 4.2, 5.3)
print(f"Is right-angled: {is_right}. {info}")
In is_right_angled_robust
:
- We first square all sides and sort them. This makes
s3_sq
always the largest squared side (potential hypotenuse squared). math.isclose()
is used to compares1_sq + s2_sq
ands3_sq
within a relative tolerance (epsilon
), which is safer for floats.- The hypotenuse is then explicitly calculated from
s3_sq
to report which side the right angle is opposite.
Summary of Right Angle Identification
Condition Met | Hypotenuse | Location of Right Angle |
---|---|---|
a*a + b*b == c*c |
Side c |
Opposite side c |
c*c + b*b == a*a |
Side a |
Opposite side a |
a*a + c*c == b*b |
Side b |
Opposite side b |
By utilizing the Pythagorean theorem, you can reliably determine if a triangle is right-angled and easily identify which of its angles is the 90-degree angle based on which side acts as the hypotenuse.