sightloom_core/
geometry.rs1use crate::GeometryError;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub struct Point {
8 x: f32,
9 y: f32,
10}
11
12impl Point {
13 pub fn new(x: f32, y: f32) -> Result<Self, GeometryError> {
20 if !x.is_finite() || !y.is_finite() {
21 return Err(GeometryError::NonFinite);
22 }
23
24 Ok(Self { x, y })
25 }
26
27 #[must_use]
29 pub const fn x(self) -> f32 {
30 self.x
31 }
32
33 #[must_use]
35 pub const fn y(self) -> f32 {
36 self.y
37 }
38}
39
40#[derive(Clone, Copy, Debug, Default, PartialEq)]
42pub struct Rect {
43 left: f32,
44 top: f32,
45 right: f32,
46 bottom: f32,
47}
48
49impl Rect {
50 pub fn new(left: f32, top: f32, right: f32, bottom: f32) -> Result<Self, GeometryError> {
58 if !left.is_finite() || !top.is_finite() || !right.is_finite() || !bottom.is_finite() {
59 return Err(GeometryError::NonFinite);
60 }
61 if right < left || bottom < top {
62 return Err(GeometryError::InvertedBounds);
63 }
64
65 Ok(Self {
66 left,
67 top,
68 right,
69 bottom,
70 })
71 }
72
73 #[must_use]
75 pub const fn left(self) -> f32 {
76 self.left
77 }
78
79 #[must_use]
81 pub const fn top(self) -> f32 {
82 self.top
83 }
84
85 #[must_use]
87 pub const fn right(self) -> f32 {
88 self.right
89 }
90
91 #[must_use]
93 pub const fn bottom(self) -> f32 {
94 self.bottom
95 }
96
97 #[must_use]
99 pub fn width(self) -> f32 {
100 self.right - self.left
101 }
102
103 #[must_use]
105 pub fn height(self) -> f32 {
106 self.bottom - self.top
107 }
108
109 #[must_use]
111 pub fn area(self) -> f32 {
112 self.width() * self.height()
113 }
114
115 #[must_use]
117 pub fn center(self) -> Point {
118 Point {
119 x: self.left * 0.5 + self.right * 0.5,
120 y: self.top * 0.5 + self.bottom * 0.5,
121 }
122 }
123
124 #[must_use]
127 pub fn intersection(self, other: Self) -> Self {
128 let left = self.left.max(other.left);
129 let top = self.top.max(other.top);
130 let right = self.right.min(other.right).max(left);
131 let bottom = self.bottom.min(other.bottom).max(top);
132
133 Self {
134 left,
135 top,
136 right,
137 bottom,
138 }
139 }
140}