Skip to main content

use_reflection/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use use_point::Point2;
5
6/// Axis-aligned 2D reflection choices.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum AxisReflection2 {
9    /// Reflect across the x axis.
10    AcrossX,
11    /// Reflect across the y axis.
12    AcrossY,
13    /// Reflect through the origin.
14    ThroughOrigin,
15}
16
17impl AxisReflection2 {
18    /// Reflects a point according to this reflection.
19    #[must_use]
20    pub const fn reflect(self, point: Point2) -> Point2 {
21        match self {
22            Self::AcrossX => Point2::new(point.x(), -point.y()),
23            Self::AcrossY => Point2::new(-point.x(), point.y()),
24            Self::ThroughOrigin => Point2::new(-point.x(), -point.y()),
25        }
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::AxisReflection2;
32    use use_point::Point2;
33
34    #[test]
35    fn reflects_across_axes() {
36        let point = Point2::new(2.0, 3.0);
37
38        assert_eq!(
39            AxisReflection2::AcrossX.reflect(point),
40            Point2::new(2.0, -3.0)
41        );
42        assert_eq!(
43            AxisReflection2::AcrossY.reflect(point),
44            Point2::new(-2.0, 3.0)
45        );
46        assert_eq!(
47            AxisReflection2::ThroughOrigin.reflect(point),
48            Point2::new(-2.0, -3.0)
49        );
50    }
51}