Add FlatRegion interface and an associated iterator

This commit is contained in:
aumgn 2012-03-13 11:55:47 +01:00 committed by TomyLobo
parent b2eb2741eb
commit a23e9b857f
5 changed files with 158 additions and 3 deletions

View File

@ -32,7 +32,7 @@ import java.util.HashSet;
* *
* @author sk89q * @author sk89q
*/ */
public class CuboidRegion extends AbstractRegion { public class CuboidRegion extends AbstractRegion implements FlatRegion {
/** /**
* Store the first point. * Store the first point.
*/ */
@ -371,6 +371,16 @@ public class CuboidRegion extends AbstractRegion {
}; };
} }
@Override
public Iterable<Vector2D> asFlatRegion() {
return new Iterable<Vector2D>() {
@Override
public Iterator<Vector2D> iterator() {
return new FlatRegionIterator(CuboidRegion.this);
}
};
}
/** /**
* Returns string representation in the format * Returns string representation in the format
* "(minX, minY, minZ) - (maxX, maxY, maxZ)". * "(minX, minY, minZ) - (maxX, maxY, maxZ)".

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.regions; package com.sk89q.worldedit.regions;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
@ -31,7 +32,7 @@ import com.sk89q.worldedit.data.ChunkStore;
* *
* @author yetanotherx * @author yetanotherx
*/ */
public class CylinderRegion extends AbstractRegion { public class CylinderRegion extends AbstractRegion implements FlatRegion {
private Vector center; private Vector center;
private Vector2D center2D; private Vector2D center2D;
private Vector2D radius; private Vector2D radius;
@ -342,6 +343,16 @@ public class CylinderRegion extends AbstractRegion {
return false; return false;
} }
@Override
public Iterable<Vector2D> asFlatRegion() {
return new Iterable<Vector2D>() {
@Override
public Iterator<Vector2D> iterator() {
return new FlatRegionIterator(CylinderRegion.this);
}
};
}
/** /**
* Returns string representation in the format * Returns string representation in the format
* "(centerX, centerY, centerZ) - (radiusX, radiusZ)" * "(centerX, centerY, centerZ) - (radiusX, radiusZ)"

View File

@ -0,0 +1,27 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com> 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.regions;
import com.sk89q.worldedit.Vector2D;
public interface FlatRegion extends Region {
public Iterable<Vector2D> asFlatRegion();
}

View File

@ -0,0 +1,97 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com> 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.regions;
import java.util.Iterator;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
public class FlatRegionIterator implements Iterator<Vector2D> {
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();
}
}

View File

@ -37,7 +37,7 @@ import com.sk89q.worldedit.data.ChunkStore;
* *
* @author sk89q * @author sk89q
*/ */
public class Polygonal2DRegion extends AbstractRegion { public class Polygonal2DRegion extends AbstractRegion implements FlatRegion {
private List<BlockVector2D> points; private List<BlockVector2D> points;
private BlockVector min; private BlockVector min;
private BlockVector max; private BlockVector max;
@ -469,6 +469,16 @@ public class Polygonal2DRegion extends AbstractRegion {
return new RegionIterator(this); return new RegionIterator(this);
} }
@Override
public Iterable<Vector2D> asFlatRegion() {
return new Iterable<Vector2D>() {
@Override
public Iterator<Vector2D> iterator() {
return new FlatRegionIterator(Polygonal2DRegion.this);
}
};
}
/** /**
* Returns string representation in the format * Returns string representation in the format
* "(x1, z1) - ... - (xN, zN) * (minY - maxY)" * "(x1, z1) - ... - (xN, zN) * (minY - maxY)"