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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use std::f32::INFINITY;
use std::ops::RangeInclusive;
use crate::*;
#[derive(Clone, Copy, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Rect {
pub min: Pos2,
pub max: Pos2,
}
impl Rect {
pub const EVERYTHING: Self = Self {
min: pos2(-INFINITY, -INFINITY),
max: pos2(INFINITY, INFINITY),
};
pub const NOTHING: Self = Self {
min: pos2(INFINITY, INFINITY),
max: pos2(-INFINITY, -INFINITY),
};
pub const NAN: Self = Self {
min: pos2(f32::NAN, f32::NAN),
max: pos2(-f32::NAN, -f32::NAN),
};
#[deprecated = "Use Rect::EVERYTHING"]
pub fn everything() -> Self {
let inf = f32::INFINITY;
Self {
min: pos2(-inf, -inf),
max: pos2(inf, inf),
}
}
#[deprecated = "Use Rect::NOTHING"]
pub fn nothing() -> Self {
let inf = f32::INFINITY;
Self {
min: pos2(inf, inf),
max: pos2(-inf, -inf),
}
}
#[deprecated = "Use Rect::NAN"]
pub fn invalid() -> Self {
Self::NAN
}
#[inline(always)]
pub const fn from_min_max(min: Pos2, max: Pos2) -> Self {
Rect { min, max }
}
pub fn from_min_size(min: Pos2, size: Vec2) -> Self {
Rect {
min,
max: min + size,
}
}
pub fn from_center_size(center: Pos2, size: Vec2) -> Self {
Rect {
min: center - size * 0.5,
max: center + size * 0.5,
}
}
pub fn from_x_y_ranges(x_range: RangeInclusive<f32>, y_range: RangeInclusive<f32>) -> Self {
Rect {
min: pos2(*x_range.start(), *y_range.start()),
max: pos2(*x_range.end(), *y_range.end()),
}
}
pub fn from_two_pos(a: Pos2, b: Pos2) -> Self {
Rect {
min: pos2(a.x.min(b.x), a.y.min(b.y)),
max: pos2(a.x.max(b.x), a.y.max(b.y)),
}
}
pub fn everything_right_of(left_x: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_left(left_x);
rect
}
pub fn everything_left_of(right_x: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_right(right_x);
rect
}
pub fn everything_below(top_y: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_top(top_y);
rect
}
pub fn everything_above(bottom_y: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_bottom(bottom_y);
rect
}
#[must_use]
pub fn expand(self, amnt: f32) -> Self {
self.expand2(Vec2::splat(amnt))
}
#[must_use]
pub fn expand2(self, amnt: Vec2) -> Self {
Rect::from_min_max(self.min - amnt, self.max + amnt)
}
#[must_use]
pub fn shrink(self, amnt: f32) -> Self {
self.shrink2(Vec2::splat(amnt))
}
#[must_use]
pub fn shrink2(self, amnt: Vec2) -> Self {
Rect::from_min_max(self.min + amnt, self.max - amnt)
}
#[must_use]
pub fn translate(self, amnt: Vec2) -> Self {
Rect::from_min_size(self.min + amnt, self.size())
}
#[must_use]
pub fn intersect(self, other: Rect) -> Self {
Self {
min: self.min.max(other.min),
max: self.max.min(other.max),
}
}
#[must_use]
pub fn intersects(self, other: Rect) -> bool {
self.min.x <= other.max.x
&& other.min.x <= self.max.x
&& self.min.y <= other.max.y
&& other.min.y <= self.max.y
}
pub fn set_width(&mut self, w: f32) {
self.max.x = self.min.x + w;
}
pub fn set_height(&mut self, h: f32) {
self.max.y = self.min.y + h;
}
pub fn set_center(&mut self, center: Pos2) {
*self = self.translate(center - self.center());
}
#[must_use]
#[inline(always)]
pub fn contains(&self, p: Pos2) -> bool {
self.min.x <= p.x && p.x <= self.max.x && self.min.y <= p.y && p.y <= self.max.y
}
#[must_use]
pub fn contains_rect(&self, other: Rect) -> bool {
self.contains(other.min) && self.contains(other.max)
}
#[must_use]
pub fn clamp(&self, p: Pos2) -> Pos2 {
p.clamp(self.min, self.max)
}
pub fn extend_with(&mut self, p: Pos2) {
self.min = self.min.min(p);
self.max = self.max.max(p);
}
pub fn extend_with_x(&mut self, x: f32) {
self.min.x = self.min.x.min(x);
self.max.x = self.max.x.max(x);
}
pub fn extend_with_y(&mut self, y: f32) {
self.min.y = self.min.y.min(y);
self.max.y = self.max.y.max(y);
}
pub fn union(self, other: Rect) -> Rect {
Rect {
min: self.min.min(other.min),
max: self.max.max(other.max),
}
}
#[inline(always)]
pub fn center(&self) -> Pos2 {
Pos2 {
x: (self.min.x + self.max.x) / 2.0,
y: (self.min.y + self.max.y) / 2.0,
}
}
#[inline(always)]
pub fn size(&self) -> Vec2 {
self.max - self.min
}
#[inline(always)]
pub fn width(&self) -> f32 {
self.max.x - self.min.x
}
#[inline(always)]
pub fn height(&self) -> f32 {
self.max.y - self.min.y
}
pub fn aspect_ratio(&self) -> f32 {
self.width() / self.height()
}
pub fn square_proportions(&self) -> Vec2 {
let w = self.width();
let h = self.height();
if w > h {
vec2(w / h, 1.0)
} else {
vec2(1.0, h / w)
}
}
pub fn area(&self) -> f32 {
self.width() * self.height()
}
pub fn x_range(&self) -> RangeInclusive<f32> {
self.min.x..=self.max.x
}
pub fn y_range(&self) -> RangeInclusive<f32> {
self.min.y..=self.max.y
}
pub fn bottom_up_range(&self) -> RangeInclusive<f32> {
self.max.y..=self.min.y
}
#[deprecated = "Use is_negative instead"]
pub fn is_empty(&self) -> bool {
self.max.x < self.min.x || self.max.y < self.min.y
}
#[inline(always)]
pub fn is_negative(&self) -> bool {
self.max.x < self.min.x || self.max.y < self.min.y
}
#[deprecated = "Use !is_negative() instead"]
pub fn is_non_negative(&self) -> bool {
!self.is_negative()
}
#[inline(always)]
pub fn is_positive(&self) -> bool {
self.min.x < self.max.x && self.min.y < self.max.y
}
#[inline(always)]
pub fn is_finite(&self) -> bool {
self.min.is_finite() && self.max.is_finite()
}
#[inline(always)]
pub fn any_nan(self) -> bool {
self.min.any_nan() || self.max.any_nan()
}
}
impl Rect {
#[inline(always)]
pub fn left(&self) -> f32 {
self.min.x
}
#[inline(always)]
pub fn left_mut(&mut self) -> &mut f32 {
&mut self.min.x
}
#[inline(always)]
pub fn set_left(&mut self, x: f32) {
self.min.x = x;
}
#[inline(always)]
pub fn right(&self) -> f32 {
self.max.x
}
#[inline(always)]
pub fn right_mut(&mut self) -> &mut f32 {
&mut self.max.x
}
#[inline(always)]
pub fn set_right(&mut self, x: f32) {
self.max.x = x;
}
#[inline(always)]
pub fn top(&self) -> f32 {
self.min.y
}
#[inline(always)]
pub fn top_mut(&mut self) -> &mut f32 {
&mut self.min.y
}
#[inline(always)]
pub fn set_top(&mut self, y: f32) {
self.min.y = y;
}
#[inline(always)]
pub fn bottom(&self) -> f32 {
self.max.y
}
#[inline(always)]
pub fn bottom_mut(&mut self) -> &mut f32 {
&mut self.max.y
}
#[inline(always)]
pub fn set_bottom(&mut self, y: f32) {
self.max.y = y;
}
#[inline(always)]
pub fn left_top(&self) -> Pos2 {
pos2(self.left(), self.top())
}
#[inline(always)]
pub fn center_top(&self) -> Pos2 {
pos2(self.center().x, self.top())
}
#[inline(always)]
pub fn right_top(&self) -> Pos2 {
pos2(self.right(), self.top())
}
#[inline(always)]
pub fn left_center(&self) -> Pos2 {
pos2(self.left(), self.center().y)
}
#[inline(always)]
pub fn right_center(&self) -> Pos2 {
pos2(self.right(), self.center().y)
}
#[inline(always)]
pub fn left_bottom(&self) -> Pos2 {
pos2(self.left(), self.bottom())
}
#[inline(always)]
pub fn center_bottom(&self) -> Pos2 {
pos2(self.center().x, self.bottom())
}
#[inline(always)]
pub fn right_bottom(&self) -> Pos2 {
pos2(self.right(), self.bottom())
}
}
impl std::fmt::Debug for Rect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{:?} - {:?}]", self.min, self.max)
}
}
impl From<[Pos2; 2]> for Rect {
fn from([min, max]: [Pos2; 2]) -> Self {
Self { min, max }
}
}