Skip to content

Commit

Permalink
physics: Add malus law
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 28, 2023
1 parent acaec83 commit b5743c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)]

pub mod kinetic_energy;
pub mod malus_law;
pub mod potential_energy;
pub mod shear_stress;
pub mod speed_of_sound;
36 changes: 36 additions & 0 deletions physics/src/malus_law.rs
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);
}
}
}

0 comments on commit b5743c8

Please sign in to comment.