From 5fd5bb979765ec47e04e3f1f34d362d8d0c9c41e Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Mon, 3 Mar 2025 23:07:03 +0700 Subject: refactor move interval related to calculus.rs --- src/calculus.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'src/calculus.rs') 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 -- cgit v1.2.3-70-g09d2