Skip to content

Commit 19aa44a

Browse files
authored
Add Snell's Law implementation for refraction angle
1 parent bd7f269 commit 19aa44a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.physics;
2+
3+
/**
4+
* Calculates refraction angle using Snell's Law:
5+
* n1 * sin(theta1) = n2 * sin(theta2)
6+
*/
7+
public class SnellLaw {
8+
9+
/**
10+
* Computes the refracted angle (theta2) in radians.
11+
*
12+
* @param n1 index of refraction of medium 1
13+
* @param n2 index of refraction of medium 2
14+
* @param theta1 incident angle in radians
15+
* @return refracted angle (theta2) in radians
16+
* @throws IllegalArgumentException if total internal reflection occurs
17+
*/
18+
public static double refractedAngle(double n1, double n2, double theta1) {
19+
double ratio = n1 / n2;
20+
double sinTheta2 = ratio * Math.sin(theta1);
21+
22+
if (Math.abs(sinTheta2) > 1.0) {
23+
throw new IllegalArgumentException("Total internal reflection: no refraction possible.");
24+
}
25+
26+
return Math.asin(sinTheta2);
27+
}
28+
}

0 commit comments

Comments
 (0)