diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-10-03 15:07:57 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-10-03 15:07:57 +0200 |
| commit | 95bae5725cf6495644e2593f8492f1cd0e5bd3c1 (patch) | |
| tree | 919dd90cac7623bcbbc09d9c92399eaa65e537f2 /src/geom.rs | |
| parent | 0fc25d732d7cbc37cf801645849d1060f2cec4a3 (diff) | |
Int, Float, Relative and Linear values 🍉
Diffstat (limited to 'src/geom.rs')
| -rw-r--r-- | src/geom.rs | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/geom.rs b/src/geom.rs index 6aa875f1..05143f22 100644 --- a/src/geom.rs +++ b/src/geom.rs @@ -3,6 +3,9 @@ #[doc(no_inline)] pub use kurbo::*; +use std::fmt::{self, Debug, Formatter}; +use std::ops::*; + use crate::layout::primitive::{Dir, GenAlign, LayoutAlign, LayoutSystem, SpecAxis}; /// Additional methods for [sizes]. @@ -176,3 +179,128 @@ impl<T> Sides<T> { } } } + +/// A function that depends linearly on one value. +/// +/// This represents a function `f(x) = rel * x + abs`. +#[derive(Copy, Clone, PartialEq)] +pub struct Linear { + /// The relative part. + pub rel: f64, + /// The absolute part. + pub abs: f64, +} + +impl Linear { + /// The constant zero function. + pub const ZERO: Linear = Linear { rel: 0.0, abs: 0.0 }; + + /// Create a new linear function. + pub fn new(rel: f64, abs: f64) -> Self { + Self { rel, abs } + } + + /// Create a new linear function with only a relative component. + pub fn rel(rel: f64) -> Self { + Self { rel, abs: 0.0 } + } + + /// Create a new linear function with only an absolute component. + pub fn abs(abs: f64) -> Self { + Self { rel: 0.0, abs } + } + + /// Evaluate the linear function with the given value. + pub fn eval(self, x: f64) -> f64 { + self.rel * x + self.abs + } +} + +impl Add for Linear { + type Output = Self; + + fn add(self, other: Self) -> Self { + Self { + rel: self.rel + other.rel, + abs: self.abs + other.abs, + } + } +} + +impl AddAssign for Linear { + fn add_assign(&mut self, other: Self) { + self.rel += other.rel; + self.abs += other.abs; + } +} + +impl Sub for Linear { + type Output = Self; + + fn sub(self, other: Self) -> Self { + Self { + rel: self.rel - other.rel, + abs: self.abs - other.abs, + } + } +} + +impl SubAssign for Linear { + fn sub_assign(&mut self, other: Self) { + self.rel -= other.rel; + self.abs -= other.abs; + } +} + +impl Mul<f64> for Linear { + type Output = Self; + + fn mul(self, other: f64) -> Self { + Self { + rel: self.rel + other, + abs: self.abs + other, + } + } +} + +impl MulAssign<f64> for Linear { + fn mul_assign(&mut self, other: f64) { + self.rel *= other; + self.abs *= other; + } +} + +impl Mul<Linear> for f64 { + type Output = Linear; + + fn mul(self, other: Linear) -> Linear { + Linear { + rel: self + other.rel, + abs: self + other.abs, + } + } +} + +impl Div<f64> for Linear { + type Output = Self; + + fn div(self, other: f64) -> Self { + Self { + rel: self.rel / other, + abs: self.abs / other, + } + } +} + +impl DivAssign<f64> for Linear { + fn div_assign(&mut self, other: f64) { + self.rel /= other; + self.abs /= other; + } +} + +impl Debug for Linear { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}x + {}", self.rel, self.abs) + } +} |
