}
}
- 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]
use teststate::TestState;
use AppState;
-use common::{Point, Radians};
+use common::{Point, ToAngle};
use core::app::StateChange;
use core::controller::Controller;
use core::controller::ControllerManager;
return Dead
}
self.bounces -= 1;
- let mut a = wall.normal().mirror(self.vel.to_radians()); // TODO interpolera normalen mellan närliggande väggdelar? bollarna studsar väldigt "kantigt" nu
+ let mut a = wall.normal().mirror(self.vel.to_angle()); // TODO interpolera normalen mellan närliggande väggdelar? bollarna studsar väldigt "kantigt" nu
self.pos = pos;
self.vel = Point::from(a) * self.vel.length() * 0.35;
self.pos += self.vel; // TODO det här kan få bollen att åka igenom en närliggande vägg utan att kollisionstestas, men behövs just nu för att inte kollidera med samma vägg bakifrån
// create another boll
use rand::distributions::{Distribution, Normal};
let mut rng = rand::thread_rng();
- a.0 += Normal::new(0.0, 0.1).sample(&mut rng); // TODO slumpen kan ge en vinkel som är under tangenten. vinkel-metoder på väggen istället kanske?
+ a += Normal::new(0.0, 0.1).sample(&mut rng).radians(); // TODO slumpen kan ge en vinkel som är under tangenten. vinkel-metoder på väggen istället kanske?
use rand::Rng;
objects.push(Box::new(Boll {
vel: Point::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length() * rng.gen_range(0.25, 1.0),
-use common::{Point, Dimension, Intersection, Radians, supercover_line};
+use common::{Point, Dimension, Intersection, Angle, ToAngle, supercover_line};
use core::render::Renderer;
use sprites::SpriteManager;
use std::rc::Rc;
for wall in &self.walls {
for e in &wall.edges {
let c = (e.p1 + e.p2) / 2.0;
- let mut rad = (e.p2 - e.p1).to_radians();
- rad.0 += std::f64::consts::FRAC_PI_2;
+ let a = (e.p2 - e.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians();
renderer.draw_line(
<(i32, i32)>::from(c.to_i32()),
- <(i32, i32)>::from((c + Point::from(rad) * 10.0).to_i32()),
+ <(i32, i32)>::from((c + Point::from(a) * 10.0).to_i32()),
(255, 128, 0));
renderer.draw_line(
}
}
- pub fn normal(&self) -> Radians {
- let mut rad = (self.edge.p2 - self.edge.p1).to_radians();
- rad.0 += std::f64::consts::FRAC_PI_2;
- rad
+ pub fn normal(&self) -> Angle {
+ (self.edge.p2 - self.edge.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians()
}
}