Commit | Line | Data |
---|---|---|
2836f506 | 1 | use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg}; |
296187ca | 2 | |
787dbfb4 | 3 | #[macro_export] |
296187ca | 4 | macro_rules! point { |
6ba7aef1 TW |
5 | ( $x:expr, $y:expr ) => { |
6 | Point2D { x: $x, y: $y } | |
7 | }; | |
296187ca TW |
8 | } |
9 | ||
b0566120 | 10 | #[derive(Debug, Default, Copy, Clone, PartialEq)] |
296187ca TW |
11 | pub struct Point2D<T> { |
12 | pub x: T, | |
13 | pub y: T, | |
14 | } | |
15 | ||
16 | impl Point2D<f64> { | |
eca25591 | 17 | pub fn length(&self) -> f64 { |
296187ca TW |
18 | ((self.x * self.x) + (self.y * self.y)).sqrt() |
19 | } | |
bf7b5671 | 20 | |
eca25591 TW |
21 | pub fn normalize(&self) -> Self { |
22 | let l = self.length(); | |
23 | Self { | |
24 | x: self.x / l, | |
25 | y: self.y / l, | |
26 | } | |
27 | } | |
28 | ||
29 | pub fn radians(&self) -> Radians { | |
30 | Radians(self.y.atan2(self.x)) | |
31 | } | |
32 | ||
33 | pub fn degrees(&self) -> Degrees { | |
34 | self.radians().to_degrees() | |
35 | } | |
36 | ||
bf7b5671 TW |
37 | pub fn to_i32(self) -> Point2D<i32> { |
38 | Point2D { | |
39 | x: self.x as i32, | |
40 | y: self.y as i32, | |
41 | } | |
42 | } | |
296187ca TW |
43 | } |
44 | ||
6cd86b94 TW |
45 | macro_rules! point_op { |
46 | ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => { | |
47 | impl<T: $trait<Output = T>> $trait<$Rhs> for Point2D<T> { | |
48 | type Output = Self; | |
49 | ||
50 | fn $fn(self, $rhs: $Rhs) -> Self { | |
51 | Self { | |
52 | x: self.x $op $x, | |
53 | y: self.y $op $y, | |
54 | } | |
55 | } | |
2836f506 | 56 | } |
2836f506 | 57 | |
6cd86b94 TW |
58 | impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point2D<T> { |
59 | fn $fn_assign(&mut self, $rhs: $Rhs) { | |
60 | *self = Self { | |
61 | x: self.x $op $x, | |
62 | y: self.y $op $y, | |
63 | } | |
64 | } | |
2836f506 TW |
65 | } |
66 | } | |
67 | } | |
68 | ||
6cd86b94 TW |
69 | point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y); |
70 | point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y); | |
71 | point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y); | |
72 | point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y); | |
73 | point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1); | |
74 | point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1); | |
75 | point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1); | |
76 | point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1); | |
2836f506 TW |
77 | |
78 | ////////// multiply point with scalar ////////////////////////////////////////// | |
79 | impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> { | |
80 | type Output = Self; | |
81 | ||
82 | fn mul(self, rhs: T) -> Self { | |
83 | Self { | |
84 | x: self.x * rhs, | |
85 | y: self.y * rhs, | |
86 | } | |
87 | } | |
88 | } | |
89 | ||
90 | impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> { | |
91 | fn mul_assign(&mut self, rhs: T) { | |
92 | *self = Self { | |
93 | x: self.x * rhs, | |
94 | y: self.y * rhs, | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
2836f506 TW |
99 | ////////// divide point with scalar //////////////////////////////////////////// |
100 | impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> { | |
101 | type Output = Self; | |
102 | ||
103 | fn div(self, rhs: T) -> Self { | |
104 | Self { | |
105 | x: self.x / rhs, | |
106 | y: self.y / rhs, | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> { | |
112 | fn div_assign(&mut self, rhs: T) { | |
113 | *self = Self { | |
114 | x: self.x / rhs, | |
115 | y: self.y / rhs, | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
2836f506 TW |
120 | impl<T: Neg<Output = T>> Neg for Point2D<T> { |
121 | type Output = Self; | |
122 | ||
123 | fn neg(self) -> Self { | |
124 | Self { | |
125 | x: -self.x, | |
126 | y: -self.y, | |
127 | } | |
296187ca TW |
128 | } |
129 | } | |
130 | ||
b0566120 TW |
131 | impl<T> From<(T, T)> for Point2D<T> { |
132 | fn from(item: (T, T)) -> Self { | |
133 | Point2D { | |
134 | x: item.0, | |
135 | y: item.1, | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
bf7b5671 TW |
140 | impl<T> From<Point2D<T>> for (T, T) { |
141 | fn from(item: Point2D<T>) -> Self { | |
142 | (item.x, item.y) | |
143 | } | |
144 | } | |
145 | ||
e58a1769 TW |
146 | impl From<Degrees> for Point2D<f64> { |
147 | fn from(item: Degrees) -> Self { | |
148 | Point2D { | |
149 | x: (item.0 * std::f64::consts::PI / 180.0).cos(), | |
150 | y: (item.0 * std::f64::consts::PI / 180.0).sin(), | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | impl From<Radians> for Point2D<f64> { | |
156 | fn from(item: Radians) -> Self { | |
157 | Point2D { | |
158 | x: item.0.cos(), | |
159 | y: item.0.sin(), | |
160 | } | |
161 | } | |
162 | } | |
163 | ||
bf7b5671 TW |
164 | #[derive(Debug, Default, PartialEq, Clone, Copy)] |
165 | pub struct Degrees(pub f64); | |
166 | #[derive(Debug, Default, PartialEq, Clone, Copy)] | |
167 | pub struct Radians(pub f64); | |
e58a1769 TW |
168 | |
169 | impl Degrees { | |
bf7b5671 | 170 | #[allow(dead_code)] |
e58a1769 TW |
171 | fn to_radians(&self) -> Radians { |
172 | Radians(self.0 * std::f64::consts::PI / 180.0) | |
173 | } | |
174 | } | |
175 | ||
176 | impl Radians { | |
bf7b5671 | 177 | #[allow(dead_code)] |
e58a1769 TW |
178 | fn to_degrees(&self) -> Degrees { |
179 | Degrees(self.0 * 180.0 * std::f64::consts::FRAC_1_PI) | |
180 | } | |
181 | } | |
182 | ||
0b5024d1 TW |
183 | #[macro_export] |
184 | macro_rules! rect { | |
185 | ( $x:expr, $y:expr ) => { | |
186 | Rect { x: $x, y: $y } | |
187 | }; | |
188 | } | |
189 | ||
6edafdc0 TW |
190 | #[derive(Default)] |
191 | pub struct Rect<T> { | |
192 | pub width: T, | |
193 | pub height: T, | |
194 | } | |
195 | ||
6ba7aef1 | 196 | impl<T: Mul<Output = T> + Copy> Rect<T> { |
6edafdc0 TW |
197 | #[allow(dead_code)] |
198 | pub fn area(&self) -> T { | |
6ba7aef1 | 199 | self.width * self.height |
6edafdc0 TW |
200 | } |
201 | } | |
202 | ||
203 | impl<T> From<(T, T)> for Rect<T> { | |
204 | fn from(item: (T, T)) -> Self { | |
6ba7aef1 TW |
205 | Rect { |
206 | width: item.0, | |
207 | height: item.1, | |
208 | } | |
6edafdc0 TW |
209 | } |
210 | } | |
211 | ||
249d43ea TW |
212 | #[macro_export] |
213 | macro_rules! hashmap { | |
214 | ($($k:expr => $v:expr),*) => { | |
215 | { | |
216 | let mut map = std::collections::HashMap::new(); | |
217 | $(map.insert($k, $v);)* | |
218 | map | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
296187ca TW |
223 | #[cfg(test)] |
224 | mod tests { | |
225 | use super::*; | |
226 | ||
227 | #[test] | |
228 | fn immutable_copy_of_point() { | |
229 | let a = point!(0, 0); | |
230 | let mut b = a; // Copy | |
231 | assert_eq!(a, b); // PartialEq | |
232 | b.x = 1; | |
233 | assert_ne!(a, b); // PartialEq | |
234 | } | |
235 | ||
236 | #[test] | |
237 | fn add_points() { | |
238 | let mut a = point!(1, 0); | |
239 | assert_eq!(a + point!(2, 2), point!(3, 2)); // Add | |
240 | a += point!(2, 2); // AddAssign | |
241 | assert_eq!(a, point!(3, 2)); | |
2836f506 TW |
242 | assert_eq!(point!(1, 0) + (2, 3), point!(3, 3)); |
243 | } | |
244 | ||
245 | #[test] | |
246 | fn sub_points() { | |
247 | let mut a = point!(1, 0); | |
248 | assert_eq!(a - point!(2, 2), point!(-1, -2)); | |
249 | a -= point!(2, 2); | |
250 | assert_eq!(a, point!(-1, -2)); | |
6cd86b94 | 251 | assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3)); |
2836f506 TW |
252 | } |
253 | ||
254 | #[test] | |
255 | fn mul_points() { | |
256 | let mut a = point!(1, 2); | |
257 | assert_eq!(a * 2, point!(2, 4)); | |
258 | assert_eq!(a * point!(2, 3), point!(2, 6)); | |
259 | a *= 2; | |
260 | assert_eq!(a, point!(2, 4)); | |
261 | a *= point!(3, 1); | |
262 | assert_eq!(a, point!(6, 4)); | |
6cd86b94 | 263 | assert_eq!(point!(1, 0) * (2, 3), point!(2, 0)); |
2836f506 TW |
264 | } |
265 | ||
266 | #[test] | |
267 | fn div_points() { | |
268 | let mut a = point!(4, 8); | |
269 | assert_eq!(a / 2, point!(2, 4)); | |
270 | assert_eq!(a / point!(2, 4), point!(2, 2)); | |
271 | a /= 2; | |
272 | assert_eq!(a, point!(2, 4)); | |
273 | a /= point!(2, 4); | |
274 | assert_eq!(a, point!(1, 1)); | |
6cd86b94 | 275 | assert_eq!(point!(6, 3) / (2, 3), point!(3, 1)); |
2836f506 TW |
276 | } |
277 | ||
278 | #[test] | |
279 | fn neg_point() { | |
280 | assert_eq!(point!(1, 1), -point!(-1, -1)); | |
296187ca | 281 | } |
6edafdc0 TW |
282 | |
283 | #[test] | |
e58a1769 TW |
284 | fn angles() { |
285 | assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0)); | |
286 | assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0)); | |
287 | assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI)); | |
288 | assert!((Point2D::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001); | |
289 | assert!((Point2D::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001); | |
290 | } | |
291 | ||
292 | #[test] | |
6edafdc0 TW |
293 | fn area_for_rect_of_multipliable_type() { |
294 | let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait | |
6ba7aef1 | 295 | assert_eq!(r.area(), 30 * 20); |
6edafdc0 TW |
296 | // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String |
297 | } | |
296187ca | 298 | } |