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
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default)]
#[repr(C)]
pub struct Rect {
    pub min: Vec2,
    pub max: Vec2,
}

impl Rect {
    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        let half_size = vec2(width / 2.0, height / 2.0);
        let min = vec2(x, y) - half_size;
        let max = min + vec2(width, height);
        Self { min, max }
    }

    #[inline]
    pub fn width(&self) -> f32 {
        self.max.x - self.min.x
    }

    #[inline]
    pub fn height(&self) -> f32 {
        self.max.y - self.min.y
    }

    #[inline]
    pub fn size(&self) -> Vec2 {
        vec2(self.width(), self.height())
    }

    #[inline]
    pub fn left(&self) -> f32 {
        self.min.x
    }

    #[inline]
    pub fn right(&self) -> f32 {
        self.max.x
    }

    #[inline]
    pub fn top(&self) -> f32 {
        self.max.y
    }

    #[inline]
    pub fn bottom(&self) -> f32 {
        self.min.y
    }

    #[inline]
    pub fn top_left(&self) -> Vec2 {
        vec2(self.min.x, self.max.y)
    }

    #[inline]
    pub fn top_right(&self) -> Vec2 {
        vec2(self.max.x, self.max.y)
    }

    #[inline]
    pub fn bottom_left(&self) -> Vec2 {
        vec2(self.min.x, self.min.y)
    }

    #[inline]
    pub fn bottom_right(&self) -> Vec2 {
        vec2(self.max.x, self.min.y)
    }

    pub fn overlaps(&self, other: &Rect) -> bool {
        self.left() <= other.right()
            && self.right() >= other.left()
            && self.top() >= other.bottom()
            && self.bottom() <= other.top()
    }

    pub fn contains(&self, point: Vec2) -> bool {
        point.x <= self.right()
            && point.x >= self.left()
            && point.y >= self.bottom()
            && point.y <= self.top()
    }

    #[inline]
    pub fn center(&self) -> Vec2 {
        let half_size = self.size() / 2.0;
        self.min + half_size
    }

    pub fn min(&self) -> Vec2 {
        self.min
    }

    pub fn max(&self) -> Vec2 {
        self.max
    }
}