refacto transformer with module hierarchy
This commit is contained in:
parent
7d7eec6695
commit
e899d8061c
31
src/transformer/replicate.rs
Normal file
31
src/transformer/replicate.rs
Normal file
@ -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<Point> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
27
src/transformer/translate.rs
Normal file
27
src/transformer/translate.rs
Normal file
@ -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> {
|
||||
point_list.iter()
|
||||
.map(| pt | {
|
||||
Point { x: pt.x + self.x,
|
||||
y: pt.y + self.y,
|
||||
..*pt
|
||||
}
|
||||
}).collect()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user