Enter at least three pieces of information (sides or angles) to solve the triangle.
Angles should be in degrees.
function calculateTriangleAngles() {
var sideA = parseFloat(document.getElementById('sideA').value);
var sideB = parseFloat(document.getElementById('sideB').value);
var sideC = parseFloat(document.getElementById('sideC').value);
var angleA = parseFloat(document.getElementById('angleA').value);
var angleB = parseFloat(document.getElementById('angleB').value);
var angleC = parseFloat(document.getElementById('angleC').value);
var resultDiv = document.getElementById('triangleResult');
resultDiv.innerHTML = "; // Clear previous results
// Helper functions
function isValidNum(val) {
return !isNaN(val) && val > 0;
}
function toRadians(degrees) {
return degrees * (Math.PI / 180);
}
function toDegrees(radians) {
return radians * (180 / Math.PI);
}
// Store original inputs to check what was provided
var originalSideA = isValidNum(sideA);
var originalSideB = isValidNum(sideB);
var originalSideC = isValidNum(sideC);
var originalAngleA = isValidNum(angleA);
var originalAngleB = isValidNum(angleB);
var originalAngleC = isValidNum(angleC);
var numKnownSides = (originalSideA ? 1 : 0) + (originalSideB ? 1 : 0) + (originalSideC ? 1 : 0);
var numKnownAngles = (originalAngleA ? 1 : 0) + (originalAngleB ? 1 : 0) + (originalAngleC ? 1 : 0);
var totalKnown = numKnownSides + numKnownAngles;
if (totalKnown 0.01) { // Allow for floating point inaccuracies
resultDiv.innerHTML = 'Invalid angles provided. The sum of angles in a triangle must be 180 degrees.';
return;
}
if (angleA <= 0 || angleB <= 0 || angleC = 180 || angleB >= 180 || angleC >= 180) {
resultDiv.innerHTML = 'Invalid angle values. Angles must be between 0 and 180 degrees (exclusive).';
return;
}
}
// — Step 2: Iteratively solve using Law of Cosines (SSS or SAS) or Law of Sines (AAS/ASA/SSA) —
var solved = false;
var maxIterations = 5; // To prevent infinite loops in case of complex interdependencies
for (var i = 0; i sideC && sideA + sideC > sideB && sideB + sideC > sideA)) {
resultDiv.innerHTML = 'Invalid side lengths. The sum of any two sides must be greater than the third side.';
return;
}
if (!isValidNum(angleA)) {
var cosA = (sideB * sideB + sideC * sideC – sideA * sideA) / (2 * sideB * sideC);
cosA = Math.max(-1, Math.min(1, cosA)); // Clamp to avoid floating point errors
angleA = toDegrees(Math.acos(cosA));
}
if (!isValidNum(angleB)) {
var cosB = (sideA * sideA + sideC * sideC – sideB * sideB) / (2 * sideA * sideC);
cosB = Math.max(-1, Math.min(1, cosB));
angleB = toDegrees(Math.acos(cosB));
}
if (!isValidNum(angleC)) {
var cosC = (sideA * sideA + sideB * sideB – sideC * sideC) / (2 * sideA * sideB);
cosC = Math.max(-1, Math.min(1, cosC));
angleC = toDegrees(Math.acos(cosC));
}
}
// SAS: Two sides and included angle known, find third side and other angles
if (isValidNum(sideB) && isValidNum(sideC) && isValidNum(angleA) && !isValidNum(sideA)) {
sideA = Math.sqrt(sideB * sideB + sideC * sideC – 2 * sideB * sideC * Math.cos(toRadians(angleA)));
}
if (isValidNum(sideA) && isValidNum(sideC) && isValidNum(angleB) && !isValidNum(sideB)) {
sideB = Math.sqrt(sideA * sideA + sideC * sideC – 2 * sideA * sideC * Math.cos(toRadians(angleB)));
}
if (isValidNum(sideA) && isValidNum(sideB) && isValidNum(angleC) && !isValidNum(sideC)) {
sideC = Math.sqrt(sideA * sideA + sideB * sideB – 2 * sideA * sideB * Math.cos(toRadians(angleC)));
}
// AAS/ASA/SSA: Two angles and a side, or two sides and a non-included angle
// Use Law of Sines to find missing sides/angles
// Prioritize finding angles first if possible, then sides
if (isValidNum(sideA) && isValidNum(angleA) && isValidNum(sideB) && !isValidNum(angleB)) {
var sinB = sideB * Math.sin(toRadians(angleA)) / sideA;
if (sinB > 1 || sinB 1 || sinC 1 || sinA 1 || sinC 1 || sinA 1 || sinB < -1) { resultDiv.innerHTML = 'Invalid triangle: Side-Side-Angle combination leads to no solution.'; return; }
angleB = toDegrees(Math.asin(sinB));
}
// After finding an angle, try to find the third angle again
if (isValidNum(angleA) && isValidNum(angleB) && !isValidNum(angleC)) {
angleC = 180 – angleA – angleB;
} else if (isValidNum(angleA) && isValidNum(angleC) && !isValidNum(angleB)) {
angleB = 180 – angleA – angleC;
} else if (isValidNum(angleB) && isValidNum(angleC) && !isValidNum(angleA)) {
angleA = 180 – angleB – angleC;
}
// After finding angles, try to find sides using Law of Sines
if (isValidNum(angleA) && isValidNum(angleB) && isValidNum(angleC)) {
if (isValidNum(sideA) && !isValidNum(sideB) && isValidNum(toRadians(angleA)) && isValidNum(toRadians(angleB))) {
sideB = sideA * Math.sin(toRadians(angleB)) / Math.sin(toRadians(angleA));
}
if (isValidNum(sideA) && !isValidNum(sideC) && isValidNum(toRadians(angleA)) && isValidNum(toRadians(angleC))) {
sideC = sideA * Math.sin(toRadians(angleC)) / Math.sin(toRadians(angleA));
}
if (isValidNum(sideB) && !isValidNum(sideA) && isValidNum(toRadians(angleB)) && isValidNum(toRadians(angleA))) {
sideA = sideB * Math.sin(toRadians(angleA)) / Math.sin(toRadians(angleB));
}
if (isValidNum(sideB) && !isValidNum(sideC) && isValidNum(toRadians(angleB)) && isValidNum(toRadians(angleC))) {
sideC = sideB * Math.sin(toRadians(angleC)) / Math.sin(toRadians(angleB));
}
if (isValidNum(sideC) && !isValidNum(sideA) && isValidNum(toRadians(angleC)) && isValidNum(toRadians(angleA))) {
sideA = sideC * Math.sin(toRadians(angleA)) / Math.sin(toRadians(angleC));
}
if (isValidNum(sideC) && !isValidNum(sideB) && isValidNum(toRadians(angleC)) && isValidNum(toRadians(angleB))) {
sideB = sideC * Math.sin(toRadians(angleB)) / Math.sin(toRadians(angleC));
}
}
// Check if all values are now known
if (isValidNum(sideA) && isValidNum(sideB) && isValidNum(sideC) &&
isValidNum(angleA) && isValidNum(angleB) && isValidNum(angleC)) {
solved = true;
}
// If nothing changed in this iteration, and not solved, break to avoid infinite loop
if (sideA === prevSideA && sideB === prevSideB && sideC === prevSideC &&
angleA === prevAngleA && angleB === prevAngleB && angleC === prevAngleC && !solved) {
break;
}
}
if (solved) {
resultDiv.innerHTML = '
Calculated Triangle Properties:
';
resultDiv.innerHTML += 'Side a: ' + sideA.toFixed(3) + ";
resultDiv.innerHTML += 'Side b: ' + sideB.toFixed(3) + ";
resultDiv.innerHTML += 'Side c: ' + sideC.toFixed(3) + ";
resultDiv.innerHTML += 'Angle A: ' + angleA.toFixed(3) + ' degrees';
resultDiv.innerHTML += 'Angle B: ' + angleB.toFixed(3) + ' degrees';
resultDiv.innerHTML += 'Angle C: ' + angleC.toFixed(3) + ' degrees';
} else {
resultDiv.innerHTML = 'Could not solve the triangle with the given information. Please ensure you have provided enough valid data (e.g., 3 sides, 2 sides and an included angle, 2 angles and a side).';
}
}
Understanding and Calculating Triangle Angles
Triangles are fundamental shapes in geometry, forming the basis for many complex structures and calculations in fields ranging from architecture to physics. A triangle is a polygon with three edges and three vertices. One of its most defining characteristics is that the sum of its interior angles always equals 180 degrees.
The Basics: Sum of Angles
The simplest way to find a missing angle in a triangle is if you already know the other two. Since the total sum of angles is 180 degrees, you can use this straightforward formula:
For example, if a triangle has angles of 60° and 70°, the third angle would be 180° – (60° + 70°) = 180° – 130° = 50°.
Using the Law of Cosines (SSS or SAS)
When you don't know two angles, but you have information about the side lengths, you'll need more advanced trigonometric laws. The Law of Cosines is particularly useful in two scenarios:
1. SSS (Side-Side-Side): All three side lengths are known.
If you know the lengths of all three sides (let's call them a, b, and c), you can find any angle using the Law of Cosines. The formulas are:
cos(A) = (b² + c² – a²) / (2bc)
cos(B) = (a² + c² – b²) / (2ac)
cos(C) = (a² + b² – c²) / (2ab)
Once you calculate the cosine value, you take the inverse cosine (arccos or cos⁻¹) to find the angle in degrees.
You would repeat this for angles B and C, then verify that A + B + C = 180°.
2. SAS (Side-Angle-Side): Two side lengths and the included angle are known.
If you know two sides and the angle between them, you can first find the third side using the Law of Cosines, and then use either the Law of Cosines again or the Law of Sines to find the remaining angles.
a² = b² + c² – 2bc * cos(A)
b² = a² + c² – 2ac * cos(B)
c² = a² + b² – 2ab * cos(C)
Example: A triangle has side b=8, side c=10, and the included Angle A=60°.
Now that you have all three sides (a≈9.165, b=8, c=10) and one angle (A=60°), you can use the Law of Cosines (SSS case) or Law of Sines to find Angle B and Angle C.
Using the Law of Sines (AAS or ASA)
The Law of Sines is useful when you know two angles and one side (AAS or ASA), or two sides and a non-included angle (SSA – though this can be ambiguous).
a / sin(A) = b / sin(B) = c / sin(C)
1. AAS (Angle-Angle-Side) or ASA (Angle-Side-Angle): Two angles and one side are known.
If you know two angles, you can immediately find the third angle (180° – sum of known angles). Then, with all three angles and one side, you can use the Law of Sines to find the other two sides.
Example: A triangle has Angle A=40°, Angle B=60°, and side a=10.
Now use Law of Sines to find side b:
b / sin(B) = a / sin(A)
b / sin(60°) = 10 / sin(40°)
b = 10 * sin(60°) / sin(40°)
b ≈ 10 * 0.866 / 0.6428 ≈ 13.47
Similarly, find side c:
c / sin(C) = a / sin(A)
c / sin(80°) = 10 / sin(40°)
c = 10 * sin(80°) / sin(40°)
c ≈ 10 * 0.9848 / 0.6428 ≈ 15.32
Using the Calculator
Our Triangle Angle & Side Calculator simplifies these complex calculations. Simply input the known values for any three sides or angles (ensuring you provide enough information to define a unique triangle), and the calculator will automatically apply the appropriate trigonometric laws to find all missing angles and side lengths. Remember that angles should always be entered in degrees.
Whether you're a student, engineer, or just curious, this tool provides a quick and accurate way to solve for unknown triangle properties.