diff options
Diffstat (limited to 'src/calculus.rs')
-rw-r--r-- | src/calculus.rs | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/src/calculus.rs b/src/calculus.rs index 12b0f3d..30ada32 100644 --- a/src/calculus.rs +++ b/src/calculus.rs @@ -184,4 +184,54 @@ pub mod calculus { 0.0) } -}
\ No newline at end of file +} + +pub struct Interval { + pub min: f32, + pub max: f32, +} + +#[allow(unused)] +impl Interval { + pub fn new(min: f32, max: f32) -> Self { + Self { min, max } + } + + pub fn size(&self) -> f32 { + self.max - self.min + } + + pub fn contains(&self, x: f32) -> bool { + self.min <= x && x <= self.max + } + + pub fn surrounds(&self, x: f32) -> bool { + self.min < x && x < self.max + } + + pub fn clamp(&self, x: f32) -> f32 { + if x < self.min { return self.min } + if x > self.max { return self.max } + x + } +} + +impl Default for Interval { + fn default() -> Self { + Self { + min: f32::NEG_INFINITY, + max: f32::INFINITY + } + } +} + +#[allow(unused)] +pub const INTERVAL_EMPTY: Interval = Interval { + min: f32::INFINITY, + max: f32::NEG_INFINITY +}; +#[allow(unused)] +pub const INTERVAL_UNIVERSE: Interval = Interval { + min: f32::NEG_INFINITY, + max: f32::INFINITY +};
\ No newline at end of file |