From ebbbc9f1a81766a27edd31d24cff5417fb1a08c5 Mon Sep 17 00:00:00 2001 From: aumgn Date: Sun, 22 Apr 2012 08:26:44 +0200 Subject: [PATCH] Clean up CylinderRegion center management & fix a minor issue with vertical contract --- .../worldedit/regions/CylinderRegion.java | 42 ++++++++++++------- .../regions/CylinderRegionSelector.java | 4 +- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index 171873a8a..3beb99803 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -36,8 +36,7 @@ import com.sk89q.worldedit.data.ChunkStore; * @author yetanotherx */ public class CylinderRegion extends AbstractRegion implements FlatRegion { - private Vector center; - private Vector2D center2D; + private Vector2D center; private Vector2D radius; private int minY; 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) { super(world); - setCenter(center); + setCenter(center.toVector2D()); setRadius(radius); this.minY = minY; this.maxY = maxY; @@ -79,7 +78,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { } 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; } @@ -89,16 +88,26 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { * @return */ public Vector getCenter() { - return center; + return center.toVector((maxY + minY) / 2); } /** * Sets the main center point of the region * + * @deprecated replaced by {@link #setCenter(Vector2D)} */ + @Deprecated 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.center2D = center.toVector2D(); } /** @@ -154,7 +163,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { * @return min. point */ 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 */ 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 */ public void expand(Vector... changes) throws RegionOperationException { - setCenter(getCenter().add(calculateDiff2D(changes).toVector())); + center = center.add(calculateDiff2D(changes)); radius = radius.add(calculateChanges2D(changes)); for (Vector change : changes) { int changeY = change.getBlockY(); @@ -267,22 +276,23 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { * @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)); radius = Vector2D.getMaximum(new Vector2D(1.5, 1.5), newRadius); for (Vector change : changes) { + int height = maxY - minY; int changeY = change.getBlockY(); if (changeY > 0) { - minY += changeY; + minY += Math.min(height, changeY); } else { - maxY += changeY; + maxY += Math.max(-height, changeY); } } } @Override public void shift(Vector change) throws RegionOperationException { - setCenter(getCenter().add(change)); + center = center.add(change.toVector2D()); int changeY = change.getBlockY(); maxY += changeY; @@ -298,7 +308,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { 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 - * "(centerX, centerY, centerZ) - (radiusX, radiusZ)" + * "(centerX, centerZ) - (radiusX, radiusZ) - (minY, maxY)" * * @return string */ @Override public String toString() { - return center + " - " + radius; + return center + " - " + radius + "(" + minY + ", " + maxY + ")"; } public CylinderRegion clone() { diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegionSelector.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegionSelector.java index eeb76c44f..3a6de1371 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegionSelector.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegionSelector.java @@ -71,7 +71,7 @@ public class CylinderRegionSelector implements RegionSelector, CUIRegion { Vector pos2 = oldRegion.getMaximumPoint(); Vector center = pos1.add(pos2).divide(2).floor(); - region.setCenter(center); + region.setCenter(center.toVector2D()); region.setRadius(pos2.toVector2D().subtract(center.toVector2D())); region.setMaximumY(Math.max(pos1.getBlockY(), pos2.getBlockY())); @@ -85,7 +85,7 @@ public class CylinderRegionSelector implements RegionSelector, CUIRegion { } region = new CylinderRegion(region.getWorld()); - region.setCenter(pos); + region.setCenter(pos.toVector2D()); region.setY(pos.getBlockY()); return true;