}
}
+impl From<Degrees> for Point2D<f64> {
+ fn from(item: Degrees) -> Self {
+ Point2D {
+ x: (item.0 * std::f64::consts::PI / 180.0).cos(),
+ y: (item.0 * std::f64::consts::PI / 180.0).sin(),
+ }
+ }
+}
+
+impl From<Radians> for Point2D<f64> {
+ fn from(item: Radians) -> Self {
+ Point2D {
+ x: item.0.cos(),
+ y: item.0.sin(),
+ }
+ }
+}
+
+#[derive(Debug, PartialEq, Clone, Copy)]
+struct Degrees(f64);
+#[derive(Debug, PartialEq, Clone, Copy)]
+struct Radians(f64);
+
+impl Degrees {
+ fn to_radians(&self) -> Radians {
+ Radians(self.0 * std::f64::consts::PI / 180.0)
+ }
+}
+
+impl Radians {
+ fn to_degrees(&self) -> Degrees {
+ Degrees(self.0 * 180.0 * std::f64::consts::FRAC_1_PI)
+ }
+}
+
#[macro_export]
macro_rules! rect {
( $x:expr, $y:expr ) => {
}
#[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);