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
use crate::*;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct AABB<T> {
pub x_min: T,
pub x_max: T,
pub y_min: T,
pub y_max: T,
}
impl<T: UNum> AABB<T> {
pub fn bottom_left(&self) -> Vec2<T> {
vec2(self.x_min, self.y_min)
}
pub fn bottom_right(&self) -> Vec2<T> {
vec2(self.x_max, self.y_min)
}
pub fn top_left(&self) -> Vec2<T> {
vec2(self.x_min, self.y_max)
}
pub fn top_right(&self) -> Vec2<T> {
vec2(self.x_max, self.y_max)
}
pub fn center(&self) -> Vec2<T> {
let two: T = T::ONE + T::ONE;
vec2(
(self.x_min + self.x_max) / two,
(self.y_min + self.y_max) / two,
)
}
pub fn from_corners(p1: Vec2<T>, p2: Vec2<T>) -> Self {
let (x_min, x_max) = partial_min_max(p1.x, p2.x);
let (y_min, y_max) = partial_min_max(p1.y, p2.y);
Self {
x_min,
x_max,
y_min,
y_max,
}
}
pub fn pos_size(pos: Vec2<T>, size: Vec2<T>) -> Self {
Self {
x_min: pos.x,
y_min: pos.y,
x_max: pos.x + size.x,
y_max: pos.y + size.y,
}
}
pub fn map<U: UNum, F: Fn(T) -> U>(self, f: F) -> AABB<U> {
AABB {
x_min: f(self.x_min),
x_max: f(self.x_max),
y_min: f(self.y_min),
y_max: f(self.y_max),
}
}
pub fn width(&self) -> T {
self.x_max - self.x_min
}
pub fn height(&self) -> T {
self.y_max - self.y_min
}
pub fn size(&self) -> Vec2<T> {
vec2(self.width(), self.height())
}
pub fn contains(&self, point: Vec2<T>) -> bool {
self.x_min <= point.x
&& point.x < self.x_max
&& self.y_min <= point.y
&& point.y < self.y_max
}
pub fn intersects(&self, other: &Self) -> bool {
self.x_max > other.x_min
&& self.y_max > other.y_min
&& self.x_min < other.x_max
&& self.y_min < other.y_max
}
pub fn translate(self, v: Vec2<T>) -> Self {
Self {
x_min: self.x_min + v.x,
x_max: self.x_max + v.x,
y_min: self.y_min + v.y,
y_max: self.y_max + v.y,
}
}
}
impl<T: Float> AABB<T> {
pub fn distance_to(&self, other: &Self) -> T {
partial_max(
partial_max(
partial_max(self.x_min - other.x_max, other.x_min - self.x_max),
partial_max(self.y_min - other.y_max, other.y_min - self.y_max),
),
T::ZERO,
)
}
}