-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,36 @@ | ||
// Copyright (c) 2023 Xu Shaohua <shaohua@biofan.org>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
//! Finding the intensity of light transmitted through a polariser using Malus Law. | ||
//! | ||
//! [Malus's law](https://en.wikipedia.org/wiki/Polarizer#Malus's_law_and_other_properties) | ||
|
||
#[must_use] | ||
pub fn malus_law(initial_intensity: f64, angle: f64) -> f64 { | ||
debug_assert!(initial_intensity >= 0.0); | ||
debug_assert!((0.0..=360.0).contains(&angle)); | ||
initial_intensity * (angle.to_radians().cos()).powi(2) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::malus_law; | ||
|
||
#[test] | ||
fn test_malus_law() { | ||
const PAIRS: &[(f64, f64, f64)] = &[ | ||
(10.0, 45.0, 5.0), | ||
(100.0, 60.0, 25.0), | ||
(50.0, 150.0, 37.5), | ||
(75.0, 270.0, 0.0), | ||
(100.0, 180.0, 100.0), | ||
(100.0, 360.0, 100.0), | ||
]; | ||
for (initial_intensity, angle, result) in PAIRS { | ||
let intensity = malus_law(*initial_intensity, *angle); | ||
let round_intensity = (intensity * 10.0).round() / 10.0; | ||
assert_eq!(round_intensity, *result); | ||
} | ||
} | ||
} |