aboutsummaryrefslogtreecommitdiff
path: root/src/symmetry.rs
blob: 6d4aed285fe0cfb665656d83db03a4a45790119f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::fmt;

use crate::bitmap::{mirror_figure, reflect_figure, Axis, MapPoint};

#[derive(Debug, Default, Copy, Clone)]
pub struct Symmetry {
    pub x: Option<u32>,
    pub y: Option<u32>,
}

impl Symmetry {
    pub fn apply(self, figure: &[MapPoint]) -> Vec<MapPoint> {
        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, (y, x).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, "OFF"),
            (Some(_), None) => write!(f, "HOR"),
            (None, Some(_)) => write!(f, "VER"),
            (Some(_), Some(_)) => write!(f, "RAD"),
        }
    }
}