1 use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg};
5 ( $x:expr, $y:expr ) => {
10 #[derive(Debug, Default, Copy, Clone, PartialEq)]
17 pub fn length(&self) -> f64 {
18 ((self.x * self.x) + (self.y * self.y)).sqrt()
21 pub fn normalized(&self) -> Self {
22 let l = self.length();
29 pub fn to_radians(&self) -> Radians {
30 Radians(self.y.atan2(self.x))
33 pub fn to_degrees(&self) -> Degrees {
34 self.to_radians().to_degrees()
37 pub fn to_i32(self) -> Point<i32> {
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 Point<T> {
50 fn $fn(self, $rhs: $Rhs) -> Self {
58 impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point<T> {
59 fn $fn_assign(&mut self, $rhs: $Rhs) {
69 point_op!(+, Add(add), AddAssign(add_assign), rhs = Point<T> => rhs.x, rhs.y);
70 point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point<T> => rhs.x, rhs.y);
71 point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point<T> => rhs.x, rhs.y);
72 point_op!(/, Div(div), DivAssign(div_assign), rhs = Point<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);
78 ////////// multiply point with scalar //////////////////////////////////////////
79 impl<T: Mul<Output = T> + Copy> Mul<T> for Point<T> {
82 fn mul(self, rhs: T) -> Self {
90 impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point<T> {
91 fn mul_assign(&mut self, rhs: T) {
99 ////////// divide point with scalar ////////////////////////////////////////////
100 impl<T: Div<Output = T> + Copy> Div<T> for Point<T> {
103 fn div(self, rhs: T) -> Self {
111 impl<T: Div<Output = T> + Copy> DivAssign<T> for Point<T> {
112 fn div_assign(&mut self, rhs: T) {
120 impl<T: Neg<Output = T>> Neg for Point<T> {
123 fn neg(self) -> Self {
131 impl<T> From<(T, T)> for Point<T> {
132 fn from(item: (T, T)) -> Self {
140 impl<T> From<Point<T>> for (T, T) {
141 fn from(item: Point<T>) -> Self {
146 impl From<Degrees> for Point<f64> {
147 fn from(item: Degrees) -> Self {
148 let r = item.0.to_radians();
156 impl From<Radians> for Point<f64> {
157 fn from(item: Radians) -> Self {
165 #[derive(Debug, Default, PartialEq, Clone, Copy)]
166 pub struct Degrees(pub f64);
167 #[derive(Debug, Default, PartialEq, Clone, Copy)]
168 pub struct Radians(pub f64);
172 fn to_radians(&self) -> Radians {
173 Radians(self.0.to_radians())
179 fn to_degrees(&self) -> Degrees {
180 Degrees(self.0.to_degrees())
186 ( $x:expr, $y:expr ) => {
187 Rect { x: $x, y: $y }
197 impl<T: Mul<Output = T> + Copy> Rect<T> {
199 pub fn area(&self) -> T {
200 self.width * self.height
204 impl<T> From<(T, T)> for Rect<T> {
205 fn from(item: (T, T)) -> Self {
214 macro_rules! hashmap {
215 ($($k:expr => $v:expr),*) => {
217 let mut map = std::collections::HashMap::new();
218 $(map.insert($k, $v);)*
229 fn immutable_copy_of_point() {
230 let a = point!(0, 0);
231 let mut b = a; // Copy
232 assert_eq!(a, b); // PartialEq
234 assert_ne!(a, b); // PartialEq
239 let mut a = point!(1, 0);
240 assert_eq!(a + point!(2, 2), point!(3, 2)); // Add
241 a += point!(2, 2); // AddAssign
242 assert_eq!(a, point!(3, 2));
243 assert_eq!(point!(1, 0) + (2, 3), point!(3, 3));
248 let mut a = point!(1, 0);
249 assert_eq!(a - point!(2, 2), point!(-1, -2));
251 assert_eq!(a, point!(-1, -2));
252 assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3));
257 let mut a = point!(1, 2);
258 assert_eq!(a * 2, point!(2, 4));
259 assert_eq!(a * point!(2, 3), point!(2, 6));
261 assert_eq!(a, point!(2, 4));
263 assert_eq!(a, point!(6, 4));
264 assert_eq!(point!(1, 0) * (2, 3), point!(2, 0));
269 let mut a = point!(4, 8);
270 assert_eq!(a / 2, point!(2, 4));
271 assert_eq!(a / point!(2, 4), point!(2, 2));
273 assert_eq!(a, point!(2, 4));
275 assert_eq!(a, point!(1, 1));
276 assert_eq!(point!(6, 3) / (2, 3), point!(3, 1));
281 assert_eq!(point!(1, 1), -point!(-1, -1));
286 assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0));
287 assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0));
288 assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI));
289 assert!((Point::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
290 assert!((Point::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
294 fn area_for_rect_of_multipliable_type() {
295 let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
296 assert_eq!(r.area(), 30 * 20);
297 // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String