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
41
src/test/java/com/thealgorithms/physics/SnellLawTest.java
Normal file
41
src/test/java/com/thealgorithms/physics/SnellLawTest.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.thealgorithms.physics;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SnellLawTest {
|
||||
|
||||
@Test
|
||||
public void testRefractedAngle() {
|
||||
double n1 = 1.0; // air
|
||||
double n2 = 1.5; // glass
|
||||
double theta1 = Math.toRadians(30);
|
||||
|
||||
double theta2 = SnellLaw.refractedAngle(n1, n2, theta1);
|
||||
|
||||
double expected = Math.asin(n1 / n2 * Math.sin(theta1));
|
||||
|
||||
assertEquals(expected, theta2, 1e-12);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTotalInternalReflection() {
|
||||
double n1 = 1.5;
|
||||
double n2 = 1.0;
|
||||
double theta1 = Math.toRadians(60); // large angle
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> SnellLaw.refractedAngle(n1, n2, theta1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoTotalInternalReflectionAtLowAngles() {
|
||||
double n1 = 1.5;
|
||||
double n2 = 1.0;
|
||||
double theta1 = Math.toRadians(10);
|
||||
|
||||
assertDoesNotThrow(() -> SnellLaw.refractedAngle(n1, n2, theta1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user