lj_rust/src/transformer/rotate.rs

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