Skip to content

Commit

Permalink
Updated Bounding boxes to be more iterable.
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityfox committed Jul 16, 2016
1 parent bf360b7 commit 9e941ed
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
import org.spongepowered.api.world.Location;

import java.io.Serializable;
import java.util.Iterator;

public class BoundingBox2 implements Serializable {
public class BoundingBox2 implements Serializable, Iterable<Vector2i> {

public final Vector2i a;
public final Vector2i b;
Expand Down Expand Up @@ -104,4 +105,32 @@ public String toString() {
return "{(" + a.getX() + ", " + a.getY() + "), ("
+ b.getX() + ", " + b.getY() + ")}";
}

@Override
public BBIterator iterator() {
return new BBIterator();
}

private class BBIterator implements Iterator<Vector2i> {

int x = a.getX(), y = a.getY();

@Override
public boolean hasNext() {
return y <= b.getY();
}

@Override
public Vector2i next() {
if (hasNext()) {
Vector2i v = new Vector2i(x, y);
x++;
if (x > b.getX()) {
x = a.getX();
y++;
}
return v;
} else return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
import org.spongepowered.api.world.Location;

import java.io.Serializable;
import java.util.Iterator;

public class BoundingBox3 implements Serializable {
public class BoundingBox3 implements Serializable , Iterable<Vector3i>{

public final Vector3i a;
public final Vector3i b;
Expand Down Expand Up @@ -97,4 +98,35 @@ public String toString() {
+ b.getX() + ", " + b.getY() + ", " + b.getZ() + ")}";
}

@Override
public Iterator<Vector3i> iterator() {
return new BBIterator();
}

private class BBIterator implements Iterator<Vector3i> {

int x = a.getX(), y = a.getY(), z = a.getZ();

@Override
public boolean hasNext() {
return y <= b.getY();
}

@Override
public Vector3i next() {
if (hasNext()) {
Vector3i v = new Vector3i(x, y, z);
x++;
if (x > b.getX()) {
x = a.getX();
z++;
}
if (z > b.getZ()) {
z = a.getZ();
y++;
}
return v;
} else return null;
}
}
}

0 comments on commit 9e941ed

Please sign in to comment.