1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Primitives for a few physics-based types (velocity, angles).

use num_traits::{Float, FloatConst};
use std::fmt::{Debug, Formatter, Result};
use std::ops::{Add, Div, Neg, Sub};
use std::time::Duration;

/// Represents dy/dt. This type is mostly used for events that involve movement,
/// such as scroll events.
pub struct Velocity<T> {
    delta: T,
    time: Duration,
}

impl<T> Velocity<T> {
    /// Returns a new velocity with the given delta within the given timeframe.
    pub fn new(delta: T, time: Duration) -> Velocity<T> {
        Velocity { delta, time }
    }

    /// Returns the velocity in O per second where O is the result of dividing
    /// T by a f32 that represents the duration of this velocity sample.
    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())
    }
}

/// Type-safe representation for angles.
#[derive(Copy, Clone, Default, Eq, PartialEq)]
pub struct Angle<T> {
    radians: T,
}

impl<T> Angle<T>
where
    T: Float,
{
    /// Returns a new angle with the given radians.
    pub fn with_radians(radians: T) -> Angle<T> {
        Angle { radians }
    }

    /// Converts the given angle to radians.
    pub fn to_radians(&self) -> T {
        self.radians
    }
}

impl<T> Angle<T>
where
    T: Float + FloatConst,
{
    /// Returns a new angle with the given degrees.
    pub fn with_degrees(degrees: T) -> Angle<T> {
        Angle {
            radians: degrees / T::from(180.0).unwrap() * T::PI(),
        }
    }

    /// Converts the given angle to degrees.
    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()))
    }
}