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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use rusttype::Rect;
use crate::dimen::Vector2;
#[derive(Debug, PartialEq, Clone)]
#[repr(C)]
pub struct Rectangle<T = f32>
{
top_left: Vector2<T>,
bottom_right: Vector2<T>
}
impl<T> Rectangle<T>
{
#[inline]
pub const fn new(top_left: Vector2<T>, bottom_right: Vector2<T>) -> Self
{
Rectangle {
top_left,
bottom_right
}
}
#[inline]
pub fn from_tuples(top_left: (T, T), bottom_right: (T, T)) -> Self
{
Rectangle {
top_left: Vector2::new(top_left.0, top_left.1),
bottom_right: Vector2::new(bottom_right.0, bottom_right.1)
}
}
#[inline]
pub const fn top_left(&self) -> &Vector2<T>
{
&self.top_left
}
#[inline]
pub const fn bottom_right(&self) -> &Vector2<T>
{
&self.bottom_right
}
}
impl<T: Copy> Rectangle<T>
{
#[inline]
pub fn top_right(&self) -> Vector2<T>
{
Vector2::new(self.bottom_right.x, self.top_left.y)
}
#[inline]
pub fn bottom_left(&self) -> Vector2<T>
{
Vector2::new(self.top_left.x, self.bottom_right.y)
}
}
impl<T: std::ops::Sub<Output = T> + Copy> Rectangle<T>
{
#[inline]
pub fn width(&self) -> T
{
self.bottom_right.x - self.top_left.x
}
#[inline]
pub fn height(&self) -> T
{
self.bottom_right.y - self.top_left.y
}
#[inline]
pub fn size(&self) -> Vector2<T>
{
Vector2::new(self.width(), self.height())
}
}
impl<T: PartialEq> Rectangle<T>
{
#[inline]
pub fn is_zero_area(&self) -> bool
{
self.top_left.x == self.bottom_right.x || self.top_left.y == self.bottom_right.y
}
}
impl<T: Copy> Rectangle<T>
where
Vector2<T>: std::ops::Add<Output = Vector2<T>>
{
#[inline]
pub fn with_offset(&self, offset: Vector2<T>) -> Self
{
Rectangle::new(self.top_left + offset, self.bottom_right + offset)
}
}
impl<T> From<rusttype::Rect<T>> for Rectangle<T>
{
fn from(rect: Rect<T>) -> Self
{
Rectangle::new(Vector2::from(rect.min), Vector2::from(rect.max))
}
}
impl<T: num_traits::AsPrimitive<f32>> Rectangle<T>
{
#[inline]
#[must_use]
pub fn into_f32(self) -> Rectangle<f32>
{
Rectangle::new(self.top_left.into_f32(), self.bottom_right.into_f32())
}
}