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