Skip to main content

trackball/
scale.rs

1use crate::float;
2
3/// Scale induced by relative input.
4///
5/// Implements [`Default`] and can be created with `Scale::default()`.
6#[derive(Debug, Clone, PartialEq)]
7pub struct Scale {
8    /// Denominator. Default is scroll unit of `120.0`.
9    den: f32,
10}
11
12impl Default for Scale {
13    fn default() -> Self {
14        Self { den: 120.0 }
15    }
16}
17
18impl Scale {
19    /// Computes scale ratio from relative value.
20    ///
21    /// The ratio is exponential in the relative value `num` over the denominator, that is
22    /// `exp(-num / den)`, so that a relative value equal to the denominator scales by `1 / e`.
23    /// Hence, scaling in and out by the same relative value restores the distance and the ratio of
24    /// a relative value is the product of the ratios of its parts, regardless of how the input is
25    /// split into events or frames. The ratio is always positive.
26    #[must_use]
27    pub fn compute(&self, num: f32) -> f32 {
28        float::exp(-num / self.den)
29    }
30    /// Denominator. Default is scroll unit of `120.0`.
31    #[must_use]
32    pub const fn denominator(&self) -> f32 {
33        self.den
34    }
35    /// Sets denominator. Default is scroll unit of `120.0`.
36    pub const fn set_denominator(&mut self, den: f32) {
37        self.den = den;
38    }
39}