1use crate::Size;
2use derive_more::{Add, Constructor, From, Sub};
3use std::ops::{Add, Div, Mul, Neg};
4
5#[derive(Constructor, Add, Sub, Copy, From, Clone, Default, Debug, PartialEq)]
18pub struct Point {
19 x: f64,
20 y: f64,
21}
22
23impl Point {
24 pub fn distance(&self, other: Self) -> f64 {
26 ((self.x - other.x).powf(2.) + (self.y - other.y).powf(2.)).sqrt()
27 }
28
29 pub fn x(&self) -> f64 {
31 self.x
32 }
33
34 pub fn set_x(&mut self, x: impl Into<f64>) {
36 self.x = x.into();
37 }
38
39 pub fn y(&self) -> f64 {
41 self.y
42 }
43
44 pub fn set_y(&mut self, y: impl Into<f64>) {
46 self.y = y.into();
47 }
48
49 pub fn min(self, other: impl Into<Point>) -> Point {
51 let other = other.into();
52 Point {
53 x: self.x.min(other.x),
54 y: self.y.min(other.y),
55 }
56 }
57
58 pub fn max(self, other: impl Into<Point>) -> Point {
60 let other = other.into();
61 Point {
62 x: self.x.max(other.x),
63 y: self.y.max(other.y),
64 }
65 }
66
67 pub fn sqrt(mut self) -> Point {
69 self.x = self.x.sqrt();
70 self.y = self.y.sqrt();
71 self
72 }
73
74 pub fn abs(mut self) -> Point {
76 self.x = self.x.abs();
77 self.y = self.y.abs();
78 self
79 }
80
81 pub fn clamp(mut self, min: f64, max: f64) -> Point {
83 self.x = self.x.max(min).min(max);
84 self.y = self.y.max(min).min(max);
85 self
86 }
87}
88
89impl Add<Size> for Point {
92 type Output = Point;
93
94 fn add(mut self, rhs: Size) -> Self::Output {
95 self.x += rhs.width();
96 self.y += rhs.height();
97 self
98 }
99}
100
101impl Mul<f64> for Point {
102 type Output = Point;
103
104 fn mul(mut self, rhs: f64) -> Self::Output {
105 self.x *= rhs;
106 self.y *= rhs;
107 self
108 }
109}
110
111impl Mul<Point> for f64 {
112 type Output = Point;
113
114 fn mul(self, mut rhs: Point) -> Self::Output {
115 rhs.x *= self;
116 rhs.y *= self;
117 rhs
118 }
119}
120
121impl Mul<Point> for Point {
122 type Output = Point;
123
124 fn mul(mut self, rhs: Point) -> Self::Output {
125 self.x *= rhs.x();
126 self.y *= rhs.y();
127 self
128 }
129}
130
131impl Div<Point> for Point {
132 type Output = Point;
133
134 fn div(mut self, rhs: Point) -> Self::Output {
135 self.x /= rhs.x();
136 self.y /= rhs.y();
137 self
138 }
139}
140
141impl Neg for Point {
142 type Output = Point;
143
144 fn neg(mut self) -> Self::Output {
145 self.x = -self.x();
146 self.y = -self.y();
147 self
148 }
149}
150
151impl From<Size> for Point {
154 fn from(s: Size) -> Self {
155 Self::new(s.width(), s.height())
156 }
157}
158
159impl From<f64> for Point {
160 fn from(t: f64) -> Self {
161 Point::new(t, t)
162 }
163}
164
165impl From<i32> for Point {
166 fn from(t: i32) -> Self {
167 Point::new(t as f64, t as f64)
168 }
169}
170
171impl From<(i32, i32)> for Point {
172 fn from(s: (i32, i32)) -> Point {
173 Point::from((s.0 as f64, s.1 as f64))
174 }
175}
176
177#[cfg(test)]
180mod tests {
181 use super::*;
182
183 #[test]
184 fn test_distance() {
185 const EXPECTED_RESULT: f64 = 9.48683;
186 const ERROR_MARGIN: f64 = 0.00001;
187
188 let point_positive = Point::new(1., 5.);
189 let point_negative = Point::new(-2., -4.);
190
191 assert!(((point_positive.distance(point_negative) - EXPECTED_RESULT).abs() < ERROR_MARGIN));
192 assert!(((point_negative.distance(point_positive) - EXPECTED_RESULT).abs() < ERROR_MARGIN));
193 }
194
195 #[test]
196 fn test_sub() {
197 const EXPECTED_RESULT: Point = Point { x: -3., y: 5. };
198 const ERROR_MARGIN: f64 = 0.00001;
199
200 let left_side = Point::new(5., 7.);
201 let right_side = Point::new(8., 2.);
202
203 let result = left_side - right_side;
204
205 assert!((result.x - EXPECTED_RESULT.x).abs() < ERROR_MARGIN);
206 assert!((result.y - EXPECTED_RESULT.y).abs() < ERROR_MARGIN);
207 }
208
209 #[test]
210 fn test_add() {
211 const EXPECTED_RESULT: Point = Point { x: 13., y: 9. };
212 const ERROR_MARGIN: f64 = 0.00001;
213
214 let left_side = Point::new(5., 7.);
215 let right_side = Point::new(8., 2.);
216
217 let result = left_side + right_side;
218
219 assert!((result.x - EXPECTED_RESULT.x).abs() < ERROR_MARGIN);
220 assert!((result.y - EXPECTED_RESULT.y).abs() < ERROR_MARGIN);
221 }
222}