File size: 1,522 Bytes
2409829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use glam::DVec2;

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Aabb {
	pub top: f64,
	pub right: f64,
	pub bottom: f64,
	pub left: f64,
}

pub(crate) fn bounding_boxes_overlap(a: &Aabb, b: &Aabb) -> bool {
	a.left <= b.right && b.left <= a.right && a.top <= b.bottom && b.top <= a.bottom
}

pub(crate) fn merge_bounding_boxes(a: Option<Aabb>, b: &Aabb) -> Aabb {
	match a {
		Some(a) => Aabb {
			top: a.top.min(b.top),
			right: a.right.max(b.right),
			bottom: a.bottom.max(b.bottom),
			left: a.left.min(b.left),
		},
		None => *b,
	}
}

pub(crate) fn extend_bounding_box(bounding_box: Option<Aabb>, point: DVec2) -> Aabb {
	match bounding_box {
		Some(bb) => Aabb {
			top: bb.top.min(point.y),
			right: bb.right.max(point.x),
			bottom: bb.bottom.max(point.y),
			left: bb.left.min(point.x),
		},
		None => Aabb {
			top: point.y,
			right: point.x,
			bottom: point.y,
			left: point.x,
		},
	}
}

pub(crate) fn bounding_box_max_extent(bounding_box: &Aabb) -> f64 {
	(bounding_box.right - bounding_box.left).max(bounding_box.bottom - bounding_box.top)
}

pub(crate) fn bounding_box_around_point(point: DVec2, padding: f64) -> Aabb {
	Aabb {
		top: point.y - padding,
		right: point.x + padding,
		bottom: point.y + padding,
		left: point.x - padding,
	}
}

pub(crate) fn expand_bounding_box(bounding_box: &Aabb, padding: f64) -> Aabb {
	Aabb {
		top: bounding_box.top - padding,
		right: bounding_box.right + padding,
		bottom: bounding_box.bottom + padding,
		left: bounding_box.left - padding,
	}
}