Interval

Struct Interval 

Source
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

Source

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 interval
  • max - 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
Source

pub fn min(&self) -> Real

Returns the minimum (lower) bound of the interval.

§Returns

The minimum value as a Real.

§Examples
use phys_geom::Interval;

let interval = Interval::new(0.0, 1.0);
assert_eq!(interval.min(), 0.0);
Source

pub fn max(&self) -> Real

Returns the maximum (upper) bound of the interval.

§Returns

The maximum value as a Real.

§Examples
use phys_geom::Interval;

let interval = Interval::new(0.0, 1.0);
assert_eq!(interval.max(), 1.0);
Source

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§

Source§

impl Clone for Interval

Source§

fn clone(&self) -> Interval

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Interval

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Interval

Source§

fn eq(&self, other: &Interval) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Interval

Source§

impl StructuralPartialEq for Interval

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,