atagen

monolith

public
Star0
Watch0
1df9b00ad0fd42883a8db6c71fdef8aed83d08c8
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git
crates/planner/src/geom.rsRust118 lines3.0 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 union(&self, other: &Rect) -> Rect {
30
        let (x, y) = (self.x.min(other.x), self.y.min(other.y));
31
        let r = self.right().max(other.right());
32
        let b = self.bottom().max(other.bottom());
33
        Rect::new(x, y, r - x, b - y)
34
    }
35
36
    pub fn center_x(&self) -> f64 {
37
        self.x + self.w / 2.0
38
    }
39
40
    pub fn center_y(&self) -> f64 {
41
        self.y + self.h / 2.0
42
    }
43
44
    pub fn lerp(&self, other: &Rect, s: f64) -> Rect {
45
        Rect::new(
46
            self.x + (other.x - self.x) * s,
47
            self.y + (other.y - self.y) * s,
48
            self.w + (other.w - self.w) * s,
49
            self.h + (other.h - self.h) * s,
50
        )
51
    }
52
53
    pub(crate) fn same_x(&self, o: &Rect) -> bool {
54
        (self.x - o.x).abs() < TOL && (self.w - o.w).abs() < TOL
55
    }
56
57
    pub(crate) fn same_y(&self, o: &Rect) -> bool {
58
        (self.y - o.y).abs() < TOL && (self.h - o.h).abs() < TOL
59
    }
60
61
    /// Takes the horizontal extent of `x_from` and the vertical extent of `y_from`.
62
    pub(crate) fn mixed(x_from: &Rect, y_from: &Rect) -> Rect {
63
        Rect::new(x_from.x, y_from.y, x_from.w, y_from.h)
64
    }
65
66
    pub(crate) fn bounding<'a>(rects: impl IntoIterator<Item = &'a Rect>) -> Rect {
67
        let (mut x0, mut y0, mut x1, mut y1) = (
68
            f64::INFINITY,
69
            f64::INFINITY,
70
            f64::NEG_INFINITY,
71
            f64::NEG_INFINITY,
72
        );
73
        for r in rects {
74
            x0 = x0.min(r.x);
75
            y0 = y0.min(r.y);
76
            x1 = x1.max(r.right());
77
            y1 = y1.max(r.bottom());
78
        }
79
        Rect::new(x0, y0, x1 - x0, y1 - y0)
80
    }
81
82
    pub(crate) fn expanded(&self, by: f64) -> Rect {
83
        Rect::new(
84
            self.x - by,
85
            self.y - by,
86
            self.w + 2.0 * by,
87
            self.h + 2.0 * by,
88
        )
89
    }
90
91
    pub(crate) fn intersects(&self, o: &Rect) -> bool {
92
        self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom()
93
    }
94
}
95
96
/// How a window changes between two keyframes.
97
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
98
pub(crate) struct Change {
99
    pub x: bool,
100
    pub y: bool,
101
}
102
103
impl Change {
104
    pub fn between(a: &Rect, b: &Rect) -> Self {
105
        Self {
106
            x: !a.same_x(b),
107
            y: !a.same_y(b),
108
        }
109
    }
110
111
    pub fn moves(&self) -> bool {
112
        self.x || self.y
113
    }
114
115
    pub fn both(&self) -> bool {
116
        self.x && self.y
117
    }
118
}