aboutsummaryrefslogtreecommitdiff
path: root/src/symmetry.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/symmetry.rs')
-rw-r--r--src/symmetry.rs42
1 files changed, 42 insertions, 0 deletions
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 @@
1use std::fmt;
2
3use crate::bitmap::{mirror_figure, reflect_figure, Axis, MapPoint};
4
5#[derive(Debug, Default, Copy, Clone)]
6pub struct Symmetry {
7 pub x: Option<u32>,
8 pub y: Option<u32>,
9}
10
11impl Symmetry {
12 pub fn apply(self, figure: &[MapPoint]) -> Vec<MapPoint> {
13 let Symmetry { x, y } = self;
14 match (x, y) {
15 (None, None) => vec![],
16 (Some(line), None) => mirror_figure(figure, line, Axis::X),
17 (None, Some(line)) => mirror_figure(figure, line, Axis::Y),
18 (Some(x), Some(y)) => {
19 let along_x = mirror_figure(figure, x, Axis::X);
20 let along_y = mirror_figure(figure, y, Axis::Y);
21 let reflected = reflect_figure(figure, (x, y).into());
22 along_x
23 .into_iter()
24 .chain(along_y)
25 .chain(reflected)
26 .collect()
27 }
28 }
29 }
30}
31
32impl fmt::Display for Symmetry {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 let Symmetry { x, y } = self;
35 match (x, y) {
36 (None, None) => write!(f, " "),
37 (Some(_), None) => write!(f, "-"),
38 (None, Some(_)) => write!(f, "|"),
39 (Some(_), Some(_)) => write!(f, "+"),
40 }
41 }
42}