mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-22 07:56:38 +00:00
Better slice implementation
This commit is contained in:
parent
3318db9a26
commit
c80dac4c4e
@ -1,26 +1,36 @@
|
|||||||
use crate::pattern::*;
|
use crate::pattern::*;
|
||||||
use crate::Vector;
|
use crate::Vector;
|
||||||
|
|
||||||
#[derive(derive_more::Constructor)]
|
|
||||||
pub struct SliceFactory {
|
pub struct SliceFactory {
|
||||||
child: Box<dyn PatternFactory>,
|
child: Box<dyn PatternFactory>,
|
||||||
scale: u8,
|
width: f32,
|
||||||
|
rest: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(derive_more::Constructor)]
|
#[derive(derive_more::Constructor)]
|
||||||
pub struct Slice {
|
pub struct Slice {
|
||||||
child: Box<dyn Pattern>,
|
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 {
|
impl PatternFactory for SliceFactory {
|
||||||
fn create(&self, config: &Config) -> Box<dyn Pattern> {
|
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 {
|
impl Pattern for Slice {
|
||||||
fn sample(&self, pos: Vector) -> f32 {
|
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