Skip to main content

xrack_layout/
geometry.rs

1#[derive(Clone, Copy, Debug, Default, PartialEq)]
2pub struct Point {
3    pub x: f32,
4    pub y: f32,
5}
6
7#[derive(Clone, Copy, Debug, Default, PartialEq)]
8pub struct Size {
9    pub width: f32,
10    pub height: f32,
11}
12
13impl Size {
14    pub const fn new(width: f32, height: f32) -> Self {
15        Self { width, height }
16    }
17}
18
19#[derive(Clone, Copy, Debug, Default, PartialEq)]
20pub struct Rect {
21    pub x: f32,
22    pub y: f32,
23    pub width: f32,
24    pub height: f32,
25}
26
27impl Rect {
28    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
29        Self {
30            x,
31            y,
32            width,
33            height,
34        }
35    }
36    pub fn contains(self, point: Point) -> bool {
37        self.width > 0.0
38            && self.height > 0.0
39            && point.x >= self.x
40            && point.y >= self.y
41            && point.x < self.x + self.width
42            && point.y < self.y + self.height
43    }
44    pub fn intersect(self, other: Self) -> Self {
45        let x = self.x.max(other.x);
46        let y = self.y.max(other.y);
47        Self::new(
48            x,
49            y,
50            (self.x + self.width).min(other.x + other.width).max(x) - x,
51            (self.y + self.height).min(other.y + other.height).max(y) - y,
52        )
53    }
54    pub fn inset(self, amount: f32) -> Self {
55        let x = amount.min(self.width * 0.5);
56        let y = amount.min(self.height * 0.5);
57        Self::new(
58            self.x + x,
59            self.y + y,
60            (self.width - 2.0 * x).max(0.0),
61            (self.height - 2.0 * y).max(0.0),
62        )
63    }
64    pub fn scaled(self, scale: f32) -> Self {
65        Self::new(
66            self.x * scale,
67            self.y * scale,
68            self.width * scale,
69            self.height * scale,
70        )
71    }
72}