summaryrefslogtreecommitdiff
path: root/src/geom/ratio.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-07-02 19:59:52 +0200
committerLaurenz <laurmaedje@gmail.com>2023-07-02 20:07:43 +0200
commitebfdb1dafa430786db10dad2ef7d5467c1bdbed1 (patch)
tree2bbc24ddb4124c4bb14dec0e536129d4de37b056 /src/geom/ratio.rs
parent3ab19185093d7709f824b95b979060ce125389d8 (diff)
Move everything into `crates/` directory
Diffstat (limited to 'src/geom/ratio.rs')
-rw-r--r--src/geom/ratio.rs133
1 files changed, 0 insertions, 133 deletions
diff --git a/src/geom/ratio.rs b/src/geom/ratio.rs
deleted file mode 100644
index fe87dd6c..00000000
--- a/src/geom/ratio.rs
+++ /dev/null
@@ -1,133 +0,0 @@
-use super::*;
-
-/// A ratio of a whole.
-///
-/// _Note_: `50%` is represented as `0.5` here, but stored as `50.0` in the
-/// corresponding [literal](crate::syntax::ast::Numeric).
-#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-pub struct Ratio(Scalar);
-
-impl Ratio {
- /// A ratio of `0%` represented as `0.0`.
- pub const fn zero() -> Self {
- Self(Scalar(0.0))
- }
-
- /// A ratio of `100%` represented as `1.0`.
- pub const fn one() -> Self {
- Self(Scalar(1.0))
- }
-
- /// Create a new ratio from a value, where `1.0` means `100%`.
- pub const fn new(ratio: f64) -> Self {
- Self(Scalar(ratio))
- }
-
- /// Get the underlying ratio.
- pub const fn get(self) -> f64 {
- (self.0).0
- }
-
- /// Whether the ratio is zero.
- pub fn is_zero(self) -> bool {
- self.0 == 0.0
- }
-
- /// Whether the ratio is one.
- pub fn is_one(self) -> bool {
- self.0 == 1.0
- }
-
- /// The absolute value of this ratio.
- pub fn abs(self) -> Self {
- Self::new(self.get().abs())
- }
-
- /// Return the ratio of the given `whole`.
- pub fn of<T: Numeric>(self, whole: T) -> T {
- let resolved = whole * self.get();
- if resolved.is_finite() {
- resolved
- } else {
- T::zero()
- }
- }
-}
-
-impl Debug for Ratio {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "{}%", round_2(100.0 * self.get()))
- }
-}
-
-impl Neg for Ratio {
- type Output = Self;
-
- fn neg(self) -> Self {
- Self(-self.0)
- }
-}
-
-impl Add for Ratio {
- type Output = Self;
-
- fn add(self, other: Self) -> Self {
- Self(self.0 + other.0)
- }
-}
-
-sub_impl!(Ratio - Ratio -> Ratio);
-
-impl Mul for Ratio {
- type Output = Self;
-
- fn mul(self, other: Self) -> Self {
- Self(self.0 * other.0)
- }
-}
-
-impl Mul<f64> for Ratio {
- type Output = Self;
-
- fn mul(self, other: f64) -> Self {
- Self(self.0 * other)
- }
-}
-
-impl Mul<Ratio> for f64 {
- type Output = Ratio;
-
- fn mul(self, other: Ratio) -> Ratio {
- other * self
- }
-}
-
-impl Div<f64> for Ratio {
- type Output = Self;
-
- fn div(self, other: f64) -> Self {
- Self(self.0 / other)
- }
-}
-
-impl Div<Ratio> for f64 {
- type Output = Self;
-
- fn div(self, other: Ratio) -> Self {
- self / other.get()
- }
-}
-
-impl Div for Ratio {
- type Output = f64;
-
- fn div(self, other: Self) -> f64 {
- self.get() / other.get()
- }
-}
-
-assign_impl!(Ratio += Ratio);
-assign_impl!(Ratio -= Ratio);
-assign_impl!(Ratio *= Ratio);
-assign_impl!(Ratio *= f64);
-assign_impl!(Ratio /= f64);