shape_core/elements/rectangles/rectangle_2d/
constructors.rs

1use super::*;
2
3impl<T> Debug for Rectangle<T>
4    where
5        T: Debug,
6{
7    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
8        let origin = Point { x: &self.min, y: &self.max };
9        f.debug_struct("Rectangle").field("origin", &origin).field("diagonal", &self.max).finish()
10    }
11}
12
13impl<T: Display> Display for Rectangle<T> {
14    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
15        write!(f, "Rectangle({}, {})", self.min, self.max)
16    }
17}
18
19/// Constructors for rectangle_2d
20impl<T> Rectangle<T> {
21    /// Create a rectangle_2d from 4 properties
22    ///
23    /// # Notice
24    ///
25    /// The constructor will not check the legality of the parameters, call [`is_valid`] to ensure that the rectangle_2d is legal.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// # use shape_core::Rectangle;
31    /// let rect = Rectangle::empty();
32    /// ```
33    pub fn empty() -> Self
34        where
35            T: Zero,
36    {
37        Self {
38            min: Point { x: T::zero(), y:T::zero() },
39            max: Point { x:T::zero(), y:T::zero() },
40        }
41    }
42
43    /// Create a rectangle_2d from 4 properties
44    ///
45    /// # Notice
46    ///
47    /// The constructor will not check the legality of the parameters, call [`is_valid`] to ensure that the rectangle_2d is legal.
48    ///
49    /// # Examples
50    ///
51    /// ```
52    /// # use shape_core::Rectangle;
53    /// let rect = Rectangle::new(0.0, 0.0, 1.0, 1.0);
54    /// ```
55    pub fn new(x: T, y: T, width: T, height: T) -> Self
56        where
57            T: Clone + Add<Output=T>,
58    {
59        let min = Point::new(x, y);
60        Self::from_anchor(min, width, height)
61    }
62    /// Create a `[Rectangle]` from anchor (top left point) and 2 properties
63    ///
64    /// # Notice
65    ///
66    /// The constructor will not check the legality of the parameters, call [`is_valid`] to ensure that the rectangle_2d is legal.
67    ///
68    /// # Examples
69    ///
70    /// ```
71    /// # use shape_core::Rectangle;
72    /// let rect = Rectangle::from_anchor((0.0, 0.0), 1.0, 1.0);
73    /// ```
74    pub fn from_anchor<P>(origin: P, width: T, height: T) -> Self
75        where
76            T: Clone + Add<Output=T>,
77            P: Into<Point<T>>,
78    {
79        let min = origin.into();
80        let max = Point::new(min.x.clone() + width, min.y.clone() + height);
81        Self { min, max }
82    }
83    /// Create a rectangle_2d from center and 2 properties
84    ///
85    /// # Notice
86    ///
87    /// The constructor will not check the legality of the parameters, call [`is_valid`] to ensure that the rectangle_2d is legal.
88    ///
89    /// # Examples
90    ///
91    /// ```
92    /// # use shape_core::Rectangle;
93    /// let rect = Rectangle::from_center((0.0, 0.0), 1.0, 1.0);
94    /// ```
95    pub fn from_center<P>(center: P, width: T, height: T) -> Self
96        where
97            T: Clone + One + Add<Output=T> + Sub<Output=T> + Div<Output=T>,
98            P: Into<Point<T>>,
99    {
100        let center = center.into();
101        let half_width = width.clone() / (T::one() + T::one());
102        let half_height = height.clone() / (T::one() + T::one());
103        let min = Point::new(center.x.clone() - half_width.clone(), center.y.clone() - half_height.clone());
104        let max = Point::new(center.x.clone() + half_width.clone(), center.y.clone() + half_height.clone());
105        Self { min, max }
106    }
107    pub fn from_origin(width: T, height: T) -> Self
108        where
109            T: Clone + Zero +One + Add<Output=T> + Sub<Output=T> + Div<Output=T>,
110    {
111        let half_width = width.clone() / (T::one() + T::one());
112        let half_height = height.clone() / (T::one() + T::one());
113        let min = Point::new(T::zero() - half_width.clone(), T::zero() - half_height.clone());
114        let max = Point::new(T::zero() + half_width.clone(), T::zero() + half_height.clone());
115        Self { min, max }
116    }
117    /// Create a rectangle_2d from two diagonal points.
118    ///
119    /// # Notice
120    ///
121    /// The constructor will not check the legality of the parameters, it is possible that two points coincide, or the side length is negative
122    ///
123    /// # Examples
124    ///
125    /// ```
126    /// # use shape_core::Rectangle;
127    /// let rect = Rectangle::from_min_max((0.0, 0.0), (1.0, 1.0));
128    /// ```
129    pub fn from_min_max<P1, P2>(min: P1, max: P2) -> Rectangle<T>
130        where
131            P1: Into<Point<T>>,
132            P2: Into<Point<T>>,
133    {
134        Rectangle { min: min.into(), max: max.into() }
135    }
136}