}
}
-////////// add point to point //////////////////////////////////////////////////
-impl<T: Add<Output = T>> Add for Point2D<T> {
- type Output = Self;
-
- fn add(self, rhs: Self) -> Self {
- Self {
- x: self.x + rhs.x,
- y: self.y + rhs.y,
+macro_rules! point_op {
+ ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => {
+ impl<T: $trait<Output = T>> $trait<$Rhs> for Point2D<T> {
+ type Output = Self;
+
+ fn $fn(self, $rhs: $Rhs) -> Self {
+ Self {
+ x: self.x $op $x,
+ y: self.y $op $y,
+ }
+ }
}
- }
-}
-
-impl<T: Add<Output = T> + Copy> AddAssign for Point2D<T> {
- fn add_assign(&mut self, rhs: Self) {
- *self = Self {
- x: self.x + rhs.x,
- y: self.y + rhs.y,
- }
- }
-}
-////////// add tuple to point //////////////////////////////////////////////////
-impl<T: Add<Output = T>> Add<(T, T)> for Point2D<T> {
- type Output = Self;
-
- fn add(self, rhs: (T, T)) -> Self {
- Self {
- x: self.x + rhs.0,
- y: self.y + rhs.1,
+ impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point2D<T> {
+ fn $fn_assign(&mut self, $rhs: $Rhs) {
+ *self = Self {
+ x: self.x $op $x,
+ y: self.y $op $y,
+ }
+ }
}
}
}
-////////// subtract point from point ///////////////////////////////////////////
-impl<T: Sub<Output = T>> Sub for Point2D<T> {
- type Output = Self;
-
- fn sub(self, rhs: Self) -> Self {
- Self {
- x: self.x - rhs.x,
- y: self.y - rhs.y,
- }
- }
-}
-
-impl<T: Sub<Output = T> + Copy> SubAssign for Point2D<T> {
- fn sub_assign(&mut self, rhs: Self) {
- *self = Self {
- x: self.x - rhs.x,
- y: self.y - rhs.y,
- }
- }
-}
+point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y);
+point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y);
+point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y);
+point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y);
+point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
+point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
+point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
+point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
////////// multiply point with scalar //////////////////////////////////////////
impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> {
}
}
-////////// multiply components of two points ///////////////////////////////////
-impl<T: Mul<Output = T>> Mul for Point2D<T> {
- type Output = Self;
-
- fn mul(self, rhs: Self) -> Self {
- Self {
- x: self.x * rhs.x,
- y: self.y * rhs.y,
- }
- }
-}
-
-impl<T: Mul<Output = T> + Copy> MulAssign for Point2D<T> {
- fn mul_assign(&mut self, rhs: Self) {
- *self = Self {
- x: self.x * rhs.x,
- y: self.y * rhs.y,
- }
- }
-}
-
////////// divide point with scalar ////////////////////////////////////////////
impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
type Output = Self;
}
}
-////////// divide components of two points /////////////////////////////////////
-impl<T: Div<Output = T>> Div for Point2D<T> {
- type Output = Self;
-
- fn div(self, rhs: Self) -> Self {
- Self {
- x: self.x / rhs.x,
- y: self.y / rhs.y,
- }
- }
-}
-
-impl<T: Div<Output = T> + Copy> DivAssign for Point2D<T> {
- fn div_assign(&mut self, rhs: Self) {
- *self = Self {
- x: self.x / rhs.x,
- y: self.y / rhs.y,
- }
- }
-}
-
impl<T: Neg<Output = T>> Neg for Point2D<T> {
type Output = Self;
assert_eq!(a - point!(2, 2), point!(-1, -2));
a -= point!(2, 2);
assert_eq!(a, point!(-1, -2));
+ assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3));
}
#[test]
assert_eq!(a, point!(2, 4));
a *= point!(3, 1);
assert_eq!(a, point!(6, 4));
+ assert_eq!(point!(1, 0) * (2, 3), point!(2, 0));
}
#[test]
assert_eq!(a, point!(2, 4));
a /= point!(2, 4);
assert_eq!(a, point!(1, 1));
+ assert_eq!(point!(6, 3) / (2, 3), point!(3, 1));
}
#[test]