1use core::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub struct Point<T = f32> {
13 pub x: T,
15 pub y: T,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct Vector<T = f32> {
23 pub x: T,
25 pub y: T,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
32pub struct Size<T = f32> {
33 pub width: T,
35 pub height: T,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
41#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42pub struct Rect<T = f32> {
43 pub x: T,
45 pub y: T,
47 pub width: T,
49 pub height: T,
51}
52
53impl<T> Point<T> {
54 pub const fn new(x: T, y: T) -> Self {
56 Self { x, y }
57 }
58}
59
60impl Point {
61 pub const ORIGIN: Self = Self::new(0.0, 0.0);
63
64 #[must_use]
66 pub fn distance(self, other: Self) -> f32 {
67 (self - other).length()
68 }
69}
70
71impl<T> Vector<T> {
72 pub const fn new(x: T, y: T) -> Self {
74 Self { x, y }
75 }
76}
77
78impl Vector {
79 pub const ZERO: Self = Self::new(0.0, 0.0);
81
82 #[must_use]
84 pub fn length(self) -> f32 {
85 self.x.hypot(self.y)
86 }
87}
88
89impl<T> Size<T> {
90 pub const fn new(width: T, height: T) -> Self {
92 Self { width, height }
93 }
94}
95
96impl Size {
97 pub const ZERO: Self = Self::new(0.0, 0.0);
99
100 #[must_use]
102 pub fn area(self) -> f32 {
103 self.width * self.height
104 }
105}
106
107impl<T: Copy> Rect<T> {
108 pub const fn new(position: Point<T>, size: Size<T>) -> Self {
110 Self {
111 x: position.x,
112 y: position.y,
113 width: size.width,
114 height: size.height,
115 }
116 }
117
118 pub const fn position(self) -> Point<T> {
120 Point::new(self.x, self.y)
121 }
122
123 pub const fn size(self) -> Size<T> {
125 Size::new(self.width, self.height)
126 }
127}
128
129impl Rect {
130 pub const ZERO: Self = Self::new(Point::ORIGIN, Size::ZERO);
132
133 #[must_use]
135 pub fn center(self) -> Point {
136 Point::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
137 }
138
139 #[must_use]
141 pub fn area(self) -> f32 {
142 self.size().area()
143 }
144
145 #[must_use]
148 pub fn contains(self, point: Point) -> bool {
149 point.x >= self.x
150 && point.y >= self.y
151 && point.x < self.x + self.width
152 && point.y < self.y + self.height
153 }
154
155 #[must_use]
157 pub fn intersection(self, other: Self) -> Option<Self> {
158 let left = self.x.max(other.x);
159 let top = self.y.max(other.y);
160 let right = (self.x + self.width).min(other.x + other.width);
161 let bottom = (self.y + self.height).min(other.y + other.height);
162 if right <= left || bottom <= top {
163 return None;
164 }
165 Some(Self {
166 x: left,
167 y: top,
168 width: right - left,
169 height: bottom - top,
170 })
171 }
172
173 #[must_use]
175 pub fn is_within(self, other: Self) -> bool {
176 self.x >= other.x
177 && self.y >= other.y
178 && self.x + self.width <= other.x + other.width
179 && self.y + self.height <= other.y + other.height
180 }
181}
182
183impl<T> From<(T, T)> for Point<T> {
184 fn from((x, y): (T, T)) -> Self {
185 Self::new(x, y)
186 }
187}
188
189impl<T> From<(T, T)> for Vector<T> {
190 fn from((x, y): (T, T)) -> Self {
191 Self::new(x, y)
192 }
193}
194
195impl<T> From<(T, T)> for Size<T> {
196 fn from((width, height): (T, T)) -> Self {
197 Self::new(width, height)
198 }
199}
200
201impl<T: Add<Output = T>> Add<Vector<T>> for Point<T> {
202 type Output = Self;
203
204 fn add(self, v: Vector<T>) -> Self {
205 Self::new(self.x + v.x, self.y + v.y)
206 }
207}
208
209impl<T: Sub<Output = T>> Sub<Vector<T>> for Point<T> {
210 type Output = Self;
211
212 fn sub(self, v: Vector<T>) -> Self {
213 Self::new(self.x - v.x, self.y - v.y)
214 }
215}
216
217impl<T: Sub<Output = T>> Sub for Point<T> {
218 type Output = Vector<T>;
219
220 fn sub(self, other: Self) -> Vector<T> {
221 Vector::new(self.x - other.x, self.y - other.y)
222 }
223}
224
225impl<T: AddAssign> AddAssign<Vector<T>> for Point<T> {
226 fn add_assign(&mut self, v: Vector<T>) {
227 self.x += v.x;
228 self.y += v.y;
229 }
230}
231
232impl<T: SubAssign> SubAssign<Vector<T>> for Point<T> {
233 fn sub_assign(&mut self, v: Vector<T>) {
234 self.x -= v.x;
235 self.y -= v.y;
236 }
237}
238
239impl<T: Add<Output = T>> Add for Vector<T> {
240 type Output = Self;
241
242 fn add(self, other: Self) -> Self {
243 Self::new(self.x + other.x, self.y + other.y)
244 }
245}
246
247impl<T: Sub<Output = T>> Sub for Vector<T> {
248 type Output = Self;
249
250 fn sub(self, other: Self) -> Self {
251 Self::new(self.x - other.x, self.y - other.y)
252 }
253}
254
255impl<T: Neg<Output = T>> Neg for Vector<T> {
256 type Output = Self;
257
258 fn neg(self) -> Self {
259 Self::new(-self.x, -self.y)
260 }
261}
262
263impl<T: Mul<Output = T> + Copy> Mul<T> for Vector<T> {
264 type Output = Self;
265
266 fn mul(self, factor: T) -> Self {
267 Self::new(self.x * factor, self.y * factor)
268 }
269}
270
271impl<T: Mul<Output = T> + Copy> Mul<T> for Size<T> {
272 type Output = Self;
273
274 fn mul(self, factor: T) -> Self {
275 Self::new(self.width * factor, self.height * factor)
276 }
277}
278
279impl<T: Add<Output = T>> Add<Vector<T>> for Rect<T> {
280 type Output = Self;
281
282 fn add(self, v: Vector<T>) -> Self {
283 Self {
284 x: self.x + v.x,
285 y: self.y + v.y,
286 width: self.width,
287 height: self.height,
288 }
289 }
290}
291
292impl<T: Sub<Output = T>> Sub<Vector<T>> for Rect<T> {
293 type Output = Self;
294
295 fn sub(self, v: Vector<T>) -> Self {
296 Self {
297 x: self.x - v.x,
298 y: self.y - v.y,
299 width: self.width,
300 height: self.height,
301 }
302 }
303}
304
305#[cfg(test)]
306mod tests {
307 use super::*;
308
309 #[test]
310 fn point_and_vector_arithmetic() {
311 let p = Point::new(1.0, 2.0);
312 let v = Vector::new(3.0, 4.0);
313 assert_eq!(p + v, Point::new(4.0, 6.0));
314 assert_eq!(p + v - v, p);
315 assert_eq!(Point::new(4.0, 6.0) - p, v);
316 assert_eq!(v + v, Vector::new(6.0, 8.0));
317 assert_eq!(v - v, Vector::ZERO);
318 assert_eq!(-v, Vector::new(-3.0, -4.0));
319 assert_eq!(v * 2.0, Vector::new(6.0, 8.0));
320 assert_eq!(v.length(), 5.0);
321 assert_eq!(Point::ORIGIN.distance(Point::new(3.0, 4.0)), 5.0);
322 let mut moved = p;
323 moved += v;
324 moved -= Vector::new(1.0, 1.0);
325 assert_eq!(moved, Point::new(3.0, 5.0));
326 }
327
328 #[test]
329 fn integer_geometry_works_too() {
330 let p: Point<i32> = Point::new(1, 2) + Vector::new(1, 1);
331 assert_eq!(p, Point::new(2, 3));
332 assert_eq!(Size::new(2_u32, 3) * 2, Size::new(4, 6));
333 }
334
335 #[test]
336 fn tuples_convert() {
337 assert_eq!(Point::from((1.0, 2.0)), Point::new(1.0, 2.0));
338 assert_eq!(Vector::from((1.0, 2.0)), Vector::new(1.0, 2.0));
339 assert_eq!(Size::from((1.0, 2.0)), Size::new(1.0, 2.0));
340 }
341
342 #[test]
343 fn rect_parts_and_centre() {
344 let r = Rect::new(Point::new(10.0, 20.0), Size::new(30.0, 40.0));
345 assert_eq!(r.position(), Point::new(10.0, 20.0));
346 assert_eq!(r.size(), Size::new(30.0, 40.0));
347 assert_eq!(r.center(), Point::new(25.0, 40.0));
348 assert_eq!(r.area(), 1200.0);
349 assert_eq!(
350 r + Vector::new(1.0, 1.0),
351 Rect::new(Point::new(11.0, 21.0), r.size())
352 );
353 assert_eq!(r + Vector::new(1.0, 1.0) - Vector::new(1.0, 1.0), r);
354 assert_eq!(Rect::ZERO.area(), 0.0);
355 }
356
357 #[test]
358 fn contains_is_half_open() {
359 let r = Rect::new(Point::ORIGIN, Size::new(10.0, 10.0));
360 assert!(r.contains(Point::new(0.0, 0.0)));
361 assert!(r.contains(Point::new(9.9, 9.9)));
362 assert!(!r.contains(Point::new(10.0, 5.0)));
363 assert!(!r.contains(Point::new(5.0, 10.0)));
364 assert!(!r.contains(Point::new(-0.1, 5.0)));
365 }
366
367 #[test]
368 fn intersection_of_overlapping_touching_and_apart() {
369 let a = Rect::new(Point::ORIGIN, Size::new(10.0, 10.0));
370 let b = Rect::new(Point::new(5.0, 5.0), Size::new(10.0, 10.0));
371 assert_eq!(
372 a.intersection(b),
373 Some(Rect::new(Point::new(5.0, 5.0), Size::new(5.0, 5.0)))
374 );
375 let touching = Rect::new(Point::new(10.0, 0.0), Size::new(5.0, 5.0));
376 assert_eq!(a.intersection(touching), None);
377 let apart = Rect::new(Point::new(50.0, 50.0), Size::new(5.0, 5.0));
378 assert_eq!(a.intersection(apart), None);
379 }
380
381 #[test]
382 fn is_within_includes_equal_edges() {
383 let outer = Rect::new(Point::ORIGIN, Size::new(10.0, 10.0));
384 assert!(outer.is_within(outer));
385 assert!(Rect::new(Point::new(1.0, 1.0), Size::new(2.0, 2.0)).is_within(outer));
386 assert!(!Rect::new(Point::new(9.0, 9.0), Size::new(2.0, 2.0)).is_within(outer));
387 }
388}