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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use skia_safe::Rect;

/// Represents an axis-aligned bounding box.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoundingBox {
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
}

impl std::fmt::Display for BoundingBox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{{ x: {:?}, y: {:?}, w: {:?}, h:{:?} }}", self.x, self.y, self.w, self.h)
    }
}

impl Default for BoundingBox {
    fn default() -> Self {
        Self { x: 0.0, y: 0.0, w: 0.0, h: 0.0 }
    }
}

impl BoundingBox {
    /// Construct a [`BoundingBox`] from checked minimum and maximum values.
    #[inline(always)]
    pub fn from_min_max(min_x: f32, min_y: f32, max_x: f32, max_y: f32) -> BoundingBox {
        BoundingBox { x: min_x, y: min_y, w: max_x - min_x, h: max_y - min_y }
    }

    /// Left side of bounds equivalent to `x`.
    #[inline(always)]
    pub fn left(&self) -> f32 {
        self.x
    }

    /// Top of bounds equivalent to `y`.
    #[inline(always)]
    pub fn top(&self) -> f32 {
        self.y
    }

    /// Bounds width equivalent to `w`.
    #[inline(always)]
    pub fn width(&self) -> f32 {
        self.w
    }

    /// Bounds height equivalent to `h`.
    #[inline(always)]
    pub fn height(&self) -> f32 {
        self.h
    }

    /// Right side of bounds.
    #[inline(always)]
    pub fn right(&self) -> f32 {
        self.left() + self.width()
    }

    /// Bottom side of bounds.
    #[inline(always)]
    pub fn bottom(&self) -> f32 {
        self.top() + self.height()
    }

    /// Horizontal and vertical center of bounds.
    #[inline(always)]
    pub fn center(&self) -> (f32, f32) {
        ((self.width() / 2f32) + self.x, (self.height() / 2f32) + self.y)
    }

    /// Left center of bounds.
    #[inline(always)]
    pub fn center_left(&self) -> (f32, f32) {
        (self.left(), (self.height() / 2f32) + self.top())
    }

    /// Right center of bounds.
    #[inline(always)]
    pub fn center_right(&self) -> (f32, f32) {
        (self.right(), (self.height() / 2f32) + self.top())
    }

    /// Top center of bounds.
    #[inline(always)]
    pub fn center_top(&self) -> (f32, f32) {
        ((self.width() / 2f32) + self.left(), self.top())
    }

    /// Bottom center of bounds.
    #[inline(always)]
    pub fn center_bottom(&self) -> (f32, f32) {
        ((self.width() / 2f32) + self.left(), self.bottom())
    }

    /// Bottom left point of bounds.
    #[inline(always)]
    pub fn bottom_left(&self) -> (f32, f32) {
        (self.left(), self.bottom())
    }

    /// Bottom right point of bounds.
    #[inline(always)]
    pub fn bottom_right(&self) -> (f32, f32) {
        (self.right(), self.bottom())
    }

    /// Top left point of bounds.
    #[inline(always)]
    pub fn top_left(&self) -> (f32, f32) {
        (self.left(), self.top())
    }

    /// Top right point of bounds.
    #[inline(always)]
    pub fn top_right(&self) -> (f32, f32) {
        (self.right(), self.top())
    }

    /// Shrinks by some `amount` in both directions and returns a new [`BoundingBox`].
    #[inline(always)]
    #[must_use]
    pub fn shrink(&self, amount: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left() + amount,
            self.top() + amount,
            self.right() - amount,
            self.bottom() - amount,
        )
    }

    /// Shrinks by some `amount` horizontally and returns a new [`BoundingBox`].
    #[inline(always)]
    #[must_use]
    pub fn shrink_horizontal(&self, amount: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left() + amount,
            self.top(),
            self.right() - amount,
            self.bottom(),
        )
    }

    /// Shrinks by some `amount` vertically and returns a new [`BoundingBox`].
    #[inline(always)]
    #[must_use]
    pub fn shrink_vertical(&self, amount: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left(),
            self.top() + amount,
            self.right(),
            self.bottom() - amount,
        )
    }

    /// Shrinks each side by the given separate amounts and returns a new [`BoundingBox`].
    pub fn shrink_sides(&self, left: f32, top: f32, right: f32, bottom: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left() + left,
            self.top() + top,
            self.right() - right,
            self.bottom() - bottom,
        )
    }

    pub fn expand_sides(&self, left: f32, top: f32, right: f32, bottom: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left() - left,
            self.top() - top,
            self.right() + right,
            self.bottom() + bottom,
        )
    }

    pub fn offset(&self, x: f32, y: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left() + x,
            self.top() + y,
            self.right() + x,
            self.bottom() + y,
        )
    }

    /// Expands by some `amount` in both directions and returns a new [`BoundingBox`].
    #[inline(always)]
    #[must_use]
    pub fn expand(&self, amount: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left() - amount,
            self.top() - amount,
            self.right() + amount,
            self.bottom() + amount,
        )
    }

    /// Expands by some `amount` horizontally and returns a new [`BoundingBox`].
    #[inline(always)]
    #[must_use]
    pub fn expand_horizontal(&self, amount: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left() - amount,
            self.top(),
            self.right() + amount,
            self.bottom(),
        )
    }

    /// Expands by some `amount` vertically and returns a new [`BoundingBox`].
    #[inline(always)]
    #[must_use]
    pub fn expand_vertical(&self, amount: f32) -> BoundingBox {
        BoundingBox::from_min_max(
            self.left(),
            self.top() - amount,
            self.right(),
            self.bottom() + amount,
        )
    }

    pub fn intersection(&self, other: &Self) -> Self {
        let left = self.left().max(other.left());
        let right = self.right().min(other.right());
        let top = self.top().max(other.top());
        let bottom = self.bottom().min(other.bottom());
        BoundingBox::from_min_max(left, top, right, bottom)
    }

    pub fn union(&self, other: &Self) -> Self {
        let left = self.left().min(other.left());
        let right = self.right().max(other.right());
        let top = self.top().min(other.top());
        let bottom = self.bottom().max(other.bottom());
        BoundingBox::from_min_max(left, top, right, bottom)
    }

    pub fn intersects(&self, other: &Self) -> bool {
        let x_hit = (self.x >= other.x && self.x < other.x + other.w)
            || (other.x >= self.x && other.x < self.x + self.w);
        let y_hit = (self.y >= other.y && self.y < other.y + other.h)
            || (other.y >= self.y && other.y < self.y + self.h);
        x_hit && y_hit
    }

    pub fn contains(&self, other: &Self) -> bool {
        let x_hit = other.x >= self.x && other.x + other.w < self.x + self.w;
        let y_hit = other.y >= self.y && other.y + other.h < self.y + self.h;
        x_hit && y_hit
    }

    pub fn contains_point(&self, x: f32, y: f32) -> bool {
        let x_hit = x >= self.x && x < self.x + self.w;
        let y_hit = y >= self.y && y < self.y + self.h;
        x_hit && y_hit
    }

    pub fn diagonal(&self) -> f32 {
        (self.width() * self.width() + self.height() * self.height()).sqrt()
    }

    // pub fn transform(&self, transform: &Transform2D) -> Self {
    //     let (tl, tt) = transform.transform_point(self.x, self.y);
    //     let (tr, tb) = transform.transform_point(self.right(), self.bottom());
    //     BoundingBox::from_min_max(tl, tt, tr, tb)
    // }
}

