use num_traits::{Float, FloatConst};
use std::fmt::{Debug, Formatter, Result};
use std::ops::{Add, Div, Neg, Sub};
use std::time::Duration;
pub struct Velocity<T> {
delta: T,
time: Duration,
}
impl<T> Velocity<T> {
pub fn new(delta: T, time: Duration) -> Velocity<T> {
Velocity { delta, time }
}
pub fn into_per_second<X, O>(self) -> O
where
X: From<f32>,
T: Div<X, Output = O>,
{
self.delta / X::from(self.time.as_secs_f32())
}
}
#[derive(Copy, Clone, Default, Eq, PartialEq)]
pub struct Angle<T> {
radians: T,
}
impl<T> Angle<T>
where
T: Float,
{
pub fn with_radians(radians: T) -> Angle<T> {
Angle { radians }
}
pub fn to_radians(&self) -> T {
self.radians
}
}
impl<T> Angle<T>
where
T: Float + FloatConst,
{
pub fn with_degrees(degrees: T) -> Angle<T> {
Angle {
radians: degrees / T::from(180.0).unwrap() * T::PI(),
}
}
pub fn to_degrees(&self) -> T {
self.radians / T::PI() * T::from(180.0).unwrap()
}
}
impl<T> Add for &Angle<T>
where
T: Float + FloatConst,
{
type Output = Angle<T>;
fn add(self, rhs: &Angle<T>) -> Self::Output {
Angle {
radians: self.radians + rhs.radians,
}
}
}
impl<T> Sub for &Angle<T>
where
T: Float + FloatConst,
{
type Output = Angle<T>;
fn sub(self, rhs: &Angle<T>) -> Self::Output {
Angle {
radians: self.radians - rhs.radians,
}
}
}
impl<T> Neg for Angle<T>
where
T: Neg,
{
type Output = Angle<T::Output>;
fn neg(self) -> Self::Output {
Angle {
radians: self.radians.neg(),
}
}
}
impl<T> Debug for Angle<T>
where
T: Float + FloatConst + Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_fmt(format_args!("{:?}ยบ", self.to_degrees()))
}
}