Add UniformSquare random distribution
This commit is contained in:
parent
c83416682e
commit
fac841295e
|
|
@ -8,6 +8,7 @@ pub mod integrators;
|
|||
pub mod materials;
|
||||
pub mod math;
|
||||
pub mod mesh;
|
||||
pub mod random_distributions;
|
||||
pub mod raycasting;
|
||||
pub mod realtype;
|
||||
pub mod sampler;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
mod uniform_square;
|
||||
pub use uniform_square::UniformSquare;
|
||||
|
||||
pub trait RandomDistribution<T> {
|
||||
fn value(&self) -> T;
|
||||
}
|
||||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue