GridView: add range() function

add GridWindowIterator from RangeInclusive
This commit is contained in:
Michael Sippel 2021-01-18 22:05:57 +01:00
parent afaa9d220b
commit c2b4683a1a
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -1,6 +1,6 @@
use {
std::{
ops::{Range}
ops::{Range, RangeInclusive}
},
cgmath::{Point2},
crate::{
@ -15,6 +15,24 @@ pub use offset::GridOffset;
pub trait GridView = IndexView<Point2<i16>>;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<Item> GridView<Item = Item> {
pub fn range(&self) -> RangeInclusive<Point2<i16>> {
let area = self.area().unwrap_or(Vec::new());
Point2::new(
area.iter().map(|p| p.x).min().unwrap_or(i16::MIN),
area.iter().map(|p| p.y).min().unwrap_or(i16::MIN)
) ..=
Point2::new(
area.iter().map(|p| p.x).max().unwrap_or(i16::MAX),
area.iter().map(|p| p.y).max().unwrap_or(i16::MAX)
)
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct GridWindowIterator {
@ -31,6 +49,15 @@ impl From<Range<Point2<i16>>> for GridWindowIterator {
}
}
impl From<RangeInclusive<Point2<i16>>> for GridWindowIterator {
fn from(range: RangeInclusive<Point2<i16>>) -> Self {
GridWindowIterator {
next: *range.start(),
range: *range.start() .. Point2::new(range.end().x+1, range.end().y+1)
}
}
}
impl Iterator for GridWindowIterator {
type Item = Point2<i16>;