From c7039f0fc2d49dd0501db8f201e66a915d189ac1 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 14 Mar 2021 12:35:34 +0530 Subject: add symmetry options --- src/bitmap.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'src/bitmap.rs') diff --git a/src/bitmap.rs b/src/bitmap.rs index 8ff311b..726e0a2 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -1,6 +1,6 @@ use std::{ convert::{From, Into, TryFrom}, - ops::Sub, + ops::{Add, Sub}, }; #[derive(Debug)] @@ -50,6 +50,56 @@ impl TryFrom<(i64, i64)> for MapPoint { } } +impl Add for MapPoint { + type Output = Self; + fn add(self, rhs: Self) -> Self::Output { + MapPoint { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} + +impl Sub for MapPoint { + type Output = Self; + fn sub(self, rhs: Self) -> Self::Output { + MapPoint { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} + +#[derive(Debug, Copy, Clone)] +pub enum Axis { + X, + Y, +} + +impl MapPoint { + #[inline] + pub fn scale(self, c: u32) -> MapPoint { + MapPoint { + x: self.x * c, + y: self.y * c, + } + } + + #[inline] + pub fn mirror_about(self, line: u32, axis: Axis) -> MapPoint { + let MapPoint { x, y } = self; + match axis { + Axis::X => MapPoint { x, y: 2 * line - y }, + Axis::Y => MapPoint { x: 2 * line - x, y }, + } + } + + #[inline] + pub fn reflect(self, around: MapPoint) -> MapPoint { + around.scale(2) - self + } +} + impl Pixmap where T: Copy + Clone + Default, @@ -179,6 +229,22 @@ where .filter(|&pt| self.is_inside(pt)) .collect() } + + pub fn mirror_figure(&self, figure: &[MapPoint], line: u32, axis: Axis) -> Vec { + figure + .iter() + .map(|pt| pt.mirror_about(line, axis)) + .filter(|&pt| self.is_inside(pt)) + .collect() + } + + pub fn reflect_figure(&self, figure: &[MapPoint], around: MapPoint) -> Vec { + figure + .iter() + .map(|pt| pt.reflect(around)) + .filter(|&pt| self.is_inside(pt)) + .collect() + } } fn abs_difference + Ord>(x: T, y: T) -> T { -- cgit v1.2.3