Calculate a missing side of a triangle using the Law of Cosines or Law of Sines. Enter known values and select which side you need to find.
Side C (using Sides A, B and Angle C – Law of Cosines)
Side A (using Sides B, C and Angle A – Law of Cosines)
Side B (using Sides A, C and Angle B – Law of Cosines)
Side A (using Side B, Angle A, Angle B – Law of Sines)
Side B (using Side A, Angle A, Angle B – Law of Sines)
Side C (using Side A, Angle A, Angle C – Law of Sines)
Result
—
units
Understanding Triangle Side Calculations
Triangles are fundamental geometric shapes with three sides and three internal angles. Knowing some of these measurements allows us to calculate the others. This calculator helps you find a missing side length when you have sufficient information about the other sides and angles, using two primary trigonometric laws: the Law of Cosines and the Law of Sines.
When to Use Which Law
Law of Cosines: This is typically used when you have the lengths of two sides and the measure of the included angle (the angle between those two sides). It can also be used if you know all three side lengths (to find an angle, though this calculator focuses on finding sides).
Law of Sines: This is most useful when you have the length of one side and the measures of two angles, or when you have two sides and an angle opposite one of them.
The Formulas
Let the sides of the triangle be denoted by a, b, and c, and their opposite angles be A, B, and C respectively (all angles in degrees).
Law of Cosines:
To find side c: c² = a² + b² - 2ab cos(C) => c = sqrt(a² + b² - 2ab cos(C))
To find side a: a² = b² + c² - 2bc cos(A) => a = sqrt(b² + c² - 2bc cos(A))
To find side b: b² = a² + c² - 2ac cos(B) => b = sqrt(a² + c² - 2ac cos(B))
Law of Sines:
a / sin(A) = b / sin(B) = c / sin(C)
To find side a: a = b * (sin(A) / sin(B)) (assuming b, A, and B are known)
To find side b: b = a * (sin(B) / sin(A)) (assuming a, A, and B are known)
To find side c: c = a * (sin(C) / sin(A)) (assuming a, A, and C are known)
Note: The trigonometric functions (sin, cos) in most programming languages expect angles in radians. Therefore, we must convert degrees to radians before applying these functions.
Use Cases
Surveying and Navigation: Determining distances and positions.
Engineering and Architecture: Designing structures and components that involve triangular elements.
Physics: Analyzing forces and vectors.
Computer Graphics: Modeling and rendering 3D objects.
Everyday Problem Solving: Estimating distances or dimensions in practical situations.
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}
function calculateTriangleSide() {
var sideA = parseFloat(document.getElementById('sideA').value);
var sideB = parseFloat(document.getElementById('sideB').value);
var angleCdegrees = parseFloat(document.getElementById('angleCdegrees').value);
var angleAdegrees = parseFloat(document.getElementById('angleAdegrees').value);
var angleBdegrees = parseFloat(document.getElementById('angleBdegrees').value);
var calculateFor = document.getElementById('calculateFor').value;
var resultValue = document.getElementById('resultValue');
var resultUnit = document.getElementById('resultUnit');
var errorMessage = document.getElementById('errorMessage');
resultValue.textContent = '–';
resultUnit.textContent = 'units';
errorMessage.textContent = ";
var calculatedSide = null;
var unit = 'units';
// Basic validation for numeric inputs
if (isNaN(sideA) && calculateFor !== 'sideA' && calculateFor !== 'sideA_sine') sideA = 0;
if (isNaN(sideB) && calculateFor !== 'sideB' && calculateFor !== 'sideB_sine') sideB = 0;
if (isNaN(angleCdegrees) && calculateFor === 'sideC') angleCdegrees = 0;
if (isNaN(angleAdegrees) && (calculateFor === 'sideA' || calculateFor === 'sideA_sine' || calculateFor === 'sideC_sine')) angleAdegrees = 0;
if (isNaN(angleBdegrees) && (calculateFor === 'sideB' || calculateFor === 'sideB_sine')) angleBdegrees = 0;
// Check if essential values are present for the selected calculation
if (calculateFor === 'sideC' && (isNaN(sideA) || isNaN(sideB) || isNaN(angleCdegrees))) {
errorMessage.textContent = 'For Law of Cosines (Side C), please provide Side A, Side B, and Angle C.';
return;
}
if (calculateFor === 'sideA' && (isNaN(sideB) || isNaN(angleAdegrees) || (isNaN(angleBdegrees) && calculateFor === 'sideA'))) { // Note: angleBdegrees is needed if calculating sideA using cosine law and all other values are known
errorMessage.textContent = 'For Law of Cosines (Side A), please provide Side B, Side C (if known), and Angle A. If you have Side B, Angle A, and Angle B, consider using the Law of Sines option.';
return;
}
if (calculateFor === 'sideB' && (isNaN(sideA) || isNaN(angleBdegrees) || (isNaN(angleAdegrees) && calculateFor === 'sideB'))) { // Note: angleAdegrees is needed if calculating sideB using cosine law and all other values are known
errorMessage.textContent = 'For Law of Cosines (Side B), please provide Side A, Side C (if known), and Angle B. If you have Side A, Angle A, and Angle B, consider using the Law of Sines option.';
return;
}
if (calculateFor === 'sideA_sine' && (isNaN(sideB) || isNaN(angleAdegrees) || isNaN(angleBdegrees))) {
errorMessage.textContent = 'For Law of Sines (Side A), please provide Side B, Angle A, and Angle B.';
return;
}
if (calculateFor === 'sideB_sine' && (isNaN(sideA) || isNaN(angleAdegrees) || isNaN(angleBdegrees))) {
errorMessage.textContent = 'For Law of Sines (Side B), please provide Side A, Angle A, and Angle B.';
return;
}
if (calculateFor === 'sideC_sine' && (isNaN(sideA) || isNaN(angleAdegrees) || isNaN(angleCdegrees))) {
errorMessage.textContent = 'For Law of Sines (Side C), please provide Side A, Angle A, and Angle C.';
return;
}
try {
if (calculateFor === 'sideC') {
// Law of Cosines for Side C
var angleCRad = degreesToRadians(angleCdegrees);
var cSquared = Math.pow(sideA, 2) + Math.pow(sideB, 2) – 2 * sideA * sideB * Math.cos(angleCRad);
if (cSquared < 0) throw new Error("Cannot calculate side C. Check input values (e.g., resulting c^2 is negative).");
calculatedSide = Math.sqrt(cSquared);
} else if (calculateFor === 'sideA') {
// Law of Cosines for Side A
// Needs sideB, sideC, and angleA. If sideC is not given, we need angleB and angleC.
// For simplicity, we'll assume sideB, angleA and angleB are given for this case, and calculate sideC first to infer angleC if needed.
// A more robust calculator would handle all SSA/SAS combinations.
// Let's implement based on the assumption that if sideA is selected, sideB, angleA, and angleB are available for Law of Sines.
// If Side B, Side C, and Angle A are given:
if (!isNaN(sideB) && !isNaN(angleAdegrees)) { // Assuming Side B and Angle A are primary inputs for finding Side A via Law of Cosines if Side C is NOT given
errorMessage.textContent = "For Law of Cosines calculating Side A, you typically need Side B, Side C, and Angle A. If you have Side B, Angle A, and Angle B, use the Law of Sines option.";
return;
}
// If Side B, Side C and Angle A are available:
var sideC = parseFloat(document.getElementById('sideC').value); // Assuming a hidden or added input for sideC if needed
if (!isNaN(sideC)) {
var angleARad = degreesToRadians(angleAdegrees);
var aSquared = Math.pow(sideB, 2) + Math.pow(sideC, 2) – 2 * sideB * sideC * Math.cos(angleARad);
if (aSquared < 0) throw new Error("Cannot calculate side A. Check input values.");
calculatedSide = Math.sqrt(aSquared);
} else {
errorMessage.textContent = "To calculate Side A using Law of Cosines, please provide Side B, Side C, and Angle A.";
return;
}
} else if (calculateFor === 'sideB') {
// Law of Cosines for Side B
// Similar to Side A, needs sideA, sideC, angleB.
var sideC = parseFloat(document.getElementById('sideC').value); // Assuming a hidden or added input for sideC if needed
if (!isNaN(sideC)) {
var angleBRad = degreesToRadians(angleBdegrees);
var bSquared = Math.pow(sideA, 2) + Math.pow(sideC, 2) – 2 * sideA * sideC * Math.cos(angleBRad);
if (bSquared < 0) throw new Error("Cannot calculate side B. Check input values.");
calculatedSide = Math.sqrt(bSquared);
} else {
errorMessage.textContent = "To calculate Side B using Law of Cosines, please provide Side A, Side C, and Angle B.";
return;
}
}
else if (calculateFor === 'sideA_sine') {
// Law of Sines for Side A
var angleARad = degreesToRadians(angleAdegrees);
var angleBRad = degreesToRadians(angleBdegrees);
if (Math.sin(angleBRad) === 0) throw new Error("Cannot divide by sin(B) when Angle B is 0 or 180 degrees.");
calculatedSide = sideB * (Math.sin(angleARad) / Math.sin(angleBRad));
} else if (calculateFor === 'sideB_sine') {
// Law of Sines for Side B
var angleARad = degreesToRadians(angleAdegrees);
var angleBRad = degreesToRadians(angleBdegrees);
if (Math.sin(angleARad) === 0) throw new Error("Cannot divide by sin(A) when Angle A is 0 or 180 degrees.");
calculatedSide = sideA * (Math.sin(angleBRad) / Math.sin(angleARad));
} else if (calculateFor === 'sideC_sine') {
// Law of Sines for Side C
// Need to find Angle C first if not given directly.
// Sum of angles in a triangle is 180 degrees.
var angleCdegrees_calculated = 180 – angleAdegrees – angleBdegrees;
if (isNaN(angleCdegrees_calculated) || angleCdegrees_calculated = 0) {
resultValue.textContent = calculatedSide.toFixed(4); // Display with 4 decimal places
resultUnit.textContent = unit;
} else {
errorMessage.textContent = "Calculation resulted in an invalid value. Please check your inputs. Ensure angles sum up correctly if derivable.";
}
} catch (error) {
errorMessage.textContent = "Error: " + error.message;
}
}