-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
feat: add Snell's Law refraction angle calculation #14623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
soradacroi
wants to merge
1
commit into
TheAlgorithms:master
Choose a base branch
from
soradacroi:snells_law
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+75
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """ | ||
| 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 | ||
| """ | ||
|
|
||
| import math | ||
|
|
||
|
|
||
| def calculate_refraction_angle( | ||
| index_of_refraction_1: float, | ||
| index_of_refraction_2: float, | ||
| incident_angle_degrees: float, | ||
| ) -> float: | ||
| """ | ||
| Calculates the refraction angle of light passing from one medium to another. | ||
| The law states that, for a given pair of media, the ratio of the sines | ||
| of angle of incidence and angle of refraction s equal to the refractive | ||
| index of the second medium with regard to the first which is equal to the | ||
| ratio of the refractive indices of the two media, or equivalently, to the | ||
| ratio of the phase velocities in the two media. | ||
|
|
||
|
|
||
| Formula: n1 * sin(theta1) = n2 * sin(theta2) | ||
| or : (sin(theta1) / sin(theta2)) = (n2 / n1) | ||
| Where: | ||
| n1 = refractive index of the first medium | ||
| n2 = refractive index of the second medium | ||
| theta1 = angle of incidence | ||
| theta2 = angle of refraction | ||
|
|
||
| Note: Total Internal Reflection (TIR) occurs when light travels from a | ||
| denser medium to a rarer medium and the incident angle exceeds the | ||
| critical angle, making refraction impossible. | ||
|
|
||
| Sources: | ||
| - https://en.wikipedia.org/wiki/Snell%27s_law | ||
|
|
||
| ----------------------------------------------------------------------------- | ||
|
|
||
| >>> calculate_refraction_angle(1.0, 1.33, 60.0) | ||
| 40.63 | ||
| >>> calculate_refraction_angle(2.0, 1.0, 30.0) | ||
| 90.0 | ||
| >>> calculate_refraction_angle(1.33, 1.0, 60.0) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Invalid physical inputs: ratio cannot be outside [-1, 1]. | ||
| >>> calculate_refraction_angle(-1.33, 1.0, 60.0) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Invalid physical inputs: ratio cannot be outside [-1, 1]. | ||
| >>> | ||
| """ | ||
|
|
||
| incident_angle_radians = math.radians(incident_angle_degrees) | ||
| refraction_sine = (index_of_refraction_1 / index_of_refraction_2) * math.sin( | ||
| incident_angle_radians | ||
| ) | ||
|
|
||
| # If the sine value is approximately 1.0 or -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): | ||
|
soradacroi marked this conversation as resolved.
|
||
| return 90.0 | ||
| if math.isclose(refraction_sine, -1.0): | ||
| return -90.0 | ||
|
|
||
| if not -1 <= refraction_sine <= 1: | ||
| raise ValueError("Invalid physical inputs: ratio cannot be outside [-1, 1].") | ||
|
|
||
| refraction_angle = round(math.degrees(math.asin(refraction_sine)), 2) | ||
| return refraction_angle | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.