Check out if a triangle is obtuse triangle
Categories:
Determining if a Triangle is Obtuse: A Java Implementation Guide

Learn how to programmatically check if a given triangle is obtuse using Java, covering geometric principles and practical code examples.
Identifying the type of a triangle based on its angles is a fundamental concept in geometry. An obtuse triangle is defined as a triangle where one of its interior angles is greater than 90 degrees. The other two angles must, by definition, be acute (less than 90 degrees). This article will guide you through the process of determining if a triangle is obtuse using Java, focusing on the relationship between side lengths and angles.
Geometric Principle: The Law of Cosines
The most reliable way to determine if a triangle is obtuse, acute, or right-angled, given its side lengths, is by using the Law of Cosines. This law relates the lengths of the sides of a triangle to the cosine of one of its angles. For a triangle with sides a
, b
, and c
, and the angle C
opposite side c
, the Law of Cosines states:
c² = a² + b² - 2ab * cos(C)
Rearranging this to solve for cos(C)
gives:
cos(C) = (a² + b² - c²) / (2ab)
Based on the value of cos(C)
:
- If
cos(C) < 0
, thenC
is an obtuse angle (greater than 90 degrees). - If
cos(C) = 0
, thenC
is a right angle (exactly 90 degrees). - If
cos(C) > 0
, thenC
is an acute angle (less than 90 degrees).
To check if a triangle is obtuse, we only need to find if any of its angles are obtuse. This means we should check the angle opposite the longest side. If the angle opposite the longest side is obtuse, the triangle is obtuse. If it's a right angle, it's a right triangle. If it's acute, then all angles are acute (since the largest angle determines the triangle type), making it an acute triangle.
flowchart TD A[Start] --> B{Get side lengths a, b, c} B --> C{Sort sides: longest side is c} C --> D{Calculate cos(C) = (a² + b² - c²) / (2ab)} D --> E{Is cos(C) < 0?} E -- Yes --> F[Triangle is Obtuse] E -- No --> G{Is cos(C) == 0?} G -- Yes --> H[Triangle is Right] G -- No --> I[Triangle is Acute] F --> J[End] H --> J I --> J
Flowchart for determining triangle type based on side lengths.
Implementing the Check in Java
To implement this logic in Java, we'll need a method that takes three side lengths as input. First, it's crucial to validate that these side lengths can actually form a triangle (Triangle Inequality Theorem: the sum of the lengths of any two sides of a triangle must be greater than the length of the third side). Then, we'll identify the longest side and apply the Law of Cosines. For simplicity, we can directly compare a² + b²
with c²
(where c
is the longest side) to determine the angle type without calculating the cosine explicitly:
- If
a² + b² < c²
, the angle oppositec
is obtuse. - If
a² + b² = c²
, the angle oppositec
is a right angle. - If
a² + b² > c²
, the angle oppositec
is acute.
import java.util.Arrays;
public class TriangleChecker {
/**
* Checks if three given side lengths can form a valid triangle.
* @param a Side length a
* @param b Side length b
* @param c Side length c
* @return true if a valid triangle can be formed, false otherwise.
*/
public static boolean isValidTriangle(double a, double b, double c) {
return (a + b > c) && (a + c > b) && (b + c > a) && (a > 0) && (b > 0) && (c > 0);
}
/**
* Determines if a triangle with the given side lengths is obtuse.
* Assumes the input side lengths can form a valid triangle.
* @param side1 Length of the first side
* @param side2 Length of the second side
* @param side3 Length of the third side
* @return true if the triangle is obtuse, false otherwise.
*/
public static boolean isObtuseTriangle(double side1, double side2, double side3) {
if (!isValidTriangle(side1, side2, side3)) {
throw new IllegalArgumentException("Invalid side lengths: Cannot form a triangle.");
}
// Sort the sides to easily identify the longest side
double[] sides = {side1, side2, side3};
Arrays.sort(sides);
double a = sides[0]; // Shortest side
double b = sides[1]; // Middle side
double c = sides[2]; // Longest side
// Check the angle opposite the longest side (c)
// If a^2 + b^2 < c^2, the angle is obtuse
return (a * a + b * b < c * c);
}
public static void main(String[] args) {
// Example Usage
System.out.println("Triangle (3, 4, 5) is obtuse: " + isObtuseTriangle(3, 4, 5)); // Expected: false (right)
System.out.println("Triangle (3, 4, 6) is obtuse: " + isObtuseTriangle(3, 4, 6)); // Expected: true
System.out.println("Triangle (7, 8, 9) is obtuse: " + isObtuseTriangle(7, 8, 9)); // Expected: false (acute)
System.out.println("Triangle (2, 3, 4) is obtuse: " + isObtuseTriangle(2, 3, 4)); // Expected: true
try {
System.out.println("Triangle (1, 2, 5) is obtuse: " + isObtuseTriangle(1, 2, 5));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage()); // Expected: Invalid side lengths
}
}
}
Java code to check if a triangle is obtuse.
==
) can be problematic due to precision issues. For checking right triangles (a*a + b*b == c*c
), it's often better to check if the difference is within a small epsilon value. However, for obtuse (<
) and acute (>
) checks, direct comparison is generally safe.Considerations for Edge Cases and Floating Point Precision
While the a² + b² < c²
comparison is robust for determining obtuseness, it's important to consider floating-point precision when dealing with real-world measurements or calculations. If your side lengths are derived from complex calculations, small inaccuracies might lead to an angle being classified as slightly acute when it's truly right, or vice-versa. For this specific problem (obtuse vs. not obtuse), the strict inequality <
is generally sufficient. However, if you were also distinguishing between right and acute, you might introduce a small epsilon for the equality check.
Also, remember the isValidTriangle
check is critical. Without it, inputs like 1, 2, 10
would incorrectly be processed, potentially leading to a*a + b*b < c*c
being true, even though these lengths cannot form a triangle at all.