Clean up CylinderRegion center management & fix a minor issue with vertical contract

This commit is contained in:
aumgn 2012-04-22 08:26:44 +02:00
parent d43e233a42
commit ebbbc9f1a8
2 changed files with 28 additions and 18 deletions

View File

@ -36,8 +36,7 @@ import com.sk89q.worldedit.data.ChunkStore;
* @author yetanotherx * @author yetanotherx
*/ */
public class CylinderRegion extends AbstractRegion implements FlatRegion { public class CylinderRegion extends AbstractRegion implements FlatRegion {
private Vector center; private Vector2D center;
private Vector2D center2D;
private Vector2D radius; private Vector2D radius;
private int minY; private int minY;
private int maxY; private int maxY;
@ -71,7 +70,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
*/ */
public CylinderRegion(LocalWorld world, Vector center, Vector2D radius, int minY, int maxY) { public CylinderRegion(LocalWorld world, Vector center, Vector2D radius, int minY, int maxY) {
super(world); super(world);
setCenter(center); setCenter(center.toVector2D());
setRadius(radius); setRadius(radius);
this.minY = minY; this.minY = minY;
this.maxY = maxY; this.maxY = maxY;
@ -79,7 +78,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
} }
public CylinderRegion(CylinderRegion region) { public CylinderRegion(CylinderRegion region) {
this(region.world, region.center, region.getRadius(), region.minY, region.maxY); this(region.world, region.getCenter(), region.getRadius(), region.minY, region.maxY);
hasY = region.hasY; hasY = region.hasY;
} }
@ -89,16 +88,26 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
* @return * @return
*/ */
public Vector getCenter() { public Vector getCenter() {
return center; return center.toVector((maxY + minY) / 2);
} }
/** /**
* Sets the main center point of the region * Sets the main center point of the region
* *
* @deprecated replaced by {@link #setCenter(Vector2D)}
*/ */
@Deprecated
public void setCenter(Vector center) { public void setCenter(Vector center) {
setCenter(center.toVector2D());
}
/**
* Sets the main center point of the region
*
* @param center
*/
public void setCenter(Vector2D center) {
this.center = center; this.center = center;
this.center2D = center.toVector2D();
} }
/** /**
@ -154,7 +163,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
* @return min. point * @return min. point
*/ */
public Vector getMinimumPoint() { public Vector getMinimumPoint() {
return center2D.subtract(getRadius()).toVector(minY); return center.subtract(getRadius()).toVector(minY);
} }
/** /**
@ -163,7 +172,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
* @return max. point * @return max. point
*/ */
public Vector getMaximumPoint() { public Vector getMaximumPoint() {
return center2D.add(getRadius()).toVector(maxY); return center.add(getRadius()).toVector(maxY);
} }
/** /**
@ -248,7 +257,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
* @throws RegionOperationException * @throws RegionOperationException
*/ */
public void expand(Vector... changes) throws RegionOperationException { public void expand(Vector... changes) throws RegionOperationException {
setCenter(getCenter().add(calculateDiff2D(changes).toVector())); center = center.add(calculateDiff2D(changes));
radius = radius.add(calculateChanges2D(changes)); radius = radius.add(calculateChanges2D(changes));
for (Vector change : changes) { for (Vector change : changes) {
int changeY = change.getBlockY(); int changeY = change.getBlockY();
@ -267,22 +276,23 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
* @throws RegionOperationException * @throws RegionOperationException
*/ */
public void contract(Vector... changes) throws RegionOperationException { public void contract(Vector... changes) throws RegionOperationException {
setCenter(getCenter().subtract(calculateDiff2D(changes).toVector())); center = center.subtract(calculateDiff2D(changes));
Vector2D newRadius = radius.subtract(calculateChanges2D(changes)); Vector2D newRadius = radius.subtract(calculateChanges2D(changes));
radius = Vector2D.getMaximum(new Vector2D(1.5, 1.5), newRadius); radius = Vector2D.getMaximum(new Vector2D(1.5, 1.5), newRadius);
for (Vector change : changes) { for (Vector change : changes) {
int height = maxY - minY;
int changeY = change.getBlockY(); int changeY = change.getBlockY();
if (changeY > 0) { if (changeY > 0) {
minY += changeY; minY += Math.min(height, changeY);
} else { } else {
maxY += changeY; maxY += Math.max(-height, changeY);
} }
} }
} }
@Override @Override
public void shift(Vector change) throws RegionOperationException { public void shift(Vector change) throws RegionOperationException {
setCenter(getCenter().add(change)); center = center.add(change.toVector2D());
int changeY = change.getBlockY(); int changeY = change.getBlockY();
maxY += changeY; maxY += changeY;
@ -298,7 +308,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
return false; return false;
} }
return pt.toVector2D().subtract(center2D).divide(radius).lengthSq() <= 1; return pt.toVector2D().subtract(center).divide(radius).lengthSq() <= 1;
} }
/** /**
@ -385,13 +395,13 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
/** /**
* Returns string representation in the format * Returns string representation in the format
* "(centerX, centerY, centerZ) - (radiusX, radiusZ)" * "(centerX, centerZ) - (radiusX, radiusZ) - (minY, maxY)"
* *
* @return string * @return string
*/ */
@Override @Override
public String toString() { public String toString() {
return center + " - " + radius; return center + " - " + radius + "(" + minY + ", " + maxY + ")";
} }
public CylinderRegion clone() { public CylinderRegion clone() {

View File

@ -71,7 +71,7 @@ public class CylinderRegionSelector implements RegionSelector, CUIRegion {
Vector pos2 = oldRegion.getMaximumPoint(); Vector pos2 = oldRegion.getMaximumPoint();
Vector center = pos1.add(pos2).divide(2).floor(); Vector center = pos1.add(pos2).divide(2).floor();
region.setCenter(center); region.setCenter(center.toVector2D());
region.setRadius(pos2.toVector2D().subtract(center.toVector2D())); region.setRadius(pos2.toVector2D().subtract(center.toVector2D()));
region.setMaximumY(Math.max(pos1.getBlockY(), pos2.getBlockY())); region.setMaximumY(Math.max(pos1.getBlockY(), pos2.getBlockY()));
@ -85,7 +85,7 @@ public class CylinderRegionSelector implements RegionSelector, CUIRegion {
} }
region = new CylinderRegion(region.getWorld()); region = new CylinderRegion(region.getWorld());
region.setCenter(pos); region.setCenter(pos.toVector2D());
region.setY(pos.getBlockY()); region.setY(pos.getBlockY());
return true; return true;