function calculateTrig() {
var sideA_input = document.getElementById('sideA').value;
var sideB_input = document.getElementById('sideB').value;
var hypotenuseC_input = document.getElementById('hypotenuseC').value;
var angleA_deg_input = document.getElementById('angleA_deg').value;
var angleB_deg_input = document.getElementById('angleB_deg').value;
var a = parseFloat(sideA_input);
var b = parseFloat(sideB_input);
var c = parseFloat(hypotenuseC_input);
var A_deg = parseFloat(angleA_deg_input);
var B_deg = parseFloat(angleB_deg_input);
var resultDiv = document.getElementById('trigResult');
var resultHTML = ";
var knownValues = 0;
if (!isNaN(a)) knownValues++;
if (!isNaN(b)) knownValues++;
if (!isNaN(c)) knownValues++;
if (!isNaN(A_deg)) knownValues++;
if (!isNaN(B_deg)) knownValues++;
if (knownValues = 90 || A_deg = 90 || B_deg 90.0001 || A_deg + B_deg < 89.9999)) { // Add small tolerance for float comparison
resultHTML = 'Angles A and B must sum to 90 degrees in a right triangle.';
resultDiv.innerHTML = resultHTML;
return;
}
// Initial validation for sides
if ((!isNaN(a) && a <= 0) || (!isNaN(b) && b <= 0) || (!isNaN(c) && c = c) {
resultHTML = 'Error: Side A cannot be greater than or equal to Hypotenuse C.';
resultDiv.innerHTML = resultHTML;
return;
}
if (!isNaN(b) && !isNaN(c) && b >= c) {
resultHTML = 'Error: Side B cannot be greater than or equal to Hypotenuse C.';
resultDiv.innerHTML = resultHTML;
return;
}
var changed = true;
var maxIterations = 10; // Prevent infinite loops
var iterationCount = 0;
while (changed && iterationCount < maxIterations) {
changed = false;
iterationCount++;
// Convert degrees to radians for calculations
var A_rad = isNaN(A_deg) ? NaN : A_deg * Math.PI / 180;
var B_rad = isNaN(B_deg) ? NaN : B_deg * Math.PI / 180;
// 1. Solve for missing angle if one is known
if (!isNaN(A_deg) && isNaN(B_deg)) {
B_deg = 90 – A_deg;
changed = true;
}
if (!isNaN(B_deg) && isNaN(A_deg)) {
A_deg = 90 – B_deg;
changed = true;
}
// 2. Solve for missing side using Pythagorean theorem
if (!isNaN(a) && !isNaN(b) && isNaN(c)) {
c = Math.sqrt(a * a + b * b);
changed = true;
}
if (!isNaN(a) && !isNaN(c) && isNaN(b)) {
var b_squared = c * c – a * a;
if (b_squared < 0) { // Should be caught by initial validation, but good to re-check
resultHTML = 'Error: Side A cannot be greater than or equal to Hypotenuse C.';
resultDiv.innerHTML = resultHTML;
return;
}
b = Math.sqrt(b_squared);
changed = true;
}
if (!isNaN(b) && !isNaN(c) && isNaN(a)) {
var a_squared = c * c – b * b;
if (a_squared c
c = a / Math.sin(A_rad);
changed = true;
}
if (!isNaN(A_rad) && !isNaN(a) && isNaN(b)) { // a and A -> b
b = a / Math.tan(A_rad);
changed = true;
}
if (!isNaN(B_rad) && !isNaN(a) && isNaN(c)) { // a and B -> c
c = a / Math.cos(B_rad);
changed = true;
}
if (!isNaN(B_rad) && !isNaN(a) && isNaN(b)) { // a and B -> b
b = a * Math.tan(B_rad);
changed = true;
}
if (!isNaN(A_rad) && !isNaN(b) && isNaN(c)) { // b and A -> c
c = b / Math.cos(A_rad);
changed = true;
}
if (!isNaN(A_rad) && !isNaN(b) && isNaN(a)) { // b and A -> a
a = b * Math.tan(A_rad);
changed = true;
}
if (!isNaN(B_rad) && !isNaN(b) && isNaN(c)) { // b and B -> c
c = b / Math.sin(B_rad);
changed = true;
}
if (!isNaN(B_rad) && !isNaN(b) && isNaN(a)) { // b and B -> a
a = b / Math.tan(B_rad);
changed = true;
}
if (!isNaN(A_rad) && !isNaN(c) && isNaN(a)) { // c and A -> a
a = c * Math.sin(A_rad);
changed = true;
}
if (!isNaN(A_rad) && !isNaN(c) && isNaN(b)) { // c and A -> b
b = c * Math.cos(A_rad);
changed = true;
}
if (!isNaN(B_rad) && !isNaN(c) && isNaN(a)) { // c and B -> a
a = c * Math.cos(B_rad);
changed = true;
}
if (!isNaN(B_rad) && !isNaN(c) && isNaN(b)) { // c and B -> b
b = c * Math.sin(B_rad);
changed = true;
}
// 4. Solve for angles using sides (inverse trig)
if (!isNaN(a) && !isNaN(c) && isNaN(A_deg)) { // a and c -> A
A_deg = Math.asin(a / c) * 180 / Math.PI;
changed = true;
}
if (!isNaN(b) && !isNaN(c) && isNaN(B_deg)) { // b and c -> B
B_deg = Math.asin(b / c) * 180 / Math.PI;
changed = true;
}
if (!isNaN(a) && !isNaN(b) && isNaN(A_deg)) { // a and b -> A
A_deg = Math.atan(a / b) * 180 / Math.PI;
changed = true;
}
if (!isNaN(a) && !isNaN(b) && isNaN(B_deg)) { // a and b -> B
B_deg = Math.atan(b / a) * 180 / Math.PI;
changed = true;
}
} // End while loop
// Final consistency checks (after all calculations)
if (!isNaN(a) && !isNaN(b) && !isNaN(c)) {
var pythagoreanCheck = Math.abs(a * a + b * b – c * c);
if (pythagoreanCheck > 0.001) { // Tolerance for floating point
resultHTML = 'Error: The provided side lengths do not form a right triangle (a² + b² ≠ c²).';
resultDiv.innerHTML = resultHTML;
return;
}
}
if (!isNaN(A_deg) && !isNaN(B_deg)) {
var angleSumCheck = Math.abs(A_deg + B_deg – 90);
if (angleSumCheck > 0.001) {
resultHTML = 'Error: The provided angles A and B do not sum to 90 degrees.';
resultDiv.innerHTML = resultHTML;
return;
}
}
// Display results
resultHTML += '
Trigonometry is a branch of mathematics that studies relationships between side lengths and angles of triangles. It's particularly powerful when applied to right-angled triangles, which are triangles containing one 90-degree angle. This calculator helps you solve for unknown sides or angles in a right triangle when you have enough information.
The Right Triangle
A right-angled triangle has three sides and three angles. One angle is always 90 degrees. The other two angles are acute (less than 90 degrees) and sum up to 90 degrees. The sides are named relative to one of the acute angles (let's call it Angle A):
Hypotenuse (C): The longest side, always opposite the 90-degree angle.
Opposite Side (A): The side directly across from Angle A.
Adjacent Side (B): The side next to Angle A that is not the hypotenuse.
If you consider Angle B, then Side B would be the opposite side, and Side A would be the adjacent side.
Key Principles: SOH CAH TOA and Pythagorean Theorem
To solve right triangles, we primarily use two fundamental principles:
1. SOH CAH TOA (Trigonometric Ratios)
This mnemonic helps remember the definitions of the three basic trigonometric functions:
SOH: Sine = Opposite / Hypotenuse (sin(A) = a / c)
CAH: Cosine = Adjacent / Hypotenuse (cos(A) = b / c)
TOA: Tangent = Opposite / Adjacent (tan(A) = a / b)
These ratios allow you to find an unknown side if you know an angle and one side, or to find an unknown angle if you know two sides (using inverse trigonometric functions like arcsin, arccos, arctan).
2. Pythagorean Theorem
This theorem states that in a right-angled triangle, the square of the hypotenuse (c) is equal to the sum of the squares of the other two sides (a and b):
a² + b² = c²
This theorem is used to find an unknown side when you know the other two sides.
How to Use the Calculator
This calculator can solve for any missing sides or angles of a right-angled triangle. You need to provide at least two known values (excluding the 90-degree angle) for the calculator to work. Here's how:
Identify Knowns: Look at your right triangle and determine which sides (A, B, C) or angles (A, B) you already know.
Enter Values: Input your known values into the corresponding fields. Leave the fields for unknown values blank.
Click "Calculate": The calculator will use trigonometric functions and the Pythagorean theorem to determine the missing values.
Review Results: The calculated sides and angles will be displayed.
"Clear" Button: Use this to reset all input fields and results for a new calculation.
Important: Angles A and B must be acute (between 0 and 90 degrees). The hypotenuse (C) must always be the longest side.
Example Calculation
Let's say you have a right triangle where:
Side A (Opposite Angle A) = 6 units
Side B (Adjacent to Angle A) = 8 units
You want to find the Hypotenuse C, Angle A, and Angle B.
Enter '6' in the "Side A" field.
Enter '8' in the "Side B" field.
Leave "Hypotenuse C", "Angle A", and "Angle B" blank.
Click "Calculate".
The calculator will output:
Side A: 6.0000
Side B: 8.0000
Hypotenuse C: 10.0000 (calculated using a² + b² = c²)
Angle A: 36.8699° (calculated using tan(A) = a/b)
Angle B: 53.1301° (calculated using 90 – A)
Angle C: 90°
This tool is invaluable for students, engineers, architects, and anyone needing to quickly solve right triangle problems in geometry, physics, or construction.