}
}
- 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_angle(&self) -> Angle {
+ self.y.atan2(self.x).radians()
}
pub fn to_i32(self) -> Point<i32> {
}
}
-impl From<Degrees> for Point<f64> {
- fn from(item: Degrees) -> Self {
- let r = item.0.to_radians();
- Point {
- x: r.cos(),
- y: r.sin(),
- }
+impl From<Angle> for Point<f64> {
+ fn from(item: Angle) -> Self {
+ Point {
+ x: item.0.cos(),
+ y: item.0.sin(),
+ }
}
}
-impl From<Radians> for Point<f64> {
- fn from(item: Radians) -> Self {
- Point {
- x: item.0.cos(),
- y: item.0.sin(),
- }
- }
-}
+////////// ANGLE ///////////////////////////////////////////////////////////////
#[derive(Debug, Default, PartialEq, Clone, Copy)]
-pub struct Degrees(pub f64);
-#[derive(Debug, Default, PartialEq, Clone, Copy)]
-pub struct Radians(pub f64);
+pub struct Angle(pub f64);
-impl Degrees {
- #[allow(dead_code)]
- pub fn to_radians(&self) -> Radians {
- Radians(self.0.to_radians())
+pub trait ToAngle {
+ fn radians(self) -> Angle;
+ fn degrees(self) -> Angle;
+}
+
+macro_rules! impl_angle {
+ ($($type:ty),*) => {
+ $(
+ impl ToAngle for $type {
+ fn radians(self) -> Angle {
+ Angle(self as f64)
+ }
+
+ fn degrees(self) -> Angle {
+ Angle((self as f64).to_radians())
+ }
+ }
+
+ impl Mul<$type> for Angle {
+ type Output = Self;
+
+ fn mul(self, rhs: $type) -> Self {
+ Angle(self.0 * (rhs as f64))
+ }
+ }
+
+ impl MulAssign<$type> for Angle {
+ fn mul_assign(&mut self, rhs: $type) {
+ self.0 *= rhs as f64;
+ }
+ }
+
+ impl Div<$type> for Angle {
+ type Output = Self;
+
+ fn div(self, rhs: $type) -> Self {
+ Angle(self.0 / (rhs as f64))
+ }
+ }
+
+ impl DivAssign<$type> for Angle {
+ fn div_assign(&mut self, rhs: $type) {
+ self.0 /= rhs as f64;
+ }
+ }
+ )*
}
}
-impl Radians {
- #[allow(dead_code)]
- pub fn to_degrees(&self) -> Degrees {
- Degrees(self.0.to_degrees())
+impl_angle!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
+
+impl Angle {
+ pub fn to_radians(self) -> f64 {
+ self.0
+ }
+
+ pub fn to_degrees(self) -> f64 {
+ self.0.to_degrees()
}
/// Returns the reflection of the incident when mirrored along this angle.
- pub fn mirror(&self, incidence: Radians) -> Radians {
- Radians((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU)
+ pub fn mirror(&self, incidence: Angle) -> Angle {
+ Angle((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU)
+ }
+}
+
+// TODO override eq, 0==360 osv
+
+// addition and subtraction of angles
+
+impl Add<Angle> for Angle {
+ type Output = Self;
+
+ fn add(self, rhs: Angle) -> Self {
+ Angle(self.0 + rhs.0)
+ }
+}
+
+impl AddAssign<Angle> for Angle {
+ fn add_assign(&mut self, rhs: Angle) {
+ self.0 += rhs.0;
+ }
+}
+
+impl Sub<Angle> for Angle {
+ type Output = Self;
+
+ fn sub(self, rhs: Angle) -> Self {
+ Angle(self.0 - rhs.0)
+ }
+}
+
+impl SubAssign<Angle> for Angle {
+ fn sub_assign(&mut self, rhs: Angle) {
+ self.0 -= rhs.0;
}
}
#[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!((Point::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
- assert!((Point::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
+ assert_eq!(0.radians(), 0.degrees());
+ assert_eq!(180.degrees(), std::f64::consts::PI.radians());
+ assert_eq!(std::f64::consts::PI.radians().to_degrees(), 180.0);
+ assert!((Point::from(90.degrees()) - point!(0.0, 1.0)).length() < 0.001);
+ assert!((Point::from(std::f64::consts::FRAC_PI_2.radians()) - point!(0.0, 1.0)).length() < 0.001);
}
#[test]