From 852d3e5bf535205e993b3bce26079494373baee7 Mon Sep 17 00:00:00 2001 From: Rico Riedel Date: Thu, 4 Aug 2022 16:24:31 +0200 Subject: [PATCH] Add swap transformation --- src/main.rs | 22 ++++++++++++++++++++-- src/transform/mod.rs | 2 ++ src/transform/swap.rs | 27 +++++++++++++++++++++++++++ src/vec.rs | 9 +++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/transform/swap.rs diff --git a/src/main.rs b/src/main.rs index 4548403..394bf61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,8 @@ struct Args { char_pattern: Vec, #[clap(long)] char_invert: Option, + #[clap(long)] + char_swap: Option, #[clap(long, value_enum)] colors: Vec, #[clap(long, value_enum)] @@ -51,6 +53,8 @@ struct Args { color_shift: Option, #[clap(long)] color_invert: Option, + #[clap(long)] + color_swap: Option, } #[derive(ValueEnum, Clone)] @@ -94,15 +98,26 @@ struct PatternConfig<'a> { patterns: &'a Vec, shift: Option, invert: Option, + swap: Option, } impl Args { fn char_config(&self) -> PatternConfig { - PatternConfig::new(&self.char_pattern, Some(true), self.char_invert) + PatternConfig::new( + &self.char_pattern, + Some(true), + self.char_invert, + self.char_swap, + ) } fn color_config(&self) -> PatternConfig { - PatternConfig::new(&self.color_pattern, self.color_shift, self.color_invert) + PatternConfig::new( + &self.color_pattern, + self.color_shift, + self.color_invert, + self.color_swap, + ) } fn pallet(&self, rand: &mut impl Rng) -> Vec { @@ -161,6 +176,9 @@ impl<'a> PatternConfig<'a> { if self.invert.unwrap_or(rand.gen()) { pattern = Box::new(InvertFactory::new(pattern)) } + if self.swap.unwrap_or(rand.gen()) { + pattern = Box::new(SwapFactory::new(pattern)) + } pattern } } diff --git a/src/transform/mod.rs b/src/transform/mod.rs index fc89bb0..22d00b2 100644 --- a/src/transform/mod.rs +++ b/src/transform/mod.rs @@ -1,5 +1,7 @@ mod invert; mod shift; +mod swap; pub use invert::*; pub use shift::*; +pub use swap::*; diff --git a/src/transform/swap.rs b/src/transform/swap.rs new file mode 100644 index 0000000..9525a80 --- /dev/null +++ b/src/transform/swap.rs @@ -0,0 +1,27 @@ +use crate::pattern::*; +use crate::Vector; + +#[derive(derive_more::Constructor)] +pub struct SwapFactory { + child: Box, +} + +#[derive(derive_more::Constructor)] +pub struct Swap { + child: Box, +} + +impl PatternFactory for SwapFactory { + fn create(&self, config: &Config) -> Box { + let mut copy = config.clone(); + copy.size = config.size.swap(); + + Box::new(Swap::new(self.child.create(©))) + } +} + +impl Pattern for Swap { + fn sample(&self, pos: Vector) -> f32 { + self.child.sample(pos.swap()) + } +} diff --git a/src/vec.rs b/src/vec.rs index 60104bf..a1ff420 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -32,6 +32,10 @@ impl Vector { pub fn angle(&self) -> f32 { self.y.atan2(self.x) } + + pub fn swap(&self) -> Vector { + Self::new(self.y, self.x) + } } #[cfg(test)] @@ -70,4 +74,9 @@ mod test { fn angle() { assert_abs_diff_eq!(-1.5, Vector::new(2.0, -20.0).angle(), epsilon = 0.1); } + + #[test] + fn swap() { + assert_eq!(Vector::new(7.0, 2.0), Vector::new(2.0, 7.0).swap()); + } }