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 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) (limited to 'src/bitmap.rs') 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() +} -- cgit v1.2.3