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
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct Rect {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
}
impl Rect {
pub fn new(x: i32, y: i32, width: i32, height: i32) -> Rect {
Rect {
x: x,
y: y,
width: width,
height: height,
}
}
#[inline(always)]
pub fn top(&self) -> i32 {
self.y
}
#[inline(always)]
pub fn bottom(&self) -> i32 {
self.y + self.height
}
#[inline(always)]
pub fn left(&self) -> i32 {
self.x
}
#[inline(always)]
pub fn right(&self) -> i32 {
self.x + self.width
}
#[inline(always)]
pub fn area(&self) -> i32 {
self.width * self.height
}
pub fn intersects(&self, other: &Rect) -> bool {
self.contains_point(other.left(), other.top()) ||
self.contains_point(other.left(), other.bottom() - 1) ||
self.contains_point(other.right() - 1, other.bottom() - 1) ||
self.contains_point(other.right() - 1, other.top()) ||
other.contains_point(self.left(), self.top()) ||
other.contains_point(self.left(), self.bottom() - 1) ||
other.contains_point(self.right() - 1, self.bottom() - 1) ||
other.contains_point(self.right() - 1, self.top())
}
pub fn contains(&self, other: &Rect) -> bool {
self.left() <= other.left() &&
self.right() >= other.right() &&
self.top() <= other.top() &&
self.bottom() >= other.bottom()
}
pub fn contains_point(&self, x: i32, y: i32) -> bool {
self.left() <= x && x < self.right() &&
self.top() <= y && y < self.bottom()
}
}