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
use glam::Vec2;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
pos: Vec2,
size: Vec2,
}
impl Rect {
pub const ZERO: Self = Self {
pos: Vec2::ZERO,
size: Vec2::ZERO,
};
pub const ONE: Self = Self {
pos: Vec2::ZERO,
size: Vec2::ONE,
};
#[inline]
pub fn from_pos_size(pos: Vec2, size: Vec2) -> Self {
Self { pos, size }
}
#[inline]
pub fn pos(&self) -> Vec2 {
self.pos
}
#[inline]
pub fn size(&self) -> Vec2 {
self.size
}
#[inline]
pub fn max(&self) -> Vec2 {
self.pos + self.size
}
#[inline]
pub fn set_pos(&mut self, pos: Vec2) {
self.pos = pos;
}
#[inline]
pub fn set_size(&mut self, size: Vec2) {
self.size = size;
}
#[inline]
pub fn contains_point(&self, point: Vec2) -> bool {
point.x >= self.pos.x
&& point.x <= self.pos.x + self.size.x
&& point.y >= self.pos.y
&& point.y <= self.pos.y + self.size.y
}
#[inline]
pub fn intersects(&self, other: &Self) -> bool {
let self_max = self.max();
let other_max = other.max();
let x_intersect = self.pos.x < other_max.x && self_max.x > other.pos.x;
let y_intersect = self.pos.y < other_max.y && self_max.y > other.pos.y;
x_intersect && y_intersect
}
#[inline]
pub fn div_vec2(&self, size: Vec2) -> Self {
Self::from_pos_size(self.pos / size, self.size / size)
}
}