astrelis_geometry/chart/
rect.rs1use glam::Vec2;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Rect {
8 pub x: f32,
10 pub y: f32,
12 pub width: f32,
14 pub height: f32,
16}
17
18impl Rect {
19 pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
21 Self {
22 x,
23 y,
24 width,
25 height,
26 }
27 }
28
29 pub fn from_pos_size(pos: Vec2, size: Vec2) -> Self {
31 Self {
32 x: pos.x,
33 y: pos.y,
34 width: size.x,
35 height: size.y,
36 }
37 }
38
39 pub fn position(&self) -> Vec2 {
41 Vec2::new(self.x, self.y)
42 }
43
44 pub fn size(&self) -> Vec2 {
46 Vec2::new(self.width, self.height)
47 }
48
49 pub fn center(&self) -> Vec2 {
51 Vec2::new(self.x + self.width * 0.5, self.y + self.height * 0.5)
52 }
53
54 pub fn inset(&self, padding: f32) -> Self {
56 Self {
57 x: self.x + padding,
58 y: self.y + padding,
59 width: (self.width - padding * 2.0).max(0.0),
60 height: (self.height - padding * 2.0).max(0.0),
61 }
62 }
63
64 pub fn contains(&self, point: Vec2) -> bool {
66 point.x >= self.x
67 && point.x <= self.x + self.width
68 && point.y >= self.y
69 && point.y <= self.y + self.height
70 }
71
72 pub fn right(&self) -> f32 {
74 self.x + self.width
75 }
76
77 pub fn bottom(&self) -> f32 {
79 self.y + self.height
80 }
81}