From 08e78b9a8815abbf033b0f31e3f9d36207b753f6 Mon Sep 17 00:00:00 2001 From: Lapin Raving Date: Thu, 8 Jun 2023 22:34:03 +0200 Subject: [PATCH] feat: adding somme new transformation * Rotate to rotate the image, !! Some point could be outside of the 0-4096 range on x and y !! you should parameter a center and an angle. All the point will move around the center. The angle is un degre. * Flip vertical: flip all the point verticaly. You should parameter an height where all point will flip * Flip horizontaly. You should parameter an X corrodinate where all point will flip around. --- src/transformer.rs | 6 +++++ src/transformer/flip_horizontal.rs | 28 +++++++++++++++++++++ src/transformer/flip_vertical.rs | 28 +++++++++++++++++++++ src/transformer/rotate.rs | 39 ++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 src/transformer/flip_horizontal.rs create mode 100644 src/transformer/flip_vertical.rs create mode 100644 src/transformer/rotate.rs diff --git a/src/transformer.rs b/src/transformer.rs index 564b981..c31b947 100644 --- a/src/transformer.rs +++ b/src/transformer.rs @@ -1,12 +1,18 @@ //pub mod common; mod translate; mod replicate; +mod rotate; +mod flip_horizontal; +mod flip_vertical; use crate::point::Point; // re-export transformers to be abe to use it directly from transformer:: pub use translate::Translate; pub use replicate::Replicate; +pub use rotate::Rotate; +pub use flip_horizontal::FlipHorizontal; +pub use flip_vertical::FlipVertical; pub trait Transformers { fn apply(&self, point_list: &[Point]) -> Vec; diff --git a/src/transformer/flip_horizontal.rs b/src/transformer/flip_horizontal.rs new file mode 100644 index 0000000..2157ddd --- /dev/null +++ b/src/transformer/flip_horizontal.rs @@ -0,0 +1,28 @@ +use crate::transformer::Transformers; +use crate::point::Point; +use serde::{Serialize,Deserialize}; + +/// Flip Horizontal + +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] +pub struct FlipHorizontal { + x: f32, +} + +impl FlipHorizontal { + pub fn new(x: f32) -> Self { + Self {x} + } +} + +impl Transformers for FlipHorizontal { + fn apply(&self, point_list: &[Point]) -> Vec { + point_list.iter() + .map(| pt | { + let dx = pt.x - self.x; + Point { x: dx - 2. * dx, + ..*pt + } + }).collect() + } +} diff --git a/src/transformer/flip_vertical.rs b/src/transformer/flip_vertical.rs new file mode 100644 index 0000000..b4f6d59 --- /dev/null +++ b/src/transformer/flip_vertical.rs @@ -0,0 +1,28 @@ +use crate::transformer::Transformers; +use crate::point::Point; +use serde::{Serialize,Deserialize}; + +/// Flip Vertical + +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] +pub struct FlipVertical { + y: f32, +} + +impl FlipVertical { + pub fn new(y: f32) -> Self { + Self {y} + } +} + +impl Transformers for FlipVertical { + fn apply(&self, point_list: &[Point]) -> Vec { + point_list.iter() + .map(| pt | { + let dy = pt.y - self.y; + Point { y: dy - 2. * dy, + ..*pt + } + }).collect() + } +} diff --git a/src/transformer/rotate.rs b/src/transformer/rotate.rs new file mode 100644 index 0000000..ca574ba --- /dev/null +++ b/src/transformer/rotate.rs @@ -0,0 +1,39 @@ +use crate::transformer::Transformers; +use crate::point::Point; +use serde::{Serialize,Deserialize}; +use std::f32::consts::PI; + +/// Rotate + +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] +pub struct Rotate { + cx: f32, + cy: f32, + angle: f32, + +} + +impl Rotate { + pub fn new(cx: f32, cy: f32, angle: f32) -> Self { + Self { cx, cy, angle: angle / 180. * PI} + } +} + +impl Transformers for Rotate { + fn apply(&self, point_list: &[Point]) -> Vec { + point_list.iter() + .map(| pt | { + let dx = (pt.x - self.cx); + let dy = (pt.y - self.cy); + let cos = self.angle.cos(); + let sin = self.angle.sin(); + let x = (dx * cos - dy * sin) + self.cx; + let y = (dx * sin + dy * cos) + self.cy; + Point { x, + y, + ..*pt + } + }).collect() + } +} +