impl From<BoundingBox> for skia_safe::Rect {
    fn from(bb: BoundingBox) -> Self {
        skia_safe::Rect { left: bb.left(), top: bb.top(), right: bb.right(), bottom: bb.bottom() }
    }
}

impl From<skia_safe::Rect> for BoundingBox {
    fn from(bb: Rect) -> Self {
        BoundingBox { x: bb.left(), y: bb.top(), w: bb.width(), h: bb.height() }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    fn rect() -> BoundingBox {
        BoundingBox { x: 100f32, y: 100f32, w: 100f32, h: 100f32 }
    }

    #[test]
    fn get_center() {
        let rect = rect();
        assert_eq!(rect.center(), (150f32, 150f32));
    }

    #[test]
    fn get_center_top() {
        let rect = rect();
        assert_eq!(rect.center_top(), (150f32, 100f32));
    }

    #[test]
    fn get_center_bottom() {
        let rect = rect();
        assert_eq!(rect.center_bottom(), (150f32, 200f32));
    }

    #[test]
    fn get_center_left() {
        let rect = rect();
        assert_eq!(rect.center_left(), (100f32, 150f32));
    }

    #[test]
    fn get_center_right() {
        let rect = rect();
        assert_eq!(rect.center_right(), (200f32, 150f32));
    }

    #[test]
    fn get_left() {
        let rect = rect();
        assert_eq!(rect.left(), 100f32);
    }

    #[test]
    fn get_right() {
        let rect = rect();
        assert_eq!(rect.right(), 200f32);
    }

    #[test]
    fn get_top() {
        let rect = rect();
        assert_eq!(rect.top(), 100f32);
    }

    #[test]
    fn get_bottom() {
        let rect = rect();
        assert_eq!(rect.bottom(), 200f32);
    }

    #[test]
    fn get_shrunken() {
        let rect = rect();
        let a = rect.shrink(25f32);
        let b = BoundingBox { x: 125f32, y: 125f32, w: 50f32, h: 50f32 };
        assert_eq!(a, b);
    }

    #[test]
    fn get_shrunken_horizontal() {
        let rect = rect();
        let a = rect.shrink_horizontal(25f32);
        let b = BoundingBox { x: 125f32, y: 100f32, w: 50f32, h: 100f32 };
        assert_eq!(a, b);
    }

    #[test]
    fn get_shrunken_vertical() {
        let rect = rect();
        let a = rect.shrink_vertical(25f32);
        let b = BoundingBox { x: 100f32, y: 125f32, w: 100f32, h: 50f32 };
        assert_eq!(a, b);
    }

    #[test]
    fn get_expanded() {
        let rect = rect();
        let a = rect.expand(25f32);
        let b = BoundingBox { x: 75f32, y: 75f32, w: 150f32, h: 150f32 };
        assert_eq!(a, b);
    }

    #[test]
    fn get_expanded_horizontal() {
        let rect = rect();
        let a = rect.expand_horizontal(25f32);
        let b = BoundingBox { x: 75f32, y: 100f32, w: 150f32, h: 100f32 };
        assert_eq!(a, b);
    }

    #[test]
    fn get_expanded_vertical() {
        let rect = rect();
        let a = rect.expand_vertical(25f32);
        let b = BoundingBox { x: 100f32, y: 75f32, w: 100f32, h: 150f32 };
        assert_eq!(a, b);
    }
}