GridView: add range() function
add GridWindowIterator from RangeInclusive
This commit is contained in:
parent
afaa9d220b
commit
c2b4683a1a
1 changed files with 28 additions and 1 deletions
|
@ -1,6 +1,6 @@
|
||||||
use {
|
use {
|
||||||
std::{
|
std::{
|
||||||
ops::{Range}
|
ops::{Range, RangeInclusive}
|
||||||
},
|
},
|
||||||
cgmath::{Point2},
|
cgmath::{Point2},
|
||||||
crate::{
|
crate::{
|
||||||
|
@ -15,6 +15,24 @@ pub use offset::GridOffset;
|
||||||
|
|
||||||
pub trait GridView = IndexView<Point2<i16>>;
|
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 {
|
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 {
|
impl Iterator for GridWindowIterator {
|
||||||
type Item = Point2<i16>;
|
type Item = Point2<i16>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue