From e899d8061c837064a50abceb9fe28a73e326de09 Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Tue, 6 Jun 2023 11:31:45 +0200 Subject: [PATCH] refacto transformer with module hierarchy --- src/transformer/replicate.rs | 31 +++++++++++++++++++++++++++++++ src/transformer/translate.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/transformer/replicate.rs create mode 100644 src/transformer/translate.rs diff --git a/src/transformer/replicate.rs b/src/transformer/replicate.rs new file mode 100644 index 0000000..bb56fec --- /dev/null +++ b/src/transformer/replicate.rs @@ -0,0 +1,31 @@ +use crate::Transformers; +use crate::point::Point; + +/// Replicate + +#[allow(dead_code)] +#[derive(Debug,Clone,Copy)] +pub enum Replicate { + Until(usize), + Times(usize) +} + +impl Transformers for Replicate { + fn apply(&self, point_list: &[Point]) -> Vec { + let mut point_list2 = vec![]; + match self { + Replicate::Until(n) => { + while point_list2.len() < *n { + point_list2.append(&mut point_list.to_vec()); + } + }, + Replicate::Times(n) => { + for _ in 0..*n { + point_list2.append(&mut point_list.to_vec()); + } + } + } + point_list2 + } +} + diff --git a/src/transformer/translate.rs b/src/transformer/translate.rs new file mode 100644 index 0000000..6b0c029 --- /dev/null +++ b/src/transformer/translate.rs @@ -0,0 +1,27 @@ +use crate::Transformers; +use crate::point::Point; +/// Translate + +#[derive(Debug,Clone,Copy)] +pub struct Translate { + x: f32, + y: f32 +} + +impl Translate { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +impl Transformers for Translate { + fn apply(&self, point_list: &[Point]) -> Vec { + point_list.iter() + .map(| pt | { + Point { x: pt.x + self.x, + y: pt.y + self.y, + ..*pt + } + }).collect() + } +}