diff options
| author | Martin Haug <mhaug@live.de> | 2021-06-01 14:56:02 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-06-09 22:44:40 +0200 |
| commit | 73fa2eda2c23bd3baeb9e22b99eb0bfb183fc638 (patch) | |
| tree | de42d0b6807b862cc28db5197751d5c503663c3c /src/geom | |
| parent | 9983634cd59b75a5842a096cc5fbf6472e65b5cb (diff) | |
Introduce `fr`s
Diffstat (limited to 'src/geom')
| -rw-r--r-- | src/geom/fr.rs | 101 | ||||
| -rw-r--r-- | src/geom/mod.rs | 2 |
2 files changed, 103 insertions, 0 deletions
diff --git a/src/geom/fr.rs b/src/geom/fr.rs new file mode 100644 index 00000000..974d675e --- /dev/null +++ b/src/geom/fr.rs @@ -0,0 +1,101 @@ +use decorum::N64; + +use super::*; + +/// A fractional length. +#[derive(Default, Copy, Clone, PartialEq, PartialOrd, Hash)] +pub struct Fractional(N64); + +impl Fractional { + /// Takes up zero space: `0fr`. + pub fn zero() -> Self { + Self(N64::from(0.0)) + } + + /// Takes up as much space as all other items with this fractional size: `1fr`. + pub fn one() -> Self { + Self(N64::from(1.0)) + } + + /// Create a new fractional value. + pub fn new(ratio: f64) -> Self { + Self(N64::from(ratio)) + } + + /// Get the underlying ratio. + pub fn get(self) -> f64 { + self.0.into() + } + + /// Whether the ratio is zero. + pub fn is_zero(self) -> bool { + self.0 == 0.0 + } +} + +impl Display for Fractional { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}fr", self.get()) + } +} + +impl Debug for Fractional { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Display::fmt(self, f) + } +} + +impl Neg for Fractional { + type Output = Self; + + fn neg(self) -> Self { + Self(-self.0) + } +} + +impl Add for Fractional { + type Output = Self; + + fn add(self, other: Self) -> Self { + Self(self.0 + other.0) + } +} + +sub_impl!(Fractional - Fractional -> Fractional); + +impl Mul<f64> for Fractional { + type Output = Self; + + fn mul(self, other: f64) -> Self { + Self(self.0 * other) + } +} + +impl Mul<Fractional> for f64 { + type Output = Fractional; + + fn mul(self, other: Fractional) -> Fractional { + other * self + } +} + +impl Div<f64> for Fractional { + type Output = Self; + + fn div(self, other: f64) -> Self { + Self(self.0 / other) + } +} + +impl Div for Fractional { + type Output = f64; + + fn div(self, other: Self) -> f64 { + self.get() / other.get() + } +} + +assign_impl!(Fractional += Fractional); +assign_impl!(Fractional -= Fractional); +assign_impl!(Fractional *= f64); +assign_impl!(Fractional /= f64); diff --git a/src/geom/mod.rs b/src/geom/mod.rs index 0031c6df..ce8a7276 100644 --- a/src/geom/mod.rs +++ b/src/geom/mod.rs @@ -5,6 +5,7 @@ mod macros; mod align; mod angle; mod dir; +mod fr; mod gen; mod length; mod linear; @@ -18,6 +19,7 @@ mod spec; pub use align::*; pub use angle::*; pub use dir::*; +pub use fr::*; pub use gen::*; pub use length::*; pub use linear::*; |
