diff --git a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index 1fb70d62f..692408e1b 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -32,7 +32,7 @@ import java.util.HashSet; * * @author sk89q */ -public class CuboidRegion extends AbstractRegion { +public class CuboidRegion extends AbstractRegion implements FlatRegion { /** * Store the first point. */ @@ -371,6 +371,16 @@ public class CuboidRegion extends AbstractRegion { }; } + @Override + public Iterable asFlatRegion() { + return new Iterable() { + @Override + public Iterator iterator() { + return new FlatRegionIterator(CuboidRegion.this); + } + }; + } + /** * Returns string representation in the format * "(minX, minY, minZ) - (maxX, maxY, maxZ)". diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index cc9e1a88c..decb82f65 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.regions; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; @@ -31,7 +32,7 @@ import com.sk89q.worldedit.data.ChunkStore; * * @author yetanotherx */ -public class CylinderRegion extends AbstractRegion { +public class CylinderRegion extends AbstractRegion implements FlatRegion { private Vector center; private Vector2D center2D; private Vector2D radius; @@ -342,6 +343,16 @@ public class CylinderRegion extends AbstractRegion { return false; } + @Override + public Iterable asFlatRegion() { + return new Iterable() { + @Override + public Iterator iterator() { + return new FlatRegionIterator(CylinderRegion.this); + } + }; + } + /** * Returns string representation in the format * "(centerX, centerY, centerZ) - (radiusX, radiusZ)" diff --git a/src/main/java/com/sk89q/worldedit/regions/FlatRegion.java b/src/main/java/com/sk89q/worldedit/regions/FlatRegion.java new file mode 100644 index 000000000..4e131f908 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/regions/FlatRegion.java @@ -0,0 +1,27 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 sk89q and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.worldedit.regions; + +import com.sk89q.worldedit.Vector2D; + +public interface FlatRegion extends Region { + + public Iterable asFlatRegion(); +} diff --git a/src/main/java/com/sk89q/worldedit/regions/FlatRegionIterator.java b/src/main/java/com/sk89q/worldedit/regions/FlatRegionIterator.java new file mode 100644 index 000000000..ee56b0895 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/regions/FlatRegionIterator.java @@ -0,0 +1,97 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 sk89q and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.worldedit.regions; + +import java.util.Iterator; + +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.Vector2D; + +public class FlatRegionIterator implements Iterator { + + private Region region; + private int y; + private int minX; + private int nextX; + private int nextZ; + private int maxX; + private int maxZ; + + public FlatRegionIterator(Region region) { + this.region = region; + + Vector min = region.getMinimumPoint(); + Vector max = region.getMaximumPoint(); + + this.y = min.getBlockY(); + + this.minX = min.getBlockX(); + + this.nextX = minX; + this.nextZ = min.getBlockZ(); + + this.maxX = max.getBlockX(); + this.maxZ = max.getBlockZ(); + + forward(); + } + + @Override + public boolean hasNext() { + return nextX != Integer.MIN_VALUE; + } + + private void forward() { + while (hasNext() && !region.contains(new Vector(nextX, y, nextZ))) { + forwardOne(); + } + } + + @Override + public Vector2D next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + + Vector2D answer = new Vector2D(nextX, nextZ); + + forwardOne(); + forward(); + + return answer; + } + + private void forwardOne() { + if (++nextX <= maxX) { + return; + } + nextX = minX; + + if (++nextZ <= maxZ) { + return; + } + nextX = Integer.MIN_VALUE; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java index 41cfbc222..d851c2a2e 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -37,7 +37,7 @@ import com.sk89q.worldedit.data.ChunkStore; * * @author sk89q */ -public class Polygonal2DRegion extends AbstractRegion { +public class Polygonal2DRegion extends AbstractRegion implements FlatRegion { private List points; private BlockVector min; private BlockVector max; @@ -469,6 +469,16 @@ public class Polygonal2DRegion extends AbstractRegion { return new RegionIterator(this); } + @Override + public Iterable asFlatRegion() { + return new Iterable() { + @Override + public Iterator iterator() { + return new FlatRegionIterator(Polygonal2DRegion.this); + } + }; + } + /** * Returns string representation in the format * "(x1, z1) - ... - (xN, zN) * (minY - maxY)"