-use std::ops::{Add, AddAssign, Mul};
+use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg};
#[macro_export]
macro_rules! point {
- ( $x:expr, $y:expr ) => { Point2D { x:$x, y:$y } };
+ ( $x:expr, $y:expr ) => {
+ Point2D { x: $x, y: $y }
+ };
}
-#[derive(Debug, Copy, Clone, PartialEq)]
+#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct Point2D<T> {
pub x: T,
pub y: T,
}
impl Point2D<f64> {
- pub fn length(self) -> f64 {
+ pub fn length(&self) -> f64 {
((self.x * self.x) + (self.y * self.y)).sqrt()
}
+
+ pub fn normalized(&self) -> Self {
+ let l = self.length();
+ Self {
+ x: self.x / l,
+ y: self.y / l,
+ }
+ }
+
+ pub fn to_radians(&self) -> Radians {
+ Radians(self.y.atan2(self.x))
+ }
+
+ pub fn to_degrees(&self) -> Degrees {
+ self.to_radians().to_degrees()
+ }
+
+ pub fn to_i32(self) -> Point2D<i32> {
+ Point2D {
+ x: self.x as i32,
+ y: self.y as i32,
+ }
+ }
+}
+
+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: $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,
+ }
+ }
+ }
+ }
+}
+
+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> {
+ type Output = Self;
+
+ fn mul(self, rhs: T) -> Self {
+ Self {
+ x: self.x * rhs,
+ y: self.y * rhs,
+ }
+ }
+}
+
+impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> {
+ fn mul_assign(&mut self, rhs: T) {
+ *self = Self {
+ x: self.x * rhs,
+ y: self.y * rhs,
+ }
+ }
+}
+
+////////// divide point with scalar ////////////////////////////////////////////
+impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
+ type Output = Self;
+
+ fn div(self, rhs: T) -> Self {
+ Self {
+ x: self.x / rhs,
+ y: self.y / rhs,
+ }
+ }
+}
+
+impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> {
+ fn div_assign(&mut self, rhs: T) {
+ *self = Self {
+ x: self.x / rhs,
+ y: self.y / rhs,
+ }
+ }
+}
+
+impl<T: Neg<Output = T>> Neg for Point2D<T> {
+ type Output = Self;
+
+ fn neg(self) -> Self {
+ Self {
+ x: -self.x,
+ y: -self.y,
+ }
+ }
+}
+
+impl<T> From<(T, T)> for Point2D<T> {
+ fn from(item: (T, T)) -> Self {
+ Point2D {
+ x: item.0,
+ y: item.1,
+ }
+ }
+}
+
+impl<T> From<Point2D<T>> for (T, T) {
+ fn from(item: Point2D<T>) -> Self {
+ (item.x, item.y)
+ }
+}
+
+impl From<Degrees> for Point2D<f64> {
+ fn from(item: Degrees) -> Self {
+ let r = item.0.to_radians();
+ Point2D {
+ x: r.cos(),
+ y: r.sin(),
+ }
+ }
+}
+
+impl From<Radians> for Point2D<f64> {
+ fn from(item: Radians) -> Self {
+ Point2D {
+ x: item.0.cos(),
+ y: item.0.sin(),
+ }
+ }
}
-impl<T: Add<Output=T>> Add for Point2D<T> {
- type Output = Point2D<T>;
+#[derive(Debug, Default, PartialEq, Clone, Copy)]
+pub struct Degrees(pub f64);
+#[derive(Debug, Default, PartialEq, Clone, Copy)]
+pub struct Radians(pub f64);
- fn add(self, rhs: Point2D<T>) -> Self::Output {
- Point2D { x: self.x + rhs.x, y: self.y + rhs.y }
+impl Degrees {
+ #[allow(dead_code)]
+ fn to_radians(&self) -> Radians {
+ Radians(self.0.to_radians())
}
}
-impl<T: AddAssign> AddAssign for Point2D<T> {
- fn add_assign(&mut self, rhs: Point2D<T>) {
- self.x += rhs.x;
- self.y += rhs.y;
+impl Radians {
+ #[allow(dead_code)]
+ fn to_degrees(&self) -> Degrees {
+ Degrees(self.0.to_degrees())
}
}
+#[macro_export]
+macro_rules! rect {
+ ( $x:expr, $y:expr ) => {
+ Rect { x: $x, y: $y }
+ };
+}
+
#[derive(Default)]
pub struct Rect<T> {
pub width: T,
pub height: T,
}
-impl<T: Mul<Output=T> + Copy> Rect<T> {
+impl<T: Mul<Output = T> + Copy> Rect<T> {
#[allow(dead_code)]
pub fn area(&self) -> T {
- self.width * self.height
+ self.width * self.height
}
}
impl<T> From<(T, T)> for Rect<T> {
fn from(item: (T, T)) -> Self {
- Rect { width: item.0, height: item.1 }
+ Rect {
+ width: item.0,
+ height: item.1,
+ }
+ }
+}
+
+#[macro_export]
+macro_rules! hashmap {
+ ($($k:expr => $v:expr),*) => {
+ {
+ let mut map = std::collections::HashMap::new();
+ $(map.insert($k, $v);)*
+ map
+ }
}
}
assert_eq!(a + point!(2, 2), point!(3, 2)); // Add
a += point!(2, 2); // AddAssign
assert_eq!(a, point!(3, 2));
+ assert_eq!(point!(1, 0) + (2, 3), point!(3, 3));
+ }
+
+ #[test]
+ fn sub_points() {
+ let mut a = point!(1, 0);
+ 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]
+ fn mul_points() {
+ let mut a = point!(1, 2);
+ assert_eq!(a * 2, point!(2, 4));
+ assert_eq!(a * point!(2, 3), point!(2, 6));
+ a *= 2;
+ 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]
+ fn div_points() {
+ let mut a = point!(4, 8);
+ assert_eq!(a / 2, point!(2, 4));
+ assert_eq!(a / point!(2, 4), point!(2, 2));
+ a /= 2;
+ 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]
+ fn neg_point() {
+ assert_eq!(point!(1, 1), -point!(-1, -1));
+ }
+
+ #[test]
+ fn angles() {
+ assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0));
+ assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0));
+ assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI));
+ assert!((Point2D::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
+ assert!((Point2D::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
}
#[test]
fn area_for_rect_of_multipliable_type() {
let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
- assert_eq!(r.area(), 30 * 20);
+ assert_eq!(r.area(), 30 * 20);
// let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
}
}