mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-22 16:06:38 +00:00
Better slice implementation
This commit is contained in:
parent
3318db9a26
commit
c80dac4c4e
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user