feat: add Snell's Law refraction angle calculation#14623
feat: add Snell's Law refraction angle calculation#14623soradacroi wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
| incident_angle_radians | ||
| ) | ||
|
|
||
| if refraction_sine > 1: |
There was a problem hiding this comment.
Good check for Total Internal Reflection — this correctly catches
cases where the sine value exceeds 1 which is physically impossible.
However the check on line 57 (refraction_sine < -1) will never
be reached in practice since a negative refractive index ratio
combined with a valid incident angle would need very specific
conditions. Worth adding a comment explaining when this case
actually occurs, or combining both checks:
if not -1 <= refraction_sine <= 1:
raise ValueError("Invalid physical inputs: ratio cannot be less than -1.")
|
|
||
| # If the sine value is approximately 1.0, it's at the critical angle | ||
| # We use math.isclose to account for floating-point precision errors | ||
| if math.isclose(refraction_sine, 1.0): |
There was a problem hiding this comment.
Smart use of math.isclose() here to handle floating-point precision
errors at the critical angle boundary — good defensive programming.
Suggestion: consider also handling the case where refraction_sine
is close to -1.0 symmetrically, since the current code raises a
ValueError for values slightly below -1 due to floating-point
errors, which could be confusing to callers passing valid inputs.
| @@ -0,0 +1,66 @@ | |||
| import math | |||
There was a problem hiding this comment.
The file is missing a module-level docstring. The repo's convention
is to have a module docstring before imports. Consider adding:
"""
Snell's Law — Refraction Angle Calculation.
Calculates the angle of refraction when light passes between two
media using Snell's Law: n1 * sin(theta1) = n2 * sin(theta2).
Reference: https://en.wikipedia.org/wiki/Snell%27s_law
"""
Describe your change:
Added snells_law.py to the physics/ directory.
This implements the calculate_refraction_angle function,
which finds the refraction angle based on Snell's Law.
The implementation handles standard refraction, the critical angle (90°), Total Internal Reflection (TIR)
and Validation logic to catch physically impossible inputs (refractive index ratios less than -1).
Checklist: