Snell’s Law Calculator

Snell's Law Calculator

Snell's Law is a formula used to describe the relationship between the angles of incidence and refraction and the refractive indices of the two media when light passes through the boundary between them. The law is named after the Dutch mathematician Willebrord Snellius, who discovered it in 1621.

The formula for Snell's Law is:

n₁ * sin(θ₁) = n₂ * sin(θ₂)

Where:

  • n₁ is the refractive index of the first medium.
  • θ₁ is the angle of incidence (the angle between the incoming ray and the normal to the surface).
  • n₂ is the refractive index of the second medium.
  • θ₂ is the angle of refraction (the angle between the refracted ray and the normal to the surface).

In this calculator, you can input three of the four variables and calculate the fourth.

function calculateSnellsLaw() { var n1 = parseFloat(document.getElementById("n1").value); var theta1_deg = parseFloat(document.getElementById("theta1").value); var n2 = parseFloat(document.getElementById("n2").value); var theta2_deg = parseFloat(document.getElementById("theta2").value); var resultDiv = document.getElementById("result"); resultDiv.innerHTML = ""; // Clear previous results var calculatedCount = 0; var calculatedValue = ""; var missingValue = ""; if (isNaN(n1) || n1 <= 0) { missingValue = "n₁"; calculatedCount++; } if (isNaN(theta1_deg) || theta1_deg 90) { missingValue = "θ₁"; calculatedCount++; } if (isNaN(n2) || n2 <= 0) { missingValue = "n₂"; calculatedCount++; } if (isNaN(theta2_deg) || theta2_deg 90) { missingValue = "θ₂"; calculatedCount++; } if (calculatedCount > 1) { resultDiv.innerHTML = "Please provide values for three out of the four variables (n₁, θ₁, n₂, θ₂)."; return; } if (!isNaN(n1) && !isNaN(theta1_deg) && !isNaN(n2) && isNaN(theta2_deg)) { // Calculate theta2 var theta1_rad = theta1_deg * Math.PI / 180; var sin_theta1 = Math.sin(theta1_rad); var sin_theta2 = (n1 * sin_theta1) / n2; if (sin_theta2 > 1 || sin_theta2 < -1) { resultDiv.innerHTML = "Calculation results in Total Internal Reflection. The angle of refraction cannot be determined for these inputs."; return; } var theta2_rad = Math.asin(sin_theta2); var theta2_result_deg = theta2_rad * 180 / Math.PI; calculatedValue = theta2_result_deg.toFixed(2); missingValue = "θ₂"; resultDiv.innerHTML = "Calculated " + missingValue + ": " + calculatedValue + " degrees"; } else if (!isNaN(n1) && !isNaN(theta1_deg) && isNaN(n2) && !isNaN(theta2_deg)) { // Calculate n2 var theta1_rad = theta1_deg * Math.PI / 180; var theta2_rad = theta2_deg * Math.PI / 180; var sin_theta1 = Math.sin(theta1_rad); var sin_theta2 = Math.sin(theta2_rad); if (sin_theta2 === 0) { resultDiv.innerHTML = "Cannot calculate n₂ when θ₂ is 0 degrees, as it would imply n₁ is also 0 or θ₁ is 0."; return; } var n2_result = (n1 * sin_theta1) / sin_theta2; calculatedValue = n2_result.toFixed(3); missingValue = "n₂"; resultDiv.innerHTML = "Calculated " + missingValue + ": " + calculatedValue + ""; } else if (!isNaN(n1) && isNaN(theta1_deg) && !isNaN(n2) && !isNaN(theta2_deg)) { // Calculate theta1 var theta2_rad = theta2_deg * Math.PI / 180; var sin_theta2 = Math.sin(theta2_rad); var sin_theta1 = (n2 * sin_theta2) / n1; if (sin_theta1 > 1 || sin_theta1 < -1) { resultDiv.innerHTML = "Input values result in an impossible scenario for θ₁. The sine of an angle cannot be greater than 1 or less than -1."; return; } var theta1_rad = Math.asin(sin_theta1); var theta1_result_deg = theta1_rad * 180 / Math.PI; calculatedValue = theta1_result_deg.toFixed(2); missingValue = "θ₁"; resultDiv.innerHTML = "Calculated " + missingValue + ": " + calculatedValue + " degrees"; } else if (isNaN(n1) && !isNaN(theta1_deg) && !isNaN(n2) && !isNaN(theta2_deg)) { // Calculate n1 var theta1_rad = theta1_deg * Math.PI / 180; var theta2_rad = theta2_deg * Math.PI / 180; var sin_theta1 = Math.sin(theta1_rad); var sin_theta2 = Math.sin(theta2_rad); if (sin_theta1 === 0) { resultDiv.innerHTML = "Cannot calculate n₁ when θ₁ is 0 degrees, as it would imply n₂ is also 0 or θ₂ is 0."; return; } var n1_result = (n2 * sin_theta2) / sin_theta1; calculatedValue = n1_result.toFixed(3); missingValue = "n₁"; resultDiv.innerHTML = "Calculated " + missingValue + ": " + calculatedValue + ""; } else { resultDiv.innerHTML = "Please ensure you have entered valid numbers for three of the four fields."; } } .snells-law-calculator { font-family: sans-serif; padding: 20px; border: 1px solid #ddd; border-radius: 8px; max-width: 600px; margin: 20px auto; background-color: #f9f9f9; } .snells-law-calculator h2 { text-align: center; color: #333; margin-bottom: 15px; } .snells-law-calculator p { color: #555; line-height: 1.6; margin-bottom: 10px; } .snells-law-calculator ul { margin-left: 20px; margin-bottom: 15px; color: #555; } .snells-law-calculator li { margin-bottom: 5px; } .snells-law-calculator strong { color: #333; } .snells-law-calculator .input-section { display: grid; grid-template-columns: auto 1fr; gap: 10px; margin-bottom: 20px; align-items: center; } .snells-law-calculator label { font-weight: bold; color: #333; text-align: right; padding-right: 10px; } .snells-law-calculator input[type="number"] { padding: 8px; border: 1px solid #ccc; border-radius: 4px; width: 100%; box-sizing: border-box; } .snells-law-calculator button { display: block; width: 100%; padding: 10px 15px; background-color: #007bff; color: white; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; margin-top: 10px; } .snells-law-calculator button:hover { background-color: #0056b3; } .snells-law-calculator .result-section { margin-top: 20px; padding: 15px; background-color: #e9ecef; border: 1px solid #ced4da; border-radius: 5px; text-align: center; font-size: 1.1em; color: #333; } .snells-law-calculator .result-section strong { color: #28a745; /* Green for results */ }

Leave a Reply

Your email address will not be published. Required fields are marked *