@@ -64,22 +64,27 @@ impl Rect {
67 pub fn area(self) -> u16 {
68 pub fn area(self) -> u16 {
68 self.width * self.height
69 self.width * self.height
71 pub fn left(self) -> u16 {
73 pub fn left(self) -> u16 {
75 pub fn right(self) -> u16 {
78 pub fn right(self) -> u16 {
76 self.x.saturating_add(self.width)
79 self.x.saturating_add(self.width)
79 pub fn top(self) -> u16 {
83 pub fn top(self) -> u16 {
83 pub fn bottom(self) -> u16 {
88 pub fn bottom(self) -> u16 {
84 self.y.saturating_add(self.height)
89 self.y.saturating_add(self.height)
@@ -97,7 +102,18 @@ impl Rect {
105+ /// Calculate the union between two [`Rect`]s.
100 pub fn union(self, other: Rect) -> Rect {
106 pub fn union(self, other: Rect) -> Rect {
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:
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
101 let x1 = min(self.x, other.x);
117 let x1 = min(self.x, other.x);
102 let y1 = min(self.y, other.y);
118 let y1 = min(self.y, other.y);
103 let x2 = max(self.x + self.width, other.x + other.width);
119 let x2 = max(self.x + self.width, other.x + other.width);
@@ -110,7 +126,18 @@ impl Rect {
129+ /// Calculate the intersection between two [`Rect`]s.
113 pub fn intersection(self, other: Rect) -> Rect {
130 pub fn intersection(self, other: Rect) -> Rect {
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:
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
114 let x1 = max(self.x, other.x);
141 let x1 = max(self.x, other.x);
115 let y1 = max(self.y, other.y);
142 let y1 = max(self.y, other.y);
116 let x2 = min(self.x + self.width, other.x + other.width);
143 let x2 = min(self.x + self.width, other.x + other.width);