atagen

monolith

public
Star0
Watch0
0e6ab493312525b26d667aca671f831d0e0fd74a
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git
crates/planner/src/geom.rsRust111 lines2.7 KB
1
#[derive(Clone, Copy, Debug, PartialEq, Default)]
2
pub struct Rect {
3
    pub x: f64,
4
    pub y: f64,
5
    pub w: f64,
6
    pub h: f64,
7
}
8
9
pub(crate) const TOL: f64 = 0.05;
10
11
impl Rect {
12
    pub const fn new(x: f64, y: f64, w: f64, h: f64) -> Self {
13
        Self { x, y, w, h }
14
    }
15
16
    pub fn right(&self) -> f64 {
17
        self.x + self.w
18
    }
19
20
    pub fn bottom(&self) -> f64 {
21
        self.y + self.h
22
    }
23
24
    /// Whether the point lies inside, counting the left and top edges only.
25
    pub fn contains(&self, x: f64, y: f64) -> bool {
26
        x >= self.x && y >= self.y && x < self.right() && y < self.bottom()
27
    }
28
29
    pub fn center_x(&self) -> f64 {
30
        self.x + self.w / 2.0
31
    }
32
33
    pub fn center_y(&self) -> f64 {
34
        self.y + self.h / 2.0
35
    }
36
37
    pub fn lerp(&self, other: &Rect, s: f64) -> Rect {
38
        Rect::new(
39
            self.x + (other.x - self.x) * s,
40
            self.y + (other.y - self.y) * s,
41
            self.w + (other.w - self.w) * s,
42
            self.h + (other.h - self.h) * s,
43
        )
44
    }
45
46
    pub(crate) fn same_x(&self, o: &Rect) -> bool {
47
        (self.x - o.x).abs() < TOL && (self.w - o.w).abs() < TOL
48
    }
49
50
    pub(crate) fn same_y(&self, o: &Rect) -> bool {
51
        (self.y - o.y).abs() < TOL && (self.h - o.h).abs() < TOL
52
    }
53
54
    /// Takes the horizontal extent of `x_from` and the vertical extent of `y_from`.
55
    pub(crate) fn mixed(x_from: &Rect, y_from: &Rect) -> Rect {
56
        Rect::new(x_from.x, y_from.y, x_from.w, y_from.h)
57
    }
58
59
    pub(crate) fn bounding<'a>(rects: impl IntoIterator<Item = &'a Rect>) -> Rect {
60
        let (mut x0, mut y0, mut x1, mut y1) = (
61
            f64::INFINITY,
62
            f64::INFINITY,
63
            f64::NEG_INFINITY,
64
            f64::NEG_INFINITY,
65
        );
66
        for r in rects {
67
            x0 = x0.min(r.x);
68
            y0 = y0.min(r.y);
69
            x1 = x1.max(r.right());
70
            y1 = y1.max(r.bottom());
71
        }
72
        Rect::new(x0, y0, x1 - x0, y1 - y0)
73
    }
74
75
    pub(crate) fn expanded(&self, by: f64) -> Rect {
76
        Rect::new(
77
            self.x - by,
78
            self.y - by,
79
            self.w + 2.0 * by,
80
            self.h + 2.0 * by,
81
        )
82
    }
83
84
    pub(crate) fn intersects(&self, o: &Rect) -> bool {
85
        self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom()
86
    }
87
}
88
89
/// How a window changes between two keyframes.
90
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
91
pub(crate) struct Change {
92
    pub x: bool,
93
    pub y: bool,
94
}
95
96
impl Change {
97
    pub fn between(a: &Rect, b: &Rect) -> Self {
98
        Self {
99
            x: !a.same_x(b),
100
            y: !a.same_y(b),
101
        }
102
    }
103
104
    pub fn moves(&self) -> bool {
105
        self.x || self.y
106
    }
107
108
    pub fn both(&self) -> bool {
109
        self.x && self.y
110
    }
111
}