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
use glam::Vec2;

/// A bounding rectangle with floating point coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
    pos: Vec2,
    size: Vec2,
}

impl Rect {
    /// A zero-sized rectangle at the origin.
    pub const ZERO: Self = Self {
        pos: Vec2::ZERO,
        size: Vec2::ZERO,
    };

    /// A rectangle of size (1, 1) at the origin.
    pub const ONE: Self = Self {
        pos: Vec2::ZERO,
        size: Vec2::ONE,
    };

    /// Create a `Rect` from a position and size.
    #[inline]
    pub fn from_pos_size(pos: Vec2, size: Vec2) -> Self {
        Self { pos, size }
    }

    /// The position of the rectangle's upper-left corner. This is the minimum
    /// value enclosed by the rectangle.
    #[inline]
    pub fn pos(&self) -> Vec2 {
        self.pos
    }

    /// The size of the rectangle.
    #[inline]
    pub fn size(&self) -> Vec2 {
        self.size
    }

    /// The maximum value enclosed by the rectangle.
    #[inline]
    pub fn max(&self) -> Vec2 {
        self.pos + self.size
    }

    /// Set the rectangle's position.
    #[inline]
    pub fn set_pos(&mut self, pos: Vec2) {
        self.pos = pos;
    }

    /// Set the rectangle's size.
    #[inline]
    pub fn set_size(&mut self, size: Vec2) {
        self.size = size;
    }

    /// Tells whether the given point is contained within the rectangle.
    ///
    /// If the point lies on the rectangle's boundary, it is considered
    /// **inside**.
    #[inline]
    pub fn contains_point(&self, point: Vec2) -> bool {
        point.x >= self.pos.x
            && point.x <= self.pos.x + self.size.x
            && point.y >= self.pos.y
            && point.y <= self.pos.y + self.size.y
    }

    /// Tells whether two rectangles intersect.
    ///
    /// If the rectangles touch but do not overlap, they are considered **not
    /// intersecting**.
    #[inline]
    pub fn intersects(&self, other: &Self) -> bool {
        let self_max = self.max();
        let other_max = other.max();

        let x_intersect = self.pos.x < other_max.x && self_max.x > other.pos.x;
        let y_intersect = self.pos.y < other_max.y && self_max.y > other.pos.y;

        x_intersect && y_intersect
    }

    /// Scale the rectangle by dividing it by a vector.
    #[inline]
    pub fn div_vec2(&self, size: Vec2) -> Self {
        Self::from_pos_size(self.pos / size, self.size / size)
    }
}