diff --git a/showbits-common/src/rect.rs b/showbits-common/src/rect.rs index 5d29d4f..f967525 100644 --- a/showbits-common/src/rect.rs +++ b/showbits-common/src/rect.rs @@ -73,25 +73,31 @@ impl Rect { Self::from_nw_se(nw, se) } - pub const fn corner_nw(&self) -> Vec2 { + pub const fn corner_nw(self) -> Vec2 { Vec2::new(self.west, self.north) } - pub const fn corner_ne(&self) -> Vec2 { + pub const fn corner_ne(self) -> Vec2 { Vec2::new(self.east, self.north) } - pub const fn corner_sw(&self) -> Vec2 { + pub const fn corner_sw(self) -> Vec2 { Vec2::new(self.west, self.south) } - pub const fn corner_se(&self) -> Vec2 { + pub const fn corner_se(self) -> Vec2 { Vec2::new(self.east, self.south) } - pub const fn size(&self) -> Vec2 { + pub const fn size(self) -> Vec2 { Vec2::new(self.east - self.west + 1, self.south - self.north + 1) } + + /// An iterator of all points contained within the rectangle. + pub fn points(self) -> impl Iterator { + (self.north..=self.south) + .flat_map(move |y| (self.west..=self.east).map(move |x| Vec2::new(x, y))) + } } impl Add for Rect { diff --git a/showbits-common/src/view.rs b/showbits-common/src/view.rs index 6bfd009..ba91d36 100644 --- a/showbits-common/src/view.rs +++ b/showbits-common/src/view.rs @@ -59,12 +59,8 @@ impl<'a> View<'a> { // More complicated drawing primitives pub fn rect(&mut self, area: Rect, color: Srgba) { - let nw = area.corner_nw(); - let se = area.corner_se(); - for y in nw.y..=se.y { - for x in nw.x..=se.x { - self.set(Vec2::new(x, y), color); - } + for pos in area.points() { + self.set(pos, color); } } }