Another project
1use bone_types::Point2;
2
3#[derive(Copy, Clone, Debug, PartialEq)]
4pub struct Aabb2 {
5 min: Point2,
6 max: Point2,
7}
8
9impl Aabb2 {
10 #[must_use]
11 pub fn from_corners(a: Point2, b: Point2) -> Self {
12 let (ax, ay) = a.coords_mm();
13 let (bx, by) = b.coords_mm();
14 Self {
15 min: Point2::from_mm(ax.min(bx), ay.min(by)),
16 max: Point2::from_mm(ax.max(bx), ay.max(by)),
17 }
18 }
19
20 #[must_use]
21 pub fn min(self) -> Point2 {
22 self.min
23 }
24
25 #[must_use]
26 pub fn max(self) -> Point2 {
27 self.max
28 }
29
30 #[must_use]
31 pub fn extend_point(self, p: Point2) -> Self {
32 let (px, py) = p.coords_mm();
33 let (mnx, mny) = self.min.coords_mm();
34 let (mxx, mxy) = self.max.coords_mm();
35 Self {
36 min: Point2::from_mm(mnx.min(px), mny.min(py)),
37 max: Point2::from_mm(mxx.max(px), mxy.max(py)),
38 }
39 }
40}
41
42impl core::fmt::Display for Aabb2 {
43 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
44 let (mnx, mny) = self.min.coords_mm();
45 let (mxx, mxy) = self.max.coords_mm();
46 write!(
47 f,
48 "aabb2{{ min=({mnx} mm, {mny} mm), max=({mxx} mm, {mxy} mm) }}"
49 )
50 }
51}