iron_shapes/interval.rs
1// SPDX-FileCopyrightText: 2018-2022 Thomas Kramer
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5//! A one dimensional interval.
6
7use crate::CoordinateType;
8
9/// A one dimensional interval which is represented by a start and end coordinate.
10#[derive(Clone, Copy, PartialEq, Eq, Debug)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub struct Interval<T>(T, T)
13where
14 T: CoordinateType;
15
16impl<T: CoordinateType> Interval<T> {
17 /// Create a new interval.
18 pub fn new(lower: T, upper: T) -> Self {
19 assert!(lower <= upper, "interval bounds must be ordered");
20 Interval(lower, upper)
21 }
22
23 /// Get the start coordinate.
24 pub fn start(&self) -> T {
25 self.0
26 }
27
28 /// Get the end coordinate.
29 pub fn end(&self) -> T {
30 self.1
31 }
32
33 // /// Compute the intersection of two intervals.
34 // pub fn intersection(&self, other: &Self) -> Self {
35 // match self {
36 // Interval(None) => Interval(None),
37 // Interval(Some((l1, u1))) => match other {
38 // Interval(None) => Interval(None),
39 // Interval(Some((l2, u2))) =>
40 // Interval::new(max(*l1, *l2), min(*u1, *u2))
41 // }
42 // }
43 // }
44
45 // /// Test if the interval is empty.
46 // pub fn is_empty(&self) -> bool {
47 // self.0.is_none()
48 // }
49}
50
51// #[test]
52// fn test_interval_intersection() {
53// let a = Interval::new(0, 10);
54// let b = Interval::new(5, 15);
55// assert_eq!(a.intersection(&b), Interval::new(5, 10));
56//
57// let a = Interval::new(0, 10);
58// let b = Interval::new(11, 15);
59// assert_eq!(a.intersection(&b), Interval(None));
60// }