rename modifier to transformer

This commit is contained in:
Marc Planard 2023-06-05 11:21:16 +02:00
parent 9844475fa1
commit 12ceb6f69b
2 changed files with 2 additions and 2 deletions

59
src/transformer.rs Normal file
View file

@ -0,0 +1,59 @@
use crate::point::Point;
pub trait Transformers {
fn apply(&self, point_list: &Vec<Point>) -> Vec<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: &Vec<Point>) -> Vec<Point> {
point_list.iter()
.map(| pt | {
Point { x: pt.x + self.x,
y: pt.y + self.y,
..*pt
}
}).collect()
}
}
/// Replicate
#[derive(Debug,Clone,Copy)]
pub enum Replicate {
Until(usize),
Times(usize)
}
impl Transformers for Replicate {
fn apply(&self, point_list: &Vec<Point>) -> Vec<Point> {
let mut point_list2 = vec![];
match self {
Replicate::Until(n) => {
while point_list2.len() < *n {
point_list2.append(&mut point_list.clone());
}
},
Replicate::Times(n) => {
for _ in 0..*n {
point_list2.append(&mut point_list.clone());
}
}
}
point_list2
}
}