From f6cff545d27d5939a1b651004458e97deaf900de Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 15 Mar 2021 10:55:18 +0530 Subject: refactor get_circle to draw filled & outline circles --- src/bitmap.rs | 50 +++++++++++++++++++++++--------------------------- src/symmetry.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 src/symmetry.rs (limited to 'src') diff --git a/src/bitmap.rs b/src/bitmap.rs index 726e0a2..128a14e 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -145,7 +145,12 @@ where old_val } - pub fn get_circle>(&self, center: P, radius: u32) -> Vec { + pub fn get_circle>( + &self, + center: P, + radius: u32, + filled: bool, + ) -> Vec { let mut circle: Vec<(i64, i64)> = vec![]; let MapPoint { x, y } = center.into(); let x = x as i64; @@ -160,6 +165,12 @@ where circle.push((x - dy, y + dx)); circle.push((x + dy, y - dx)); circle.push((x - dy, y - dx)); + if filled { + circle.extend((x - dx.abs() + 1..x + dx.abs()).map(|x| (x, y + dy))); + circle.extend((x - dx.abs() + 1..x + dx.abs()).map(|x| (x, y - dy))); + circle.extend((x - dy.abs() + 1..x + dy.abs()).map(|x| (x, y + dx))); + circle.extend((x - dy.abs() + 1..x + dy.abs()).map(|x| (x, y - dx))); + } dy = dy + 1; if err < 0 { err = err + 2 * dy + 1; @@ -168,16 +179,6 @@ where err += 2 * (dy - dx) + 1; } } - for xi in 0..radius as i64 { - for yi in 0..radius as i64 { - if xi.pow(2) + yi.pow(2) < (radius as i64).pow(2) { - circle.push((x + xi, y + yi)); - circle.push((x - xi, y + yi)); - circle.push((x + xi, y - yi)); - circle.push((x - xi, y - yi)); - } - } - } circle .into_iter() .flat_map(|pt| MapPoint::try_from(pt)) @@ -229,22 +230,6 @@ 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 { @@ -254,3 +239,14 @@ fn abs_difference + Ord>(x: T, y: T) -> T { x - y } } + +pub fn mirror_figure(figure: &[MapPoint], line: u32, axis: Axis) -> Vec { + figure + .iter() + .map(|pt| pt.mirror_about(line, axis)) + .collect() +} + +pub fn reflect_figure(figure: &[MapPoint], around: MapPoint) -> Vec { + figure.iter().map(|pt| pt.reflect(around)).collect() +} diff --git a/src/symmetry.rs b/src/symmetry.rs new file mode 100644 index 0000000..7517317 --- /dev/null +++ b/src/symmetry.rs @@ -0,0 +1,42 @@ +use std::fmt; + +use crate::bitmap::{mirror_figure, reflect_figure, Axis, MapPoint}; + +#[derive(Debug, Default, Copy, Clone)] +pub struct Symmetry { + pub x: Option, + pub y: Option, +} + +impl Symmetry { + pub fn apply(self, figure: &[MapPoint]) -> Vec { + let Symmetry { x, y } = self; + match (x, y) { + (None, None) => vec![], + (Some(line), None) => mirror_figure(figure, line, Axis::X), + (None, Some(line)) => mirror_figure(figure, line, Axis::Y), + (Some(x), Some(y)) => { + let along_x = mirror_figure(figure, x, Axis::X); + let along_y = mirror_figure(figure, y, Axis::Y); + let reflected = reflect_figure(figure, (x, y).into()); + along_x + .into_iter() + .chain(along_y) + .chain(reflected) + .collect() + } + } + } +} + +impl fmt::Display for Symmetry { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Symmetry { x, y } = self; + match (x, y) { + (None, None) => write!(f, " "), + (Some(_), None) => write!(f, "-"), + (None, Some(_)) => write!(f, "|"), + (Some(_), Some(_)) => write!(f, "+"), + } + } +} -- cgit v1.2.3