1use std::fmt::Display;
5
6use crate::{Position, Size};
7
8#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
9pub struct Rect {
10 position: Position,
11 size: Size,
12}
13
14impl Rect {
15 #[must_use]
16 pub const fn new(position: Position, size: Size) -> Self {
17 Self {
18 position,
19 size,
20 }
21 }
22
23 #[must_use]
24 pub const fn position(&self) -> Position {
25 self.position
26 }
27
28 #[must_use]
29 pub fn middle(&self) -> Position {
30 Position::new(
31 self.x() + self.width() / 2.0,
32 self.y() + self.height() / 2.0
33 )
34 }
35
36 #[must_use]
37 pub const fn x(&self) -> f64 {
38 self.position().x()
39 }
40
41 #[must_use]
42 pub const fn y(&self) -> f64 {
43 self.position().y()
44 }
45
46 #[must_use]
47 pub const fn size(&self) -> Size {
48 self.size
49 }
50
51 #[must_use]
52 pub const fn width(&self) -> f64 {
53 self.size().width()
54 }
55
56 #[must_use]
57 pub const fn height(&self) -> f64 {
58 self.size().height()
59 }
60}
61
62impl From<(Position, Size)> for Rect {
63 fn from(value: (Position, Size)) -> Self {
64 Self::new(value.0, value.1)
65 }
66}
67
68impl Display for Rect {
69 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70 f.write_fmt(format_args!("rectangle at {} sized {}", self.position, self.size))
71 }
72}