shape_core/elements/rectangles/square_2d/
mod.rs

1use super::*;
2use std::vec::IntoIter;
3
4mod constructors;
5
6/// A square is a special case of a rectangle_2d, where the width and height are equal.
7///
8/// # Notice
9///
10/// The constructor will not check the legality of the parameters, call [`is_valid`] to ensure that the rectangle_2d is legal.
11///
12/// # Examples
13///
14/// ```
15/// # use shape_core::Square;
16/// let rect = Square::new(0.0, 0.0, 1.0);
17/// ```
18#[cfg_attr(feature = "serde", repr(C), derive(Serialize, Deserialize))]
19#[derive(Copy, Clone, PartialEq, Eq, Hash)]
20pub struct Square<T> {
21    /// The origin x points of the square.
22    pub x: T,
23    /// The origin y points of the square.
24    pub y: T,
25    /// The side length of the square.
26    pub s: T,
27}
28
29impl<T> Square<T> {
30    /// Get the origin of the square.
31    pub fn origin(&self) -> Point<&T>
32    where
33        T: Clone,
34    {
35        Point { x: &self.x, y: &self.y }
36    }
37}
38
39impl<T> Shape2D for Square<T>
40where
41    T: Signed + Clone + PartialOrd,
42{
43    type Value = T;
44    type VertexIterator<'a>
45    = IntoIter<Point<T>>where
46        T: 'a;
47    type LineIterator<'a>
48    = IntoIter<Line<T>>where
49        T: 'a;
50
51    fn is_valid(&self) -> bool {
52        self.s > T::zero()
53    }
54    fn normalize(&mut self) -> bool {
55        todo!()
56    }
57    fn boundary(&self) -> Rectangle<Self::Value> {
58        Rectangle {
59            min: Point { x: self.x.clone(), y: self.y.clone() },
60            max: Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() + self.s.clone() },
61        }
62    }
63
64    fn vertices<'a>(&'a self, _: usize) -> Self::VertexIterator<'a> {
65        vec![
66            Point { x: self.x.clone(), y: self.y.clone() },
67            Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() },
68            Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() + self.s.clone() },
69            Point { x: self.x.clone(), y: self.y.clone() + self.s.clone() },
70        ]
71        .into_iter()
72    }
73
74    fn edges<'a>(&'a self, _: usize) -> Self::LineIterator<'a> {
75        let mut start = Point { x: self.x.clone(), y: self.y.clone() };
76        let mut end = Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() };
77        let mut out = Vec::with_capacity(4);
78        {
79            out.push(Line::new(start.clone(), end.clone()));
80            start = end;
81            end = Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() + self.s.clone() };
82            out.push(Line::new(start.clone(), end.clone()));
83            start = end;
84            end = Point { x: self.x.clone(), y: self.y.clone() + self.s.clone() };
85            out.push(Line::new(start.clone(), end.clone()));
86            start = end;
87            end = Point { x: self.x.clone(), y: self.y.clone() };
88            out.push(Line::new(start, end));
89        };
90        out.into_iter()
91    }
92}