From c2b4683a1a6c617fdae36c29523c03dc6c57e2ea Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 18 Jan 2021 22:05:57 +0100 Subject: [PATCH] GridView: add range() function add GridWindowIterator from RangeInclusive --- src/grid/mod.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 7e67dce..8330c53 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -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>; +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +impl GridView { + pub fn range(&self) -> RangeInclusive> { + 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>> for GridWindowIterator { } } +impl From>> for GridWindowIterator { + fn from(range: RangeInclusive>) -> 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;