1use std::ops::{Add, Sub};
6
7#[derive(Clone, Copy, Debug, Default, PartialEq)]
9pub struct PointF {
10 pub x: f32,
11 pub y: f32,
12}
13
14impl PointF {
15 pub const ZERO: PointF = PointF::new(0.0, 0.0);
16
17 pub const fn new(x: f32, y: f32) -> Self {
18 Self { x, y }
19 }
20}
21
22impl Add for PointF {
23 type Output = PointF;
24 fn add(self, other: PointF) -> PointF {
25 PointF::new(self.x + other.x, self.y + other.y)
26 }
27}
28
29impl Sub for PointF {
30 type Output = PointF;
31 fn sub(self, other: PointF) -> PointF {
32 PointF::new(self.x - other.x, self.y - other.y)
33 }
34}
35
36#[derive(Clone, Copy, Debug, Default, PartialEq)]
38pub struct SizeF {
39 pub width: f32,
40 pub height: f32,
41}
42
43impl SizeF {
44 pub const ZERO: SizeF = SizeF::new(0.0, 0.0);
45
46 pub const fn new(width: f32, height: f32) -> Self {
47 Self { width, height }
48 }
49}
50
51#[derive(Clone, Copy, Debug, Default, PartialEq)]
53pub struct RectF {
54 pub x: f32,
55 pub y: f32,
56 pub width: f32,
57 pub height: f32,
58}
59
60impl RectF {
61 pub const ZERO: RectF = RectF::new(0.0, 0.0, 0.0, 0.0);
62
63 pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
64 Self { x, y, width, height }
65 }
66
67 pub const fn from_size(size: SizeF) -> Self {
69 Self::new(0.0, 0.0, size.width, size.height)
70 }
71
72 pub fn right(&self) -> f32 {
73 self.x + self.width
74 }
75
76 pub fn bottom(&self) -> f32 {
77 self.y + self.height
78 }
79
80 pub fn origin(&self) -> PointF {
81 PointF::new(self.x, self.y)
82 }
83
84 pub fn size(&self) -> SizeF {
85 SizeF::new(self.width, self.height)
86 }
87
88 pub fn center(&self) -> PointF {
89 PointF::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
90 }
91
92 pub fn is_empty(&self) -> bool {
93 self.width <= 0.0 || self.height <= 0.0
94 }
95
96 pub fn contains(&self, p: PointF) -> bool {
97 p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
98 }
99
100 pub fn union(&self, other: RectF) -> RectF {
103 let (x, y) = (self.x.min(other.x), self.y.min(other.y));
104 let (r, b) = (self.right().max(other.right()), self.bottom().max(other.bottom()));
105 RectF::new(x, y, r - x, b - y)
106 }
107
108 pub fn contains_rect(&self, other: RectF) -> bool {
109 other.x >= self.x && other.y >= self.y && other.right() <= self.right() && other.bottom() <= self.bottom()
110 }
111
112 pub fn intersects(&self, other: RectF) -> bool {
114 self.x < other.right() && other.x < self.right() && self.y < other.bottom() && other.y < self.bottom()
115 }
116
117 pub fn from_points(a: PointF, b: PointF) -> Self {
119 Self::new(a.x.min(b.x), a.y.min(b.y), (a.x - b.x).abs(), (a.y - b.y).abs())
120 }
121
122 pub fn offset(self, dx: f32, dy: f32) -> Self {
123 Self::new(self.x + dx, self.y + dy, self.width, self.height)
124 }
125
126 pub fn inflate(self, d: f32) -> Self {
128 Self::new(self.x - d, self.y - d, self.width + 2.0 * d, self.height + 2.0 * d)
129 }
130
131 pub fn deflate(self, b: Bounds) -> Self {
133 Self::new(
134 self.x + b.left,
135 self.y + b.top,
136 (self.width - b.horizontal()).max(0.0),
137 (self.height - b.vertical()).max(0.0),
138 )
139 }
140
141 pub fn intersect(self, other: RectF) -> Option<RectF> {
142 let x = self.x.max(other.x);
143 let y = self.y.max(other.y);
144 let r = self.right().min(other.right());
145 let b = self.bottom().min(other.bottom());
146 (r > x && b > y).then(|| RectF::new(x, y, r - x, b - y))
147 }
148}
149
150#[derive(Clone, Copy, Debug, Default, PartialEq)]
152pub struct Bounds {
153 pub left: f32,
154 pub top: f32,
155 pub right: f32,
156 pub bottom: f32,
157}
158
159impl Bounds {
160 pub const ZERO: Bounds = Bounds::new(0.0, 0.0, 0.0, 0.0);
161
162 pub const fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
163 Self { left, top, right, bottom }
164 }
165
166 pub const fn all(v: f32) -> Self {
168 Self::new(v, v, v, v)
169 }
170
171 pub const fn symmetric(h: f32, v: f32) -> Self {
173 Self::new(h, v, h, v)
174 }
175
176 pub fn scaled(self, factor: f32) -> Self {
178 Self::new(self.left * factor, self.top * factor, self.right * factor, self.bottom * factor)
179 }
180
181 pub fn horizontal(&self) -> f32 {
183 self.left + self.right
184 }
185
186 pub fn vertical(&self) -> f32 {
188 self.top + self.bottom
189 }
190}
191
192impl From<f32> for Bounds {
193 fn from(v: f32) -> Self {
194 Bounds::all(v)
195 }
196}
197
198impl From<(f32, f32)> for Bounds {
199 fn from((h, v): (f32, f32)) -> Self {
200 Bounds::symmetric(h, v)
201 }
202}
203
204impl From<[f32; 4]> for Bounds {
205 fn from([l, t, r, b]: [f32; 4]) -> Self {
206 Bounds::new(l, t, r, b)
207 }
208}