Trigonometry Calculator for Right-Angled Triangles
Trigonometry is a branch of mathematics that studies relationships between side lengths and angles of triangles. This calculator specifically focuses on right-angled triangles, which are triangles containing one 90-degree angle. Understanding these relationships is fundamental in fields like engineering, physics, architecture, and surveying.
Understanding Right-Angled Triangles
In a right-angled triangle, the side opposite the right angle is called the hypotenuse (always the longest side). The other two sides are called legs. When referring to a specific non-right angle (let's say Angle A), one leg is opposite to it, and the other leg is adjacent to it.
Side 'a': The leg opposite Angle A.
Side 'b': The leg adjacent to Angle A (and opposite Angle B).
Side 'c': The hypotenuse.
Angle 'A': One of the acute angles (less than 90 degrees).
Angle 'B': The other acute angle.
Angle 'C': The right angle (90 degrees).
Key Trigonometric Ratios (SOH CAH TOA)
The relationships between the angles and sides are defined by three primary trigonometric ratios:
Sine (sin): Opposite / Hypotenuse (SOH)
Cosine (cos): Adjacent / Hypotenuse (CAH)
Tangent (tan): Opposite / Adjacent (TOA)
These ratios allow us to find unknown sides or angles if we have enough information. Additionally, the Pythagorean Theorem (a² + b² = c²) relates the lengths of the three sides.
How to Use the Calculator
To use this calculator, enter at least two known values for your right-angled triangle. You can input any combination of two sides, one side and one angle, or two angles (the third angle is always 90 degrees). The calculator will then determine the remaining unknown sides and angles.
Important Notes:
Angles should be entered in degrees.
Ensure your inputs are consistent with a valid right-angled triangle (e.g., the hypotenuse must be longer than either leg).
If you enter more than two values, the calculator will attempt to solve and validate consistency.
Examples
Let's look at a few scenarios:
Given two sides:
Input: Side A = 3, Side B = 4
Output: Hypotenuse C = 5, Angle A ≈ 36.87°, Angle B ≈ 53.13°
Given one side and one angle:
Input: Hypotenuse C = 10, Angle A = 30°
Output: Side A = 5, Side B ≈ 8.66, Angle B = 60°
Given two angles and one side:
Input: Angle A = 45°, Angle B = 45°, Side A = 7
Output: Side B = 7, Hypotenuse C ≈ 9.90
Right Triangle Solver
Results:
Side A:
Side B:
Hypotenuse C:
Angle A:
Angle B:
Angle C: 90° (Right Angle)
.trigonometry-calculator-app {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #f9f9f9;
}
.trigonometry-calculator-app h2, .trigonometry-calculator-app h3, .trigonometry-calculator-app h4 {
color: #333;
margin-bottom: 15px;
}
.trigonometry-calculator-app p {
line-height: 1.6;
margin-bottom: 10px;
}
.trigonometry-calculator-app ul {
margin-bottom: 15px;
padding-left: 20px;
}
.trigonometry-calculator-app li {
margin-bottom: 5px;
}
.calculator-container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-top: 20px;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.input-group input[type="number"] {
width: calc(100% – 22px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.calculator-container button {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-right: 10px;
}
.calculator-container button:hover {
background-color: #0056b3;
}
.calculator-container button:last-of-type {
background-color: #6c757d;
}
.calculator-container button:last-of-type:hover {
background-color: #5a6268;
}
.result-section {
margin-top: 20px;
padding-top: 15px;
border-top: 1px solid #eee;
}
.result-section p {
margin-bottom: 8px;
font-size: 1.1em;
}
.result-section span {
font-weight: bold;
color: #007bff;
}
#resultMessage {
color: red;
font-weight: bold;
}
function calculateTrigonometry() {
var sideA_input = document.getElementById('sideA').value;
var sideB_input = document.getElementById('sideB').value;
var hypotenuseC_input = document.getElementById('hypotenuseC').value;
var angleA_input = document.getElementById('angleA').value;
var angleB_input = document.getElementById('angleB').value;
var a = parseFloat(sideA_input);
var b = parseFloat(sideB_input);
var c = parseFloat(hypotenuseC_input);
var angleA_deg = parseFloat(angleA_input);
var angleB_deg = parseFloat(angleB_input);
var knownCount = 0;
if (!isNaN(a)) knownCount++;
if (!isNaN(b)) knownCount++;
if (!isNaN(c)) knownCount++;
if (!isNaN(angleA_deg)) knownCount++;
if (!isNaN(angleB_deg)) knownCount++;
var resultMessage = document.getElementById('resultMessage');
resultMessage.textContent = "; // Clear previous messages
// Initial validation
if (knownCount < 2) {
resultMessage.textContent = 'Please enter at least two known values to solve the triangle.';
displayResults(NaN, NaN, NaN, NaN, NaN);
return;
}
if ((!isNaN(angleA_deg) && (angleA_deg = 90)) ||
(!isNaN(angleB_deg) && (angleB_deg = 90))) {
resultMessage.textContent = 'Angles must be between 0 and 90 degrees (exclusive).';
displayResults(NaN, NaN, NaN, NaN, NaN);
return;
}
if ((!isNaN(a) && a <= 0) || (!isNaN(b) && b <= 0) || (!isNaN(c) && c <= 0)) {
resultMessage.textContent = 'Side lengths must be positive.';
displayResults(NaN, NaN, NaN, NaN, NaN);
return;
}
// Iterative solving loop (run multiple times to propagate values)
for (var i = 0; i < 10; i++) { // 10 iterations should be enough for propagation
// 1. Solve for angles if one is known
if (!isNaN(angleA_deg) && isNaN(angleB_deg)) {
angleB_deg = 90 – angleA_deg;
} else if (!isNaN(angleB_deg) && isNaN(angleA_deg)) {
angleA_deg = 90 – angleB_deg;
}
// 2. Pythagorean Theorem
if (!isNaN(a) && !isNaN(b) && isNaN(c)) {
c = Math.sqrt(a * a + b * b);
} else if (!isNaN(a) && !isNaN(c) && isNaN(b)) {
var b_squared = c * c – a * a;
if (b_squared < 0) { resultMessage.textContent = 'Invalid triangle: Side A is longer than Hypotenuse C.'; displayResults(NaN, NaN, NaN, NaN, NaN); return; }
b = Math.sqrt(b_squared);
} else if (!isNaN(b) && !isNaN(c) && isNaN(a)) {
var a_squared = c * c – b * b;
if (a_squared 1) { resultMessage.textContent = 'Invalid triangle: Side A is longer than Hypotenuse C.'; displayResults(NaN, NaN, NaN, NaN, NaN); return; }
angleA_deg = Math.asin(a / c) * 180 / Math.PI;
}
if (!isNaN(b) && !isNaN(c) && isNaN(angleB_deg)) {
if (b / c > 1) { resultMessage.textContent = 'Invalid triangle: Side B is longer than Hypotenuse C.'; displayResults(NaN, NaN, NaN, NaN, NaN); return; }
angleB_deg = Math.acos(b / c) * 180 / Math.PI;
}
if (!isNaN(a) && !isNaN(b) && isNaN(angleA_deg)) {
angleA_deg = Math.atan(a / b) * 180 / Math.PI;
}
if (!isNaN(b) && !isNaN(a) && isNaN(angleB_deg)) { // Angle B from a and b
angleB_deg = Math.atan(b / a) * 180 / Math.PI;
}
// Calculate sides from angles and one side
if (!isNaN(angleA_rad)) {
if (!isNaN(a)) { // Known a, angleA
if (isNaN(c)) c = a / Math.sin(angleA_rad);
if (isNaN(b)) b = a / Math.tan(angleA_rad);
} else if (!isNaN(b)) { // Known b, angleA
if (isNaN(a)) a = b * Math.tan(angleA_rad);
if (isNaN(c)) c = b / Math.cos(angleA_rad);
} else if (!isNaN(c)) { // Known c, angleA
if (isNaN(a)) a = c * Math.sin(angleA_rad);
if (isNaN(b)) b = c * Math.cos(angleA_rad);
}
}
if (!isNaN(angleB_rad)) {
if (!isNaN(b)) { // Known b, angleB
if (isNaN(c)) c = b / Math.sin(angleB_rad);
if (isNaN(a)) a = b / Math.tan(angleB_rad);
} else if (!isNaN(a)) { // Known a, angleB
if (isNaN(b)) b = a * Math.tan(angleB_rad);
if (isNaN(c)) c = a / Math.cos(angleB_rad);
} else if (!isNaN(c)) { // Known c, angleB
if (isNaN(b)) b = c * Math.sin(angleB_rad);
if (isNaN(a)) a = c * Math.cos(angleB_rad);
}
}
}
// Final consistency checks
var finalKnownCount = 0;
if (!isNaN(a)) finalKnownCount++;
if (!isNaN(b)) finalKnownCount++;
if (!isNaN(c)) finalKnownCount++;
if (!isNaN(angleA_deg)) finalKnownCount++;
if (!isNaN(angleB_deg)) finalKnownCount++;
if (finalKnownCount 0.001) { // Allow for floating point inaccuracies
resultMessage.textContent = 'Input values are inconsistent with the Pythagorean theorem (a² + b² ≠ c²).';
displayResults(NaN, NaN, NaN, NaN, NaN);
return;
}
}
// Check angle sum consistency
if (!isNaN(angleA_deg) && !isNaN(angleB_deg)) {
var angleSumCheck = Math.abs(angleA_deg + angleB_deg – 90);
if (angleSumCheck > 0.001) { // Allow for floating point inaccuracies
resultMessage.textContent = 'Input angles are inconsistent (A + B ≠ 90°).';
displayResults(NaN, NaN, NaN, NaN, NaN);
return;
}
}
// Check for valid ranges after calculation
if ((!isNaN(angleA_deg) && (angleA_deg = 90)) ||
(!isNaN(angleB_deg) && (angleB_deg = 90))) {
resultMessage.textContent = 'Calculated angles are outside the valid range (0-90 degrees). Check input consistency.';
displayResults(NaN, NaN, NaN, NaN, NaN);
return;
}
if ((!isNaN(a) && a <= 0) || (!isNaN(b) && b <= 0) || (!isNaN(c) && c <= 0)) {
resultMessage.textContent = 'Calculated side lengths are not positive. Check input consistency.';
displayResults(NaN, NaN, NaN, NaN, NaN);
return;
}
resultMessage.textContent = 'Triangle solved successfully!';
displayResults(a, b, c, angleA_deg, angleB_deg);
}
function displayResults(a, b, c, angleA_deg, angleB_deg) {
document.getElementById('resultSideA').textContent = !isNaN(a) ? a.toFixed(3) : 'N/A';
document.getElementById('resultSideB').textContent = !isNaN(b) ? b.toFixed(3) : 'N/A';
document.getElementById('resultHypotenuseC').textContent = !isNaN(c) ? c.toFixed(3) : 'N/A';
document.getElementById('resultAngleA').textContent = !isNaN(angleA_deg) ? angleA_deg.toFixed(3) + '°' : 'N/A';
document.getElementById('resultAngleB').textContent = !isNaN(angleB_deg) ? angleB_deg.toFixed(3) + '°' : 'N/A';
}
function clearTrigonometry() {
document.getElementById('sideA').value = '';
document.getElementById('sideB').value = '';
document.getElementById('hypotenuseC').value = '';
document.getElementById('angleA').value = '';
document.getElementById('angleB').value = '';
document.getElementById('resultMessage').textContent = '';
displayResults(NaN, NaN, NaN, NaN, NaN);
}