Better slice implementation

This commit is contained in:
Rico Riedel 2022-08-04 17:37:25 +02:00
parent 3318db9a26
commit c80dac4c4e
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18

View File

@ -1,26 +1,36 @@
use crate::pattern::*;
use crate::Vector;
#[derive(derive_more::Constructor)]
pub struct SliceFactory {
child: Box<dyn PatternFactory>,
scale: u8,
width: f32,
rest: f32,
}
#[derive(derive_more::Constructor)]
pub struct Slice {
child: Box<dyn Pattern>,
scale: u8,
width: f32,
rest: f32,
}
impl SliceFactory {
pub fn new(child: Box<dyn PatternFactory>, slices: u8) -> Self {
let width = 1.0 / slices as f32;
let rest = 1.0 - width;
Self { child, width, rest }
}
}
impl PatternFactory for SliceFactory {
fn create(&self, config: &Config) -> Box<dyn Pattern> {
Box::new(Slice::new(self.child.create(config), self.scale))
Box::new(Slice::new(self.child.create(config), self.width, self.rest))
}
}
impl Pattern for Slice {
fn sample(&self, pos: Vector) -> f32 {
self.child.sample(pos) * self.scale as f32
(self.child.sample(pos) - self.rest) / self.width
}
}