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
use crate::quadtree::aabb::AABB;
use crate::quadtree::centered_aabb::CenteredAABB;

/// A rectangle describing the extents of the QuadTree.
///
/// # Remarks
/// Only the tree node stores its extents. Bounding boxes for sub-nodes are computed on the fly.
#[derive(Debug, Copy, Clone)]
pub struct QuadRect {
    l: i32,
    t: i32,
    hx: i32,
    hy: i32,
}

impl QuadRect {
    pub fn new(left: i32, top: i32, width: i32, height: i32) -> Self {
        Self {
            l: left,
            t: top,
            hx: width,
            hy: height,
        }
    }

    pub fn contains(&self, rect: &AABB) -> bool {
        let mx = (rect.tl.x + rect.br.x) >> 1;
        let my = (rect.tl.y + rect.br.y) >> 1;

        let r = self.l + self.hx;
        let b = self.t + self.hy;
        mx >= self.l && mx <= r && my >= self.t && my <= b
    }
}

impl Default for QuadRect {
    fn default() -> Self {
        QuadRect {
            l: i32::MIN >> 1,
            t: i32::MIN >> 1,
            hx: i32::MAX,
            hy: i32::MAX,
        }
    }
}

impl Into<AABB> for QuadRect {
    fn into(self) -> AABB {
        AABB::new(self.l, self.t, self.l + self.hx, self.t + self.hy)
    }
}

impl Into<CenteredAABB> for QuadRect {
    fn into(self) -> CenteredAABB {
        CenteredAABB::from_ltwh(self.l, self.t, self.hx, self.hy)
    }
}

impl Into<CenteredAABB> for &QuadRect {
    fn into(self) -> CenteredAABB {
        CenteredAABB::from_ltwh(self.l, self.t, self.hx, self.hy)
    }
}