pub struct Span { /* private fields */ }
Expand description
A closed interval of coordinates in one dimension.
Represents the range [start, stop]
.
Implementations§
Source§impl Span
impl Span
Sourcepub const unsafe fn new_unchecked(start: i64, stop: i64) -> Self
pub const unsafe fn new_unchecked(start: i64, stop: i64) -> Self
Sourcepub const fn from_point(x: i64) -> Self
pub const fn from_point(x: i64) -> Self
Creates a span of zero length encompassing the given point.
Sourcepub const fn with_start_and_length(start: i64, length: i64) -> Self
pub const fn with_start_and_length(start: i64, length: i64) -> Self
Creates a span of the given length starting from start
.
Sourcepub const fn with_stop_and_length(stop: i64, length: i64) -> Self
pub const fn with_stop_and_length(stop: i64, length: i64) -> Self
Creates a span of the given length ending at stop
.
Sourcepub const fn with_point_and_length(sign: Sign, point: i64, length: i64) -> Self
pub const fn with_point_and_length(sign: Sign, point: i64, length: i64) -> Self
Sourcepub const fn expand(self, sign: Sign, amount: i64) -> Self
pub const fn expand(self, sign: Sign, amount: i64) -> Self
Creates a new Span
expanded by amount
in the direction indicated by pos
.
Sourcepub const fn expand_all(self, amount: i64) -> Self
pub const fn expand_all(self, amount: i64) -> Self
Creates a new Span
expanded by amount
in both directions.
Sourcepub fn dist_to(&self, point: i64) -> i64
pub fn dist_to(&self, point: i64) -> i64
Gets the shortest distance between this span and a point.
§Example
let span = Span::new(10, 20);
assert_eq!(span.dist_to(4), 6);
assert_eq!(span.dist_to(12), 0);
assert_eq!(span.dist_to(27), 7);
Sourcepub fn from_center_span(center: i64, span: i64) -> Self
pub fn from_center_span(center: i64, span: i64) -> Self
Creates a new Span
with center center
and length span
.
span
must be a positive, even integer.
§Example
let span = Span::from_center_span(0, 40);
assert_eq!(span, Span::new(-20, 20));
§Panics
Passing an odd span
to this method results in a panic:
let span = Span::from_center_span(0, 25);
Passing a negative span
to this method also results in a panic:
let span = Span::from_center_span(0, -200);
Sourcepub fn from_center_span_gridded(center: i64, span: i64, grid: i64) -> Self
pub fn from_center_span_gridded(center: i64, span: i64, grid: i64) -> Self
Creates a new Span
with center center
and length span
and snap the edges to the
grid.
§Example
let span = Span::from_center_span_gridded(0, 40, 5);
assert_eq!(span, Span::new(-20, 20));
let span = Span::from_center_span_gridded(35, 40, 5);
assert_eq!(span, Span::new(15, 55));
§Panics
This function panics if span
is negative, odd, or not an integer multiple of grid
.
Sourcepub const fn intersects(&self, other: &Self) -> bool
pub const fn intersects(&self, other: &Self) -> bool
Checks if the span intersects with the Span
other
.
Sourcepub fn merge(spans: impl IntoIterator<Item = Self>) -> Self
pub fn merge(spans: impl IntoIterator<Item = Self>) -> Self
Creates a new minimal Span
that contains all of the elements of spans
.
Sourcepub fn union(self, other: Self) -> Self
pub fn union(self, other: Self) -> Self
Calculates the smallest interval containing this span and other
.
Sourcepub fn union_all<T>(spans: impl Iterator<Item = T>) -> Selfwhere
T: Into<Self>,
pub fn union_all<T>(spans: impl Iterator<Item = T>) -> Selfwhere
T: Into<Self>,
Calculates the minimal bounding interval of all spans provided.
§Example
let spans = vec![
Span::new(10, 40),
Span::new(35, 60),
Span::new(20, 30),
Span::new(-10, 5),
];
assert_eq!(Span::union_all(spans.into_iter()), Span::new(-10, 60));
§Panics
This function panics if the provided iterator has no elements.
If your iterator may be empty, consider using Span::union_all_option
.
Sourcepub fn union_all_option<T>(spans: impl Iterator<Item = T>) -> Option<Self>
pub fn union_all_option<T>(spans: impl Iterator<Item = T>) -> Option<Self>
Calculates the minimal bounding interval of all Option<Span>
s provided.
All None
elements in the iterator are ignored.
If the iterator has no Some(_)
elements, this function returns None
.
§Example
let spans = vec![
Some(Span::new(10, 40)),
Some(Span::new(35, 60)),
None,
Some(Span::new(20, 30)),
None,
Some(Span::new(-10, 5)),
];
assert_eq!(Span::union_all_option(spans.into_iter()), Some(Span::new(-10, 60)));
Sourcepub fn intersection(self, other: Self) -> Option<Self>
pub fn intersection(self, other: Self) -> Option<Self>
Calculates the intersection of this span with other
.
Sourcepub fn add_point(self, pos: i64) -> Self
pub fn add_point(self, pos: i64) -> Self
Returns a new Span
representing the union of the current span with the given point.
Sourcepub const fn shrink_all(self, amount: i64) -> Self
pub const fn shrink_all(self, amount: i64) -> Self
Shrinks the span by the given amount on all sides.
Sourcepub fn dist_to_span(self, other: Span) -> i64
pub fn dist_to_span(self, other: Span) -> i64
The minimum separation between this span and other
.
§Example
let s1 = Span::new(10, 20);
let s2 = Span::new(30, 50);
let s3 = Span::new(25, 40);
assert_eq!(s1.dist_to_span(s2), 10);
assert_eq!(s1.dist_to_span(s3), 5);
assert_eq!(s2.dist_to_span(s3), 0);
assert_eq!(s3.dist_to_span(s3), 0);
Sourcepub fn has_integer_center(&self) -> bool
pub fn has_integer_center(&self) -> bool
Returns whether the span’s center is at an integer coordinate.
§Example
let span = Span::new(0, 100);
assert!(span.has_integer_center());
let span = Span::new(0, 99);
assert!(!span.has_integer_center());
Trait Implementations§
Source§impl BoundingUnion<Span> for Span
impl BoundingUnion<Span> for Span
Source§impl Contains<Span> for Span
impl Contains<Span> for Span
Source§fn contains(&self, other: &Span) -> Containment
fn contains(&self, other: &Span) -> Containment
Source§fn partially_intersects(&self, other: &T) -> bool
fn partially_intersects(&self, other: &T) -> bool
other
is fully or partially enclosed in this shape.