Cosine Law Calculator

Cosine Law Calculator body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f8f9fa; color: #333; line-height: 1.6; margin: 0; padding: 20px; } .cosine-calc-container { max-width: 800px; margin: 30px auto; background-color: #ffffff; padding: 30px; border-radius: 8px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); display: flex; flex-wrap: wrap; gap: 30px; } .calculator-section { flex: 1; min-width: 300px; } h1, h2 { color: #004a99; text-align: center; margin-bottom: 20px; } .input-group { margin-bottom: 20px; display: flex; align-items: center; gap: 10px; } .input-group label { flex: 1; min-width: 120px; font-weight: 600; color: #555; } .input-group input[type="number"] { flex: 2; padding: 10px 15px; border: 1px solid #ccc; border-radius: 5px; font-size: 1rem; box-sizing: border-box; } .input-group input[type="number"]:focus { border-color: #004a99; outline: none; box-shadow: 0 0 0 3px rgba(0, 74, 153, 0.2); } button { display: block; width: 100%; padding: 12px 20px; background-color: #004a99; color: white; border: none; border-radius: 5px; font-size: 1.1rem; font-weight: 600; cursor: pointer; transition: background-color 0.3s ease, transform 0.2s ease; margin-top: 10px; } button:hover { background-color: #003b7a; transform: translateY(-2px); } button:active { transform: translateY(0); } #result { margin-top: 30px; padding: 20px; background-color: #e6f2ff; border: 1px solid #004a99; border-radius: 5px; text-align: center; font-size: 1.3rem; font-weight: bold; color: #004a99; } #result span { font-size: 1.5rem; color: #28a745; } .explanation { margin-top: 40px; padding-top: 30px; border-top: 1px solid #eee; } .explanation h2 { text-align: left; color: #004a99; margin-bottom: 15px; } .explanation p, .explanation ul { margin-bottom: 15px; } .explanation ul { padding-left: 25px; } .explanation code { background-color: #e6f2ff; padding: 2px 6px; border-radius: 3px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; } @media (max-width: 768px) { .cosine-calc-container { flex-direction: column; padding: 20px; } .input-group { flex-direction: column; align-items: stretch; } .input-group label { margin-bottom: 5px; min-width: auto; } .input-group input[type="number"] { width: 100%; } }

Cosine Law Calculator

Calculate unknown sides or angles in any triangle using the Cosine Law.

Result will appear here.

Understanding the Cosine Law

The Cosine Law (also known as the Law of Cosines) is a fundamental theorem in trigonometry that relates the lengths of the sides of any triangle to the cosine of one of its angles.

It's an extension of the Pythagorean theorem, which only applies to right-angled triangles. The Cosine Law works for any triangle, including acute and obtuse triangles.

The Formula:

For a triangle with sides of length a, b, and c, and angles opposite these sides being A, B, and C respectively:

  • To find a side (e.g., side c) when two sides (a, b) and the included angle (C) are known:
    c² = a² + b² - 2ab * cos(C)
  • To find an angle (e.g., angle C) when all three sides (a, b, c) are known:
    cos(C) = (a² + b² - c²) / (2ab)
    Then, C = arccos((a² + b² - c²) / (2ab))

How This Calculator Works:

This calculator uses the Cosine Law to solve for an unknown side or angle. You need to provide at least three of the four values (two sides and the included angle, or all three sides).

  • If you know a, b, and C, and want to find c, enter the known values and leave the input for Side c blank.
  • If you know a, b, and c, and want to find C, enter the known values for all three sides and leave the input for Angle C blank.

Important Note: Angles must be entered in degrees.

Use Cases:

  • Navigation: Calculating distances and bearings between points.
  • Surveying: Determining distances and angles on land.
  • Engineering: Designing structures and calculating forces.
  • Physics: Analyzing vectors and resolving forces.
  • Geometry: Solving problems involving non-right triangles.
function calculateCosineLaw() { var sideA = parseFloat(document.getElementById("sideA").value); var sideB = parseFloat(document.getElementById("sideB").value); var angleC_deg = parseFloat(document.getElementById("angleC").value); var sideC = parseFloat(document.getElementById("sideC").value); var resultDiv = document.getElementById("result"); resultDiv.innerHTML = "Result will appear here."; // Reset result var unknownValue = ""; var knownValuesCount = 0; if (!isNaN(sideA) && sideA > 0) knownValuesCount++; else sideA = null; if (!isNaN(sideB) && sideB > 0) knownValuesCount++; else sideB = null; if (!isNaN(angleC_deg) && angleC_deg > 0 && angleC_deg 0) knownValuesCount++; else sideC = null; // Determine which value is unknown if (sideA === null) unknownValue = "sideA"; if (sideB === null) unknownValue = "sideB"; if (angleC_deg === null) unknownValue = "angleC"; if (sideC === null) unknownValue = "sideC"; // Check for sufficient inputs if (knownValuesCount < 3) { resultDiv.innerHTML = "Error: Please provide at least 3 values."; return; } // Perform calculations based on the unknown value if (unknownValue === "sideC") { // Calculate side c using c² = a² + b² – 2ab * cos(C) if (sideA !== null && sideB !== null && angleC_deg !== null) { var angleC_rad = angleC_deg * Math.PI / 180; var sideCSquared = Math.pow(sideA, 2) + Math.pow(sideB, 2) – 2 * sideA * sideB * Math.cos(angleC_rad); if (sideCSquared < 0) { resultDiv.innerHTML = "Calculation Error: Invalid triangle input (result negative)."; } else { var calculatedSideC = Math.sqrt(sideCSquared); resultDiv.innerHTML = "Calculated Side c: " + calculatedSideC.toFixed(4) + ""; } } else { resultDiv.innerHTML = "Error: Missing inputs to calculate side c."; } } else if (unknownValue === "angleC") { // Calculate angle C using cos(C) = (a² + b² – c²) / (2ab) if (sideA !== null && sideB !== null && sideC !== null) { var numerator = Math.pow(sideA, 2) + Math.pow(sideB, 2) – Math.pow(sideC, 2); var denominator = 2 * sideA * sideB; if (denominator === 0) { resultDiv.innerHTML = "Calculation Error: Division by zero (sides a or b cannot be zero)."; return; } var cosC = numerator / denominator; // Clamp cosC value to the range [-1, 1] due to potential floating point inaccuracies if (cosC > 1) cosC = 1; if (cosC < -1) cosC = -1; if (isNaN(cosC)) { resultDiv.innerHTML = "Calculation Error: Invalid triangle inequality (e.g., sides too short)."; } else { var angleC_rad = Math.acos(cosC); var calculatedAngleC_deg = angleC_rad * 180 / Math.PI; resultDiv.innerHTML = "Calculated Angle C: " + calculatedAngleC_deg.toFixed(4) + "°"; } } else { resultDiv.innerHTML = "Error: Missing inputs to calculate angle C."; } } else { // Handle cases where the user might have entered all values or ambiguous inputs resultDiv.innerHTML = "Please leave one field blank to calculate the unknown."; } }

Leave a Comment