Cleaner area iterator
This commit is contained in:
parent
7d9d95b8ed
commit
dc2f3d7c05
@ -6,28 +6,36 @@ use super::Vec3;
|
|||||||
pub struct AreaIterator {
|
pub struct AreaIterator {
|
||||||
min: Vec3,
|
min: Vec3,
|
||||||
max: Vec3,
|
max: Vec3,
|
||||||
cur: Vec3
|
pos: Vec3
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AreaIterator {
|
||||||
|
pub fn new(min: Vec3, max: Vec3) -> Self {
|
||||||
|
Self {min, max, pos: min}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for AreaIterator {
|
impl Iterator for AreaIterator {
|
||||||
// TODO: Fix this mess.
|
|
||||||
type Item = Vec3;
|
type Item = Vec3;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
self.cur.x += 1;
|
if self.pos.z > self.max.z {
|
||||||
if self.cur.x > self.max.x {
|
None
|
||||||
self.cur.x = self.min.x;
|
} else {
|
||||||
self.cur.y += 1;
|
let last_pos = self.pos;
|
||||||
if self.cur.y > self.max.y {
|
|
||||||
self.cur.y = self.min.y;
|
self.pos.x += 1;
|
||||||
self.cur.z += 1;
|
if self.pos.x > self.max.x {
|
||||||
if self.cur.z > self.max.z {
|
self.pos.x = self.min.x;
|
||||||
return None;
|
self.pos.y += 1;
|
||||||
|
if self.pos.y > self.max.y {
|
||||||
|
self.pos.y = self.min.y;
|
||||||
|
self.pos.z += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Some(self.cur)
|
Some(last_pos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,11 +82,7 @@ impl Area {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn iterate(&self) -> AreaIterator {
|
pub fn iterate(&self) -> AreaIterator {
|
||||||
AreaIterator {
|
AreaIterator::new(self.min, self.max)
|
||||||
min: self.min,
|
|
||||||
max: self.max,
|
|
||||||
cur: self.min - Vec3::new(1, 0, 0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_contained_block_area(&self) -> Self {
|
pub fn to_contained_block_area(&self) -> Self {
|
||||||
@ -149,12 +153,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_area_iteration() {
|
fn test_area_iteration() {
|
||||||
let a = Area::new(Vec3::new(-1, -1, -1), Vec3::new(1, 1, 1));
|
let a = Area::new(Vec3::new(0, -1, -2), Vec3::new(5, 7, 11));
|
||||||
let mut iter = a.iterate();
|
let mut iter = a.iterate();
|
||||||
|
|
||||||
for z in -1 .. 2 {
|
for z in -2..=11 {
|
||||||
for y in -1 .. 2 {
|
for y in -1..=7 {
|
||||||
for x in -1 .. 2 {
|
for x in 0..=5 {
|
||||||
assert_eq!(iter.next(), Some(Vec3::new(x, y, z)));
|
assert_eq!(iter.next(), Some(Vec3::new(x, y, z)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user