yakui_core/geometry/
rect.rs1use glam::Vec2;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct Rect {
6 pos: Vec2,
7 size: Vec2,
8}
9
10impl Rect {
11 pub const ZERO: Self = Self {
13 pos: Vec2::ZERO,
14 size: Vec2::ZERO,
15 };
16
17 pub const ONE: Self = Self {
19 pos: Vec2::ZERO,
20 size: Vec2::ONE,
21 };
22
23 #[inline]
25 pub fn from_pos_size(pos: Vec2, size: Vec2) -> Self {
26 Self { pos, size }
27 }
28
29 #[inline]
32 pub fn pos(&self) -> Vec2 {
33 self.pos
34 }
35
36 #[inline]
38 pub fn size(&self) -> Vec2 {
39 self.size
40 }
41
42 #[inline]
44 pub fn max(&self) -> Vec2 {
45 self.pos + self.size
46 }
47
48 #[inline]
50 pub fn set_pos(&mut self, pos: Vec2) {
51 self.pos = pos;
52 }
53
54 #[inline]
56 pub fn set_size(&mut self, size: Vec2) {
57 self.size = size;
58 }
59
60 #[inline]
62 pub fn set_max(&mut self, max: Vec2) {
63 self.size = max - self.pos;
64 }
65
66 #[inline]
71 pub fn contains_point(&self, point: Vec2) -> bool {
72 point.x >= self.pos.x
73 && point.x <= self.pos.x + self.size.x
74 && point.y >= self.pos.y
75 && point.y <= self.pos.y + self.size.y
76 }
77
78 #[inline]
83 pub fn intersects(&self, other: &Self) -> bool {
84 let self_max = self.max();
85 let other_max = other.max();
86
87 let x_intersect = self.pos.x < other_max.x && self_max.x > other.pos.x;
88 let y_intersect = self.pos.y < other_max.y && self_max.y > other.pos.y;
89
90 x_intersect && y_intersect
91 }
92
93 #[inline]
95 pub fn div_vec2(&self, size: Vec2) -> Self {
96 Self::from_pos_size(self.pos / size, self.size / size)
97 }
98
99 #[inline]
101 pub fn constrain(mut self, other: Rect) -> Self {
102 let min = self.pos().max(other.pos());
103 let max = self.max().min(other.max());
104
105 self.set_pos(min);
106 self.set_max(max);
107 self
108 }
109}