1use std::ops::{Add, Sub};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq)]
4pub struct Point {
5 pub x: f32,
6 pub y: f32,
7}
8
9impl Point {
10 pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
11
12 pub const fn new(x: f32, y: f32) -> Self {
13 Point { x, y }
14 }
15
16 pub const fn to_array(self) -> [f32; 2] {
17 [self.x, self.y]
18 }
19}
20
21impl Add for Point {
22 type Output = Point;
23 fn add(self, rhs: Point) -> Point {
24 Point::new(self.x + rhs.x, self.y + rhs.y)
25 }
26}
27
28impl Sub for Point {
29 type Output = Point;
30 fn sub(self, rhs: Point) -> Point {
31 Point::new(self.x - rhs.x, self.y - rhs.y)
32 }
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq)]
36pub struct Size {
37 pub width: f32,
38 pub height: f32,
39}
40
41impl Size {
42 pub const ZERO: Size = Size {
43 width: 0.0,
44 height: 0.0,
45 };
46
47 pub const fn new(width: f32, height: f32) -> Self {
48 Size { width, height }
49 }
50}
51
52#[derive(Clone, Copy, Debug, Default, PartialEq)]
53pub struct Rect {
54 pub origin: Point,
55 pub size: Size,
56}
57
58impl Rect {
59 pub const ZERO: Rect = Rect::new(0.0, 0.0, 0.0, 0.0);
60 pub const EVERYTHING: Rect = Rect::new(-1.0e7, -1.0e7, 2.0e7, 2.0e7);
61
62 pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
63 Rect {
64 origin: Point::new(x, y),
65 size: Size::new(width, height),
66 }
67 }
68
69 pub fn from_min_max(min: Point, max: Point) -> Self {
70 Rect::new(
71 min.x,
72 min.y,
73 (max.x - min.x).max(0.0),
74 (max.y - min.y).max(0.0),
75 )
76 }
77
78 pub fn max(&self) -> Point {
79 Point::new(
80 self.origin.x + self.size.width,
81 self.origin.y + self.size.height,
82 )
83 }
84
85 pub fn center(&self) -> Point {
86 Point::new(
87 self.origin.x + self.size.width * 0.5,
88 self.origin.y + self.size.height * 0.5,
89 )
90 }
91
92 pub fn is_empty(&self) -> bool {
93 self.size.width <= 0.0 || self.size.height <= 0.0
94 }
95
96 pub fn contains(&self, point: Point) -> bool {
97 let max = self.max();
98 point.x >= self.origin.x && point.y >= self.origin.y && point.x < max.x && point.y < max.y
99 }
100
101 pub fn intersect(&self, other: &Rect) -> Rect {
102 let (a, b) = (self.max(), other.max());
103 Rect::from_min_max(
104 Point::new(
105 self.origin.x.max(other.origin.x),
106 self.origin.y.max(other.origin.y),
107 ),
108 Point::new(a.x.min(b.x), a.y.min(b.y)),
109 )
110 }
111
112 pub fn union(&self, other: &Rect) -> Rect {
113 let (a, b) = (self.max(), other.max());
114 Rect::from_min_max(
115 Point::new(
116 self.origin.x.min(other.origin.x),
117 self.origin.y.min(other.origin.y),
118 ),
119 Point::new(a.x.max(b.x), a.y.max(b.y)),
120 )
121 }
122
123 pub fn translate(&self, by: Point) -> Rect {
124 Rect {
125 origin: self.origin + by,
126 size: self.size,
127 }
128 }
129
130 pub fn inflate(&self, amount: f32) -> Rect {
131 Rect::new(
132 self.origin.x - amount,
133 self.origin.y - amount,
134 (self.size.width + amount * 2.0).max(0.0),
135 (self.size.height + amount * 2.0).max(0.0),
136 )
137 }
138
139 pub fn to_array(self) -> [f32; 4] {
140 [
141 self.origin.x,
142 self.origin.y,
143 self.size.width,
144 self.size.height,
145 ]
146 }
147}
148
149#[derive(Clone, Copy, Debug, Default, PartialEq)]
150pub struct Corners {
151 pub top_left: f32,
152 pub top_right: f32,
153 pub bottom_right: f32,
154 pub bottom_left: f32,
155}
156
157impl Corners {
158 pub const ZERO: Corners = Corners::all(0.0);
159
160 pub const fn all(value: f32) -> Self {
161 Corners {
162 top_left: value,
163 top_right: value,
164 bottom_right: value,
165 bottom_left: value,
166 }
167 }
168
169 pub fn map(&self, f: impl Fn(f32) -> f32) -> Corners {
170 Corners {
171 top_left: f(self.top_left),
172 top_right: f(self.top_right),
173 bottom_right: f(self.bottom_right),
174 bottom_left: f(self.bottom_left),
175 }
176 }
177
178 pub fn to_array(self) -> [f32; 4] {
179 [
180 self.top_left,
181 self.top_right,
182 self.bottom_right,
183 self.bottom_left,
184 ]
185 }
186}
187
188#[derive(Clone, Copy, Debug, Default, PartialEq)]
189pub struct Edges {
190 pub top: f32,
191 pub right: f32,
192 pub bottom: f32,
193 pub left: f32,
194}
195
196impl Edges {
197 pub const ZERO: Edges = Edges::all(0.0);
198
199 pub const fn all(value: f32) -> Self {
200 Edges {
201 top: value,
202 right: value,
203 bottom: value,
204 left: value,
205 }
206 }
207
208 pub fn is_zero(&self) -> bool {
209 *self == Edges::ZERO
210 }
211
212 pub fn to_array(self) -> [f32; 4] {
213 [self.top, self.right, self.bottom, self.left]
214 }
215}
216
217#[derive(Clone, Copy, Debug, PartialEq)]
218pub struct Affine {
219 pub a: f32,
220 pub b: f32,
221 pub c: f32,
222 pub d: f32,
223 pub tx: f32,
224 pub ty: f32,
225}
226
227impl Default for Affine {
228 fn default() -> Self {
229 Affine::IDENTITY
230 }
231}
232
233impl Affine {
234 pub const IDENTITY: Affine = Affine {
235 a: 1.0,
236 b: 0.0,
237 c: 0.0,
238 d: 1.0,
239 tx: 0.0,
240 ty: 0.0,
241 };
242
243 pub const fn translate(x: f32, y: f32) -> Self {
244 Affine {
245 tx: x,
246 ty: y,
247 ..Affine::IDENTITY
248 }
249 }
250
251 pub const fn scale(x: f32, y: f32) -> Self {
252 Affine {
253 a: x,
254 d: y,
255 ..Affine::IDENTITY
256 }
257 }
258
259 pub fn rotate(radians: f32) -> Self {
260 let (sin, cos) = radians.sin_cos();
261 Affine {
262 a: cos,
263 b: sin,
264 c: -sin,
265 d: cos,
266 tx: 0.0,
267 ty: 0.0,
268 }
269 }
270
271 pub fn then(&self, next: &Affine) -> Affine {
272 Affine {
273 a: next.a * self.a + next.c * self.b,
274 b: next.b * self.a + next.d * self.b,
275 c: next.a * self.c + next.c * self.d,
276 d: next.b * self.c + next.d * self.d,
277 tx: next.a * self.tx + next.c * self.ty + next.tx,
278 ty: next.b * self.tx + next.d * self.ty + next.ty,
279 }
280 }
281
282 pub fn about(&self, center: Point) -> Affine {
283 Affine::translate(-center.x, -center.y)
284 .then(self)
285 .then(&Affine::translate(center.x, center.y))
286 }
287
288 pub fn apply(&self, p: Point) -> Point {
289 Point::new(
290 self.a * p.x + self.c * p.y + self.tx,
291 self.b * p.x + self.d * p.y + self.ty,
292 )
293 }
294
295 pub fn apply_rect(&self, rect: &Rect) -> Rect {
296 let (min, max) = (rect.origin, rect.max());
297 let corners =
298 [min, Point::new(max.x, min.y), max, Point::new(min.x, max.y)].map(|p| self.apply(p));
299 let fold = |f: fn(f32, f32) -> f32, start: f32| {
300 corners.iter().fold(Point::new(start, start), |acc, p| {
301 Point::new(f(acc.x, p.x), f(acc.y, p.y))
302 })
303 };
304 Rect::from_min_max(
305 fold(f32::min, f32::INFINITY),
306 fold(f32::max, f32::NEG_INFINITY),
307 )
308 }
309
310 pub fn inverse(&self) -> Option<Affine> {
311 let det = self.a * self.d - self.b * self.c;
312 if det.abs() < 1e-9 {
313 return None;
314 }
315 let (a, b, c, d) = (self.d / det, -self.b / det, -self.c / det, self.a / det);
316 Some(Affine {
317 a,
318 b,
319 c,
320 d,
321 tx: -(a * self.tx + c * self.ty),
322 ty: -(b * self.tx + d * self.ty),
323 })
324 }
325}