diff options
Diffstat (limited to 'src/bitmap.rs')
-rw-r--r-- | src/bitmap.rs | 68 |
1 files changed, 67 insertions, 1 deletions
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 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | convert::{From, Into, TryFrom}, | 2 | convert::{From, Into, TryFrom}, |
3 | ops::Sub, | 3 | ops::{Add, Sub}, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | #[derive(Debug)] | 6 | #[derive(Debug)] |
@@ -50,6 +50,56 @@ impl TryFrom<(i64, i64)> for MapPoint { | |||
50 | } | 50 | } |
51 | } | 51 | } |
52 | 52 | ||
53 | impl Add for MapPoint { | ||
54 | type Output = Self; | ||
55 | fn add(self, rhs: Self) -> Self::Output { | ||
56 | MapPoint { | ||
57 | x: self.x + rhs.x, | ||
58 | y: self.y + rhs.y, | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | |||
63 | impl Sub for MapPoint { | ||
64 | type Output = Self; | ||
65 | fn sub(self, rhs: Self) -> Self::Output { | ||
66 | MapPoint { | ||
67 | x: self.x - rhs.x, | ||
68 | y: self.y - rhs.y, | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | #[derive(Debug, Copy, Clone)] | ||
74 | pub enum Axis { | ||
75 | X, | ||
76 | Y, | ||
77 | } | ||
78 | |||
79 | impl MapPoint { | ||
80 | #[inline] | ||
81 | pub fn scale(self, c: u32) -> MapPoint { | ||
82 | MapPoint { | ||
83 | x: self.x * c, | ||
84 | y: self.y * c, | ||
85 | } | ||
86 | } | ||
87 | |||
88 | #[inline] | ||
89 | pub fn mirror_about(self, line: u32, axis: Axis) -> MapPoint { | ||
90 | let MapPoint { x, y } = self; | ||
91 | match axis { | ||
92 | Axis::X => MapPoint { x, y: 2 * line - y }, | ||
93 | Axis::Y => MapPoint { x: 2 * line - x, y }, | ||
94 | } | ||
95 | } | ||
96 | |||
97 | #[inline] | ||
98 | pub fn reflect(self, around: MapPoint) -> MapPoint { | ||
99 | around.scale(2) - self | ||
100 | } | ||
101 | } | ||
102 | |||
53 | impl<T> Pixmap<T> | 103 | impl<T> Pixmap<T> |
54 | where | 104 | where |
55 | T: Copy + Clone + Default, | 105 | T: Copy + Clone + Default, |
@@ -179,6 +229,22 @@ where | |||
179 | .filter(|&pt| self.is_inside(pt)) | 229 | .filter(|&pt| self.is_inside(pt)) |
180 | .collect() | 230 | .collect() |
181 | } | 231 | } |
232 | |||
233 | pub fn mirror_figure(&self, figure: &[MapPoint], line: u32, axis: Axis) -> Vec<MapPoint> { | ||
234 | figure | ||
235 | .iter() | ||
236 | .map(|pt| pt.mirror_about(line, axis)) | ||
237 | .filter(|&pt| self.is_inside(pt)) | ||
238 | .collect() | ||
239 | } | ||
240 | |||
241 | pub fn reflect_figure(&self, figure: &[MapPoint], around: MapPoint) -> Vec<MapPoint> { | ||
242 | figure | ||
243 | .iter() | ||
244 | .map(|pt| pt.reflect(around)) | ||
245 | .filter(|&pt| self.is_inside(pt)) | ||
246 | .collect() | ||
247 | } | ||
182 | } | 248 | } |
183 | 249 | ||
184 | fn abs_difference<T: Sub<Output = T> + Ord>(x: T, y: T) -> T { | 250 | fn abs_difference<T: Sub<Output = T> + Ord>(x: T, y: T) -> T { |