@@ -64,22 +64,27 @@ impl Rect {
6464 }
6565 }
6666
67+ #[inline]
6768 pub fn area(self) -> u16 {
6869 self.width * self.height
6970 }
7071
72+ #[inline]
7173 pub fn left(self) -> u16 {
7274 self.x
7375 }
7476
77+ #[inline]
7578 pub fn right(self) -> u16 {
7679 self.x.saturating_add(self.width)
7780 }
7881
82+ #[inline]
7983 pub fn top(self) -> u16 {
8084 self.y
8185 }
8286
87+ #[inline]
8388 pub fn bottom(self) -> u16 {
8489 self.y.saturating_add(self.height)
8590 }
@@ -97,7 +102,18 @@ impl Rect {
97102 }
98103 }
99104
105+ /// Calculate the union between two [`Rect`]s.
100106 pub fn union(self, other: Rect) -> Rect {
107+ // Example:
108+ //
109+ // If `Rect` A is positioned at `(0, 0)` with a width and height of `5`,
110+ // and `Rect` B is positioned at `(5, 0)` with a width and height of `2`,
111+ // then this is the resulting union:
112+ //
113+ // x1 = min(0, 5) => x1 = 0
114+ // y1 = min(0, 0) => y1 = 0
115+ // x2 = max(0 + 5, 5 + 2) => x2 = 7
116+ // y2 = max(0 + 5, 0 + 2) => y2 = 5
101117 let x1 = min(self.x, other.x);
102118 let y1 = min(self.y, other.y);
103119 let x2 = max(self.x + self.width, other.x + other.width);
@@ -110,7 +126,18 @@ impl Rect {
110126 }
111127 }
112128
129+ /// Calculate the intersection between two [`Rect`]s.
113130 pub fn intersection(self, other: Rect) -> Rect {
131+ // Example:
132+ //
133+ // If `Rect` A is positioned at `(0, 0)` with a width and height of `5`,
134+ // and `Rect` B is positioned at `(5, 0)` with a width and height of `2`,
135+ // then this is the resulting intersection:
136+ //
137+ // x1 = max(0, 5) => x1 = 5
138+ // y1 = max(0, 0) => y1 = 0
139+ // x2 = min(0 + 5, 5 + 2) => x2 = 5
140+ // y2 = min(0 + 5, 0 + 2) => y2 = 2
114141 let x1 = max(self.x, other.x);
115142 let y1 = max(self.y, other.y);
116143 let x2 = min(self.x + self.width, other.x + other.width);