shape_core/elements/rectangles/square_2d/
mod.rs1use super::*;
2use std::vec::IntoIter;
3
4mod constructors;
5
6#[cfg_attr(feature = "serde", repr(C), derive(Serialize, Deserialize))]
19#[derive(Copy, Clone, PartialEq, Eq, Hash)]
20pub struct Square<T> {
21 pub x: T,
23 pub y: T,
25 pub s: T,
27}
28
29impl<T> Square<T> {
30 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}