pub struct Interval { /* private fields */ }
Expand description
A one-dimensional interval on the real number line.
An interval represents a closed range [min, max] where both endpoints are included. This is a fundamental building block for many geometric algorithms, particularly in collision detection and spatial queries.
§Mathematical Representation
The interval represents all real numbers x such that: min <= x <= max
§Examples
use phys_geom::Interval;
// Create an interval representing the range [0.0, 1.0]
let interval = Interval::new(0.0, 1.0);
// Check if a value is within the interval
let contains_0_5 = interval.min() <= 0.5 && 0.5 <= interval.max();
assert!(contains_0_5);
§Common Applications
- Separating Axis Theorem: Testing overlap of projected shapes
- AABB Operations: Computing intersections along specific axes
- Numerical Robustness: Handling floating-point precision issues
- Spatial Partitioning: Determining object overlaps in 1D
§Invariants
The struct maintains the invariant that min <= max
. This is enforced
at construction time and preserved by all operations.
§Memory Layout
The struct contains exactly two Real
values and is designed to be
efficient for both computation and memory usage.
Implementations§
Source§impl Interval
impl Interval
Sourcepub fn new(min: Real, max: Real) -> Self
pub fn new(min: Real, max: Real) -> Self
Creates a new interval with the specified bounds.
This function creates a closed interval [min, max] that includes both endpoints.
§Arguments
min
- The minimum (lower) bound of the intervalmax
- The maximum (upper) bound of the interval
§Returns
A new Interval
instance representing the range [min, max].
§Panics
Panics if min > max
, as this would violate the interval invariant.
§Examples
use phys_geom::Interval;
// Create a valid interval
let interval = Interval::new(0.0, 1.0);
assert_eq!(interval.min(), 0.0);
assert_eq!(interval.max(), 1.0);
// This would panic: Interval::new(1.0, 0.0);
§Edge Cases
min == max
: Creates a zero-width interval (single point)min < max
: Creates a normal interval with positive width
Sourcepub fn overlaps(&self, other: &Self) -> bool
pub fn overlaps(&self, other: &Self) -> bool
Checks if this interval overlaps with another interval.
Two intervals overlap if they have at least one point in common.
Since these are closed intervals, the overlap condition is:
self.min <= other.max && self.max >= other.min
§Arguments
other
- The other interval to test for overlap
§Returns
true
if the intervals overlap, false
otherwise.
§Examples
use phys_geom::Interval;
let a = Interval::new(0.0, 2.0);
let b = Interval::new(1.0, 3.0);
let c = Interval::new(2.5, 4.0);
assert!(a.overlaps(&b)); // Overlap in [1.0, 2.0]
assert!(!a.overlaps(&c)); // No overlap
assert!(b.overlaps(&c)); // Overlap at point 2.5
§Edge Cases
- Touching intervals:
[0,1]
and[1,2]
overlap at point 1 - Zero-width intervals:
[1,1]
overlaps with[0,2]
but not with[2,3]
- Identical intervals: Always overlap with themselves
§Mathematical Definition
The overlap condition can be written as:
overlap = [max(self.min, other.min), min(self.max, other.max)]
is non-empty
Note: This is a closed interval, meaning that the bounds are included in the overlap calculation.
Trait Implementations§
impl Copy for Interval
impl StructuralPartialEq for Interval
Auto Trait Implementations§
impl Freeze for Interval
impl RefUnwindSafe for Interval
impl Send for Interval
impl Sync for Interval
impl Unpin for Interval
impl UnwindSafe for Interval
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.