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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::*;
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Rect<T> {
pub location: Vector2<T>,
pub dimensions: Vector2<T>,
}
impl<T> Rect<T>
where
T: SpacialNumeric,
{
pub fn new<V: Into<Vector2<T>>>(position: V, size: V) -> Self {
Rect {
location: position.into(),
dimensions: size.into(),
}
}
pub fn min_point(&self) -> Vector2<T> {
self.location
}
pub fn max_point(&self) -> Vector2<T> {
self.location + self.dimensions
}
pub fn intersect(&self, other: &Self) -> Option<Self> {
let Vector2 {
x: self_min_x,
y: self_min_y,
} = self.min_point();
let Vector2 {
x: self_max_x,
y: self_max_y,
} = self.max_point();
let Vector2 {
x: other_min_x,
y: other_min_y,
} = other.min_point();
let Vector2 {
x: other_max_x,
y: other_max_y,
} = other.max_point();
if self_min_x < other_max_x
&& self_max_x > other_min_x
&& self_max_y > other_min_y
&& self_min_y < other_max_y
{
let min_point = Vector2 {
x: if self_min_x > other_min_x {
self_min_x
} else {
other_min_x
},
y: if self_min_y > other_min_y {
self_min_y
} else {
other_min_y
},
};
let max_point = Vector2 {
x: if self_max_x < other_max_x {
self_max_x
} else {
other_max_x
},
y: if self_max_y < other_max_y {
self_max_y
} else {
other_max_y
},
};
Some(Rect {
location: min_point,
dimensions: max_point - min_point,
})
} else {
None
}
}
pub fn contains_point(&self, point: Vector2<T>) -> bool {
let min_point = self.min_point();
let max_point = self.max_point();
!(point.x < min_point.x
|| point.y < min_point.y
|| point.x >= max_point.x
|| point.y >= max_point.y)
}
}
impl<T> Rect<T>
where
T: SignedSpacialNumeric,
{
pub fn intersect_relative_to_both<S: SpacialNumericConversion<T>>(
size_a: Vector2<S>,
size_b: Vector2<S>,
b_relative_position: Vector2<T>,
) -> Option<(Self, Self)> {
let mut rect_a = Rect {
location: Default::default(),
dimensions: size_a.convert(),
};
let mut rect_b = Rect {
location: b_relative_position,
dimensions: size_b.convert(),
};
let rel_a_rect = rect_a.intersect(&rect_b);
rect_b.location = rect_a.location;
rect_a.location = -b_relative_position;
let rel_b_rect = rect_b.intersect(&rect_a);
if let (Some(rel_a), Some(rel_b)) = (rel_a_rect, rel_b_rect) {
Some((rel_a, rel_b))
} else {
None
}
}
}
impl<T: SpacialNumericConversion<U>, U> SpacialNumericConversion<Rect<U>> for Rect<T> {
#[inline]
fn convert(self) -> Rect<U> {
Rect {
location: self.location.convert(),
dimensions: self.dimensions.convert(),
}
}
}
impl<T: PartialEq> PartialEq for Rect<T> {
fn eq(&self, other: &Self) -> bool {
self.location == other.location && self.dimensions == other.dimensions
}
}
impl<T: PartialEq> Eq for Rect<T> {}