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
//! Rectangular bounds utility for chart rendering.
use glam::Vec2;
/// Rectangular bounds for chart rendering.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
/// X position (left)
pub x: f32,
/// Y position (top)
pub y: f32,
/// Width
pub width: f32,
/// Height
pub height: f32,
}
impl Rect {
/// Create a new rect.
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width,
height,
}
}
/// Create from position and size.
pub fn from_pos_size(pos: Vec2, size: Vec2) -> Self {
Self {
x: pos.x,
y: pos.y,
width: size.x,
height: size.y,
}
}
/// Get the position as a Vec2.
pub fn position(&self) -> Vec2 {
Vec2::new(self.x, self.y)
}
/// Get the size as a Vec2.
pub fn size(&self) -> Vec2 {
Vec2::new(self.width, self.height)
}
/// Get the center point.
pub fn center(&self) -> Vec2 {
Vec2::new(self.x + self.width * 0.5, self.y + self.height * 0.5)
}
/// Inset the rect by a padding amount.
pub fn inset(&self, padding: f32) -> Self {
Self {
x: self.x + padding,
y: self.y + padding,
width: (self.width - padding * 2.0).max(0.0),
height: (self.height - padding * 2.0).max(0.0),
}
}
/// Check if a point is inside the rect.
pub fn contains(&self, point: Vec2) -> bool {
point.x >= self.x
&& point.x <= self.x + self.width
&& point.y >= self.y
&& point.y <= self.y + self.height
}
/// Get the right edge.
pub fn right(&self) -> f32 {
self.x + self.width
}
/// Get the bottom edge.
pub fn bottom(&self) -> f32 {
self.y + self.height
}
}