lj_rust/src/transformer/rotate.rs
Lapin Raving 08e78b9a88 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.
2023-06-08 22:34:03 +02:00

40 lines
888 B
Rust

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> {
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()
}
}