Add iterator over points in rect

This commit is contained in:
Joscha 2024-03-09 00:18:37 +01:00
parent 4f14d70152
commit e31eea8178
2 changed files with 13 additions and 11 deletions

View file

@ -73,25 +73,31 @@ impl Rect {
Self::from_nw_se(nw, se) 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) 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) 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) 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) 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) 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<Item = Vec2> {
(self.north..=self.south)
.flat_map(move |y| (self.west..=self.east).map(move |x| Vec2::new(x, y)))
}
} }
impl Add<Vec2> for Rect { impl Add<Vec2> for Rect {

View file

@ -59,12 +59,8 @@ impl<'a> View<'a> {
// More complicated drawing primitives // More complicated drawing primitives
pub fn rect(&mut self, area: Rect, color: Srgba) { pub fn rect(&mut self, area: Rect, color: Srgba) {
let nw = area.corner_nw(); for pos in area.points() {
let se = area.corner_se(); self.set(pos, color);
for y in nw.y..=se.y {
for x in nw.x..=se.x {
self.set(Vec2::new(x, y), color);
}
} }
} }
} }