1#[derive(Clone, Copy, Debug, Default, PartialEq)]
6pub struct Point {
7 pub x: f32,
9 pub y: f32,
11}
12
13impl Point {
14 pub const fn new(x: f32, y: f32) -> Point {
16 Point { x, y }
17 }
18}
19
20#[derive(Clone, Copy, Debug, Default, PartialEq)]
22pub struct Size {
23 pub width: f32,
25 pub height: f32,
27}
28
29impl Size {
30 pub const fn new(width: f32, height: f32) -> Size {
32 Size { width, height }
33 }
34}
35
36#[derive(Clone, Copy, Debug, Default, PartialEq)]
38pub struct Rect {
39 pub x: f32,
41 pub y: f32,
43 pub width: f32,
45 pub height: f32,
47}
48
49impl Rect {
50 pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Rect {
52 Rect {
53 x,
54 y,
55 width,
56 height,
57 }
58 }
59
60 pub fn right(&self) -> f32 {
62 self.x + self.width
63 }
64
65 pub fn bottom(&self) -> f32 {
67 self.y + self.height
68 }
69
70 pub fn is_empty(&self) -> bool {
72 self.width <= 0.0 || self.height <= 0.0
73 }
74
75 pub fn contains(&self, point: Point) -> bool {
79 point.x >= self.x && point.x < self.right() && point.y >= self.y && point.y < self.bottom()
80 }
81
82 pub fn union(&self, other: Rect) -> Rect {
85 if other.is_empty() {
86 return *self;
87 }
88 if self.is_empty() {
89 return other;
90 }
91 let x = self.x.min(other.x);
92 let y = self.y.min(other.y);
93 Rect::new(
94 x,
95 y,
96 self.right().max(other.right()) - x,
97 self.bottom().max(other.bottom()) - y,
98 )
99 }
100
101 pub fn intersect(&self, other: Rect) -> Rect {
103 let x = self.x.max(other.x);
104 let y = self.y.max(other.y);
105 let right = self.right().min(other.right());
106 let bottom = self.bottom().min(other.bottom());
107 if right > x && bottom > y {
108 Rect::new(x, y, right - x, bottom - y)
109 } else {
110 Rect::default()
111 }
112 }
113}