Add UniformSquare random distribution

This commit is contained in:
Matthew Gordon 2021-10-03 09:59:06 -04:00
parent c83416682e
commit fac841295e
3 changed files with 44 additions and 0 deletions

View File

@ -8,6 +8,7 @@ pub mod integrators;
pub mod materials; pub mod materials;
pub mod math; pub mod math;
pub mod mesh; pub mod mesh;
pub mod random_distributions;
pub mod raycasting; pub mod raycasting;
pub mod realtype; pub mod realtype;
pub mod sampler; pub mod sampler;

View File

@ -0,0 +1,6 @@
mod uniform_square;
pub use uniform_square::UniformSquare;
pub trait RandomDistribution<T> {
fn value(&self) -> T;
}

View File

@ -0,0 +1,37 @@
use rand::distributions::Open01;
use rand::{thread_rng, Rng};
use crate::math::Vec2;
use super::RandomDistribution;
#[derive(Debug)]
pub struct UniformSquare {
corner: Vec2,
size: f64,
}
impl RandomDistribution<Vec2> for UniformSquare {
fn value(&self) -> Vec2 {
let mut rng = thread_rng();
self.corner
+ Vec2::new(rng.sample::<f64, _>(Open01), rng.sample::<f64, _>(Open01)) * self.size
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn print_values() {
let target = UniformSquare {
corner: Vec2::new(1.5, -2.5),
size: 3.0,
};
for _ in 0..1000 {
let value = target.value();
println!("{}, {}", value.x(), value.y());
}
}
}