2011-02-20 01:44:39 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2011-02-20 01:44:39 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2011-02-20 01:44:39 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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 Lesser General Public License
|
|
|
|
* for more details.
|
2011-02-20 01:44:39 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2011-02-20 01:44:39 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-04 22:03:18 +00:00
|
|
|
*/
|
2011-02-20 01:44:39 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.regions;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import com.sk89q.worldedit.BlockVector;
|
|
|
|
import com.sk89q.worldedit.BlockVector2D;
|
2011-12-13 03:20:31 +00:00
|
|
|
import com.sk89q.worldedit.LocalWorld;
|
2011-02-20 01:44:39 +00:00
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
import com.sk89q.worldedit.Vector2D;
|
2014-04-04 01:32:51 +00:00
|
|
|
import com.sk89q.worldedit.regions.iterator.FlatRegion3DIterator;
|
|
|
|
import com.sk89q.worldedit.regions.iterator.FlatRegionIterator;
|
2014-04-05 09:59:38 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2011-02-20 01:44:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a 2D polygonal region.
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
2012-03-13 10:55:47 +00:00
|
|
|
public class Polygonal2DRegion extends AbstractRegion implements FlatRegion {
|
2012-01-03 02:20:19 +00:00
|
|
|
private List<BlockVector2D> points;
|
2012-03-25 10:37:03 +00:00
|
|
|
private Vector2D min;
|
|
|
|
private Vector2D max;
|
2012-01-03 02:20:19 +00:00
|
|
|
private int minY;
|
|
|
|
private int maxY;
|
|
|
|
private boolean hasY = false;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
2011-12-13 03:20:31 +00:00
|
|
|
* Construct the region
|
2011-02-20 01:44:39 +00:00
|
|
|
*/
|
|
|
|
public Polygonal2DRegion() {
|
2014-04-05 09:59:38 +00:00
|
|
|
this((World) null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public Polygonal2DRegion(LocalWorld world) {
|
|
|
|
this((World) world);
|
2011-12-13 03:20:31 +00:00
|
|
|
}
|
2012-01-03 02:20:19 +00:00
|
|
|
|
2011-12-13 03:20:31 +00:00
|
|
|
/**
|
|
|
|
* Construct the region.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-12-13 03:20:31 +00:00
|
|
|
* @param world
|
|
|
|
*/
|
2014-04-05 09:59:38 +00:00
|
|
|
public Polygonal2DRegion(World world) {
|
2012-01-03 14:52:21 +00:00
|
|
|
this(world, Collections.<BlockVector2D>emptyList(), 0, 0);
|
2011-02-20 01:44:39 +00:00
|
|
|
hasY = false;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-04-05 09:59:38 +00:00
|
|
|
@Deprecated
|
|
|
|
public Polygonal2DRegion(LocalWorld world, List<BlockVector2D> points, int minY, int maxY) {
|
|
|
|
this((World) world, points, minY, maxY);
|
|
|
|
}
|
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Construct the region.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-12-13 03:20:31 +00:00
|
|
|
* @param world
|
2011-02-20 01:44:39 +00:00
|
|
|
* @param points
|
|
|
|
* @param minY
|
|
|
|
* @param maxY
|
|
|
|
*/
|
2014-04-05 09:59:38 +00:00
|
|
|
public Polygonal2DRegion(World world, List<BlockVector2D> points, int minY, int maxY) {
|
2012-01-03 14:57:29 +00:00
|
|
|
super(world);
|
2012-01-01 13:08:22 +00:00
|
|
|
this.points = new ArrayList<BlockVector2D>(points);
|
2011-02-20 01:44:39 +00:00
|
|
|
this.minY = minY;
|
|
|
|
this.maxY = maxY;
|
|
|
|
hasY = true;
|
|
|
|
recalculate();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-12-27 11:07:11 +00:00
|
|
|
public Polygonal2DRegion(Polygonal2DRegion region) {
|
|
|
|
this(region.world, region.points, region.minY, region.maxY);
|
|
|
|
hasY = region.hasY;
|
|
|
|
}
|
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Get the list of points.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public List<BlockVector2D> getPoints() {
|
|
|
|
return Collections.unmodifiableList(points);
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Recalculate the bounding box of this polygonal region. This should be
|
|
|
|
* called after points have been changed.
|
|
|
|
*/
|
|
|
|
protected void recalculate() {
|
|
|
|
if (points.size() == 0) {
|
2012-03-25 10:37:03 +00:00
|
|
|
min = new Vector2D(0, 0);
|
|
|
|
minY = 0;
|
|
|
|
max = new Vector2D(0, 0);
|
|
|
|
maxY = 0;
|
2011-02-20 01:44:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
int minX = points.get(0).getBlockX();
|
|
|
|
int minZ = points.get(0).getBlockZ();
|
|
|
|
int maxX = points.get(0).getBlockX();
|
|
|
|
int maxZ = points.get(0).getBlockZ();
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
for (BlockVector2D v : points) {
|
|
|
|
int x = v.getBlockX();
|
|
|
|
int z = v.getBlockZ();
|
|
|
|
if (x < minX) minX = x;
|
|
|
|
if (z < minZ) minZ = z;
|
|
|
|
if (x > maxX) maxX = x;
|
|
|
|
if (z > maxZ) maxZ = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
int oldMinY = minY;
|
|
|
|
int oldMaxY = maxY;
|
|
|
|
minY = Math.min(oldMinY, oldMaxY);
|
|
|
|
maxY = Math.max(oldMinY, oldMaxY);
|
|
|
|
|
2012-03-02 04:47:19 +00:00
|
|
|
minY = Math.min(Math.max(0, minY), world == null ? 255 : world.getMaxY());
|
|
|
|
maxY = Math.min(Math.max(0, maxY), world == null ? 255 : world.getMaxY());
|
2011-12-13 03:20:31 +00:00
|
|
|
|
2012-03-25 10:37:03 +00:00
|
|
|
min = new Vector2D(minX, minZ);
|
|
|
|
max = new Vector2D(maxX, maxZ);
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Add a point to the list.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @param pt
|
|
|
|
*/
|
|
|
|
public void addPoint(Vector2D pt) {
|
|
|
|
points.add(pt.toBlockVector2D());
|
|
|
|
recalculate();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Add a point to the list.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @param pt
|
|
|
|
*/
|
|
|
|
public void addPoint(BlockVector2D pt) {
|
|
|
|
points.add(pt);
|
|
|
|
recalculate();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Add a point to the list.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @param pt
|
|
|
|
*/
|
|
|
|
public void addPoint(Vector pt) {
|
|
|
|
points.add(new BlockVector2D(pt.getBlockX(), pt.getBlockZ()));
|
|
|
|
recalculate();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2012-01-03 03:44:34 +00:00
|
|
|
/**
|
|
|
|
* Get the minimum Y.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2012-01-03 03:44:34 +00:00
|
|
|
* @return min y
|
|
|
|
*/
|
2012-04-16 05:41:14 +00:00
|
|
|
public int getMinimumY() {
|
|
|
|
return minY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
2012-01-03 03:44:34 +00:00
|
|
|
public int getMininumY() {
|
|
|
|
return minY;
|
|
|
|
}
|
|
|
|
|
2011-04-03 18:03:57 +00:00
|
|
|
/**
|
|
|
|
* Set the minimum Y.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-04-03 18:03:57 +00:00
|
|
|
* @param y
|
|
|
|
*/
|
|
|
|
public void setMinimumY(int y) {
|
|
|
|
hasY = true;
|
|
|
|
minY = y;
|
|
|
|
recalculate();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-04-03 18:03:57 +00:00
|
|
|
/**
|
2012-01-03 03:44:34 +00:00
|
|
|
* Get the maximum Y.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2012-01-03 03:44:34 +00:00
|
|
|
* @return max y
|
|
|
|
*/
|
|
|
|
public int getMaximumY() {
|
|
|
|
return maxY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the maximum Y.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-04-03 18:03:57 +00:00
|
|
|
* @param y
|
|
|
|
*/
|
|
|
|
public void setMaximumY(int y) {
|
|
|
|
hasY = true;
|
|
|
|
maxY = y;
|
|
|
|
recalculate();
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the lower point of a region.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return min. point
|
|
|
|
*/
|
|
|
|
public Vector getMinimumPoint() {
|
2012-03-25 10:37:03 +00:00
|
|
|
return min.toVector(minY);
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the upper point of a region.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return max. point
|
|
|
|
*/
|
|
|
|
public Vector getMaximumPoint() {
|
2012-03-25 10:37:03 +00:00
|
|
|
return max.toVector(maxY);
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of blocks in the region.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return number of blocks
|
|
|
|
*/
|
|
|
|
public int getArea() {
|
|
|
|
double area = 0;
|
|
|
|
int i, j = points.size() - 1;
|
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (i = 0; i < points.size(); ++i) {
|
2011-02-20 01:44:39 +00:00
|
|
|
area += (points.get(j).getBlockX() + points.get(i).getBlockX())
|
|
|
|
* (points.get(j).getBlockZ() - points.get(i).getBlockZ());
|
|
|
|
j = i;
|
|
|
|
}
|
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
return (int) Math.floor(Math.abs(area * 0.5)
|
2011-02-20 01:44:39 +00:00
|
|
|
* (maxY - minY + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get X-size.
|
|
|
|
*
|
|
|
|
* @return width
|
|
|
|
*/
|
|
|
|
public int getWidth() {
|
2012-12-05 23:31:08 +00:00
|
|
|
return max.getBlockX() - min.getBlockX() + 1;
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Y-size.
|
|
|
|
*
|
|
|
|
* @return height
|
|
|
|
*/
|
|
|
|
public int getHeight() {
|
2012-12-05 23:31:08 +00:00
|
|
|
return maxY - minY + 1;
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Z-size.
|
|
|
|
*
|
|
|
|
* @return length
|
|
|
|
*/
|
|
|
|
public int getLength() {
|
2012-12-05 23:31:08 +00:00
|
|
|
return max.getBlockZ() - min.getBlockZ() + 1;
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the region.
|
|
|
|
*
|
2012-03-30 04:06:43 +00:00
|
|
|
* @param changes
|
2011-02-20 01:44:39 +00:00
|
|
|
*/
|
2012-03-14 10:21:41 +00:00
|
|
|
public void expand(Vector... changes) throws RegionOperationException {
|
|
|
|
for (Vector change : changes) {
|
|
|
|
if (change.getBlockX() != 0 || change.getBlockZ() != 0) {
|
|
|
|
throw new RegionOperationException("Polygons can only be expanded vertically.");
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2012-03-14 10:21:41 +00:00
|
|
|
for (Vector change : changes) {
|
|
|
|
int changeY = change.getBlockY();
|
|
|
|
if (changeY > 0) {
|
|
|
|
maxY += changeY;
|
|
|
|
} else {
|
|
|
|
minY += changeY;
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
recalculate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contract the region.
|
|
|
|
*
|
2012-03-30 04:06:43 +00:00
|
|
|
* @param changes
|
2011-02-20 01:44:39 +00:00
|
|
|
*/
|
2012-03-14 10:21:41 +00:00
|
|
|
public void contract(Vector... changes) throws RegionOperationException {
|
|
|
|
for (Vector change : changes) {
|
|
|
|
if (change.getBlockX() != 0 || change.getBlockZ() != 0) {
|
|
|
|
throw new RegionOperationException("Polygons can only be contracted vertically.");
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2012-03-14 10:21:41 +00:00
|
|
|
for (Vector change : changes) {
|
|
|
|
int changeY = change.getBlockY();
|
|
|
|
if (changeY > 0) {
|
|
|
|
minY += changeY;
|
|
|
|
} else {
|
|
|
|
maxY += changeY;
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
recalculate();
|
|
|
|
}
|
|
|
|
|
2012-01-03 15:13:50 +00:00
|
|
|
@Override
|
|
|
|
public void shift(Vector change) throws RegionOperationException {
|
|
|
|
final double changeX = change.getX();
|
|
|
|
final double changeY = change.getY();
|
|
|
|
final double changeZ = change.getZ();
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size(); ++i) {
|
|
|
|
BlockVector2D point = points.get(i);
|
|
|
|
points.set(i, new BlockVector2D(point.getX() + changeX, point.getZ() + changeZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
minY += changeY;
|
|
|
|
maxY += changeY;
|
|
|
|
|
|
|
|
recalculate();
|
|
|
|
}
|
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if a point is inside this region.
|
|
|
|
*/
|
|
|
|
public boolean contains(Vector pt) {
|
|
|
|
return contains(points, minY, maxY, pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see if a point is inside a region.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
|
|
|
* @param points
|
|
|
|
* @param minY
|
|
|
|
* @param maxY
|
|
|
|
* @param pt
|
|
|
|
* @return
|
2011-02-20 01:44:39 +00:00
|
|
|
*/
|
|
|
|
public static boolean contains(List<BlockVector2D> points, int minY,
|
|
|
|
int maxY, Vector pt) {
|
|
|
|
if (points.size() < 3) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-03 02:56:05 +00:00
|
|
|
int targetX = pt.getBlockX(); //wide
|
|
|
|
int targetY = pt.getBlockY(); //height
|
|
|
|
int targetZ = pt.getBlockZ(); //depth
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
if (targetY < minY || targetY > maxY) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
boolean inside = false;
|
|
|
|
int npoints = points.size();
|
|
|
|
int xNew, zNew;
|
|
|
|
int xOld, zOld;
|
|
|
|
int x1, z1;
|
|
|
|
int x2, z2;
|
2011-08-23 15:01:16 +00:00
|
|
|
long crossproduct;
|
2011-02-20 01:44:39 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
xOld = points.get(npoints - 1).getBlockX();
|
|
|
|
zOld = points.get(npoints - 1).getBlockZ();
|
2011-04-03 02:56:05 +00:00
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (i = 0; i < npoints; ++i) {
|
2011-02-20 01:44:39 +00:00
|
|
|
xNew = points.get(i).getBlockX();
|
|
|
|
zNew = points.get(i).getBlockZ();
|
2011-04-03 02:56:05 +00:00
|
|
|
//Check for corner
|
|
|
|
if (xNew == targetX && zNew == targetZ) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
if (xNew > xOld) {
|
2011-04-03 02:56:05 +00:00
|
|
|
x1 = xOld;
|
2011-02-20 01:44:39 +00:00
|
|
|
x2 = xNew;
|
|
|
|
z1 = zOld;
|
2011-04-03 02:56:05 +00:00
|
|
|
z2 = zNew;
|
2011-02-20 01:44:39 +00:00
|
|
|
} else {
|
2011-04-03 02:56:05 +00:00
|
|
|
x1 = xNew;
|
2011-02-20 01:44:39 +00:00
|
|
|
x2 = xOld;
|
|
|
|
z1 = zNew;
|
2011-04-03 02:56:05 +00:00
|
|
|
z2 = zOld;
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
2011-08-23 15:01:16 +00:00
|
|
|
if (x1 <= targetX && targetX <= x2) {
|
|
|
|
crossproduct = ((long) targetZ - (long) z1) * (long) (x2 - x1)
|
2011-11-23 01:29:48 +00:00
|
|
|
- ((long) z2 - (long) z1) * (long) (targetX - x1);
|
2011-08-23 15:01:16 +00:00
|
|
|
if (crossproduct == 0) {
|
|
|
|
if ((z1 <= targetZ) == (targetZ <= z2)) return true; //on edge
|
|
|
|
} else if (crossproduct < 0 && (x1 != targetX)) {
|
|
|
|
inside = !inside;
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
|
|
|
xOld = xNew;
|
|
|
|
zOld = zNew;
|
|
|
|
}
|
2011-04-03 02:56:05 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
return inside;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Return the number of points.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int size() {
|
|
|
|
return points.size();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
/**
|
|
|
|
* Expand the height of the polygon to fit the specified Y.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
|
|
|
* @param y
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return true if the area was expanded
|
|
|
|
*/
|
|
|
|
public boolean expandY(int y) {
|
|
|
|
if (!hasY) {
|
|
|
|
minY = y;
|
|
|
|
maxY = y;
|
|
|
|
hasY = true;
|
|
|
|
return true;
|
|
|
|
} else if (y < minY) {
|
|
|
|
minY = y;
|
|
|
|
return true;
|
|
|
|
} else if (y > maxY) {
|
|
|
|
maxY = y;
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the iterator.
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-02-20 01:44:39 +00:00
|
|
|
* @return iterator of points inside the region
|
|
|
|
*/
|
2012-01-03 14:57:29 +00:00
|
|
|
@Override
|
2011-02-20 01:44:39 +00:00
|
|
|
public Iterator<BlockVector> iterator() {
|
2012-04-16 06:47:43 +00:00
|
|
|
return new FlatRegion3DIterator(this);
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|
2011-08-24 22:27:18 +00:00
|
|
|
|
2012-03-13 10:55:47 +00:00
|
|
|
@Override
|
|
|
|
public Iterable<Vector2D> asFlatRegion() {
|
|
|
|
return new Iterable<Vector2D>() {
|
|
|
|
@Override
|
|
|
|
public Iterator<Vector2D> iterator() {
|
|
|
|
return new FlatRegionIterator(Polygonal2DRegion.this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-08-24 22:27:18 +00:00
|
|
|
/**
|
|
|
|
* Returns string representation in the format
|
|
|
|
* "(x1, z1) - ... - (xN, zN) * (minY - maxY)"
|
2012-03-02 04:47:19 +00:00
|
|
|
*
|
2011-08-24 22:27:18 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-11-29 17:23:24 +00:00
|
|
|
@Override
|
2011-08-24 22:27:18 +00:00
|
|
|
public String toString() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
List<BlockVector2D> pts = getPoints();
|
|
|
|
Iterator<BlockVector2D> it = pts.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
BlockVector2D current = it.next();
|
|
|
|
sb.append("(" + current.getBlockX() + ", " + current.getBlockZ() + ")");
|
|
|
|
if (it.hasNext()) sb.append(" - ");
|
|
|
|
}
|
|
|
|
sb.append(" * (" + minY + " - " + maxY + ")");
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2012-03-20 14:14:41 +00:00
|
|
|
|
|
|
|
public Polygonal2DRegion clone() {
|
|
|
|
Polygonal2DRegion clone = (Polygonal2DRegion) super.clone();
|
|
|
|
clone.points = new ArrayList<BlockVector2D>(points);
|
|
|
|
return clone;
|
|
|
|
}
|
2013-07-27 09:12:29 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<BlockVector2D> polygonize(int maxPoints) {
|
|
|
|
if (maxPoints >= 0 && maxPoints < points.size()) {
|
|
|
|
throw new IllegalArgumentException("Cannot polygonize a this Polygonal2DRegion into the amount of points given.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
|
|
|
}
|
2011-02-20 01:44:39 +00:00
|
|
|
}
|