mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Add Snell's Law refraction algorithm (#7150)
* Add Snell's Law implementation for refraction angle * Add tests for Snell's Law calculations * Update documentation with reference link to Snell's Law Added a reference link to Snell's Law in the documentation. * Prevent instantiation of SnellLaw class Make SnellLaw class non-instantiable by adding a private constructor. * Rename SnellsLawTest to SnellLawTest * Refactor SnellLawTest for clarity and accuracy * Rename SnellsLaw.java to SnellLaw.java * Refactor SnellLawTest with additional assertions * Refactor SnellLaw class constructor and error handling Refactor SnellLaw constructor and error message formatting. * Fix missing newline at end of SnellLawTest.java Ensure that the SnellLawTest class has a newline at the end of the file. * Simplify assertions in SnellLawTest * Simplify exception throwing for total internal reflection
This commit is contained in:
committed by
GitHub
parent
246162e5bd
commit
316def4ea6
33
src/main/java/com/thealgorithms/physics/SnellLaw.java
Normal file
33
src/main/java/com/thealgorithms/physics/SnellLaw.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.thealgorithms.physics;
|
||||
|
||||
/**
|
||||
* Calculates refraction angle using Snell's Law:
|
||||
* n1 * sin(theta1) = n2 * sin(theta2)
|
||||
* @see <a href="https://en.wikipedia.org/wiki/Snell%27s_law">Snell's Law</a>
|
||||
*/
|
||||
public final class SnellLaw {
|
||||
|
||||
private SnellLaw() {
|
||||
throw new AssertionError("No instances.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the refracted angle (theta2) in radians.
|
||||
*
|
||||
* @param n1 index of refraction of medium 1
|
||||
* @param n2 index of refraction of medium 2
|
||||
* @param theta1 incident angle in radians
|
||||
* @return refracted angle (theta2) in radians
|
||||
* @throws IllegalArgumentException if total internal reflection occurs
|
||||
*/
|
||||
public static double refractedAngle(double n1, double n2, double theta1) {
|
||||
double ratio = n1 / n2;
|
||||
double sinTheta2 = ratio * Math.sin(theta1);
|
||||
|
||||
if (Math.abs(sinTheta2) > 1.0) {
|
||||
throw new IllegalArgumentException("Total internal reflection: no refraction possible.");
|
||||
}
|
||||
|
||||
return Math.asin(sinTheta2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user