From 1359a7fada8ebb2e8e259e988c9dd41ee1830b7e Mon Sep 17 00:00:00 2001 From: aumgn Date: Tue, 13 Mar 2012 11:00:40 +0100 Subject: [PATCH] Implement contract/expand(Vector...) for Cylinder & Ellipsoid --- .../worldedit/regions/CylinderRegion.java | 43 +++++++++++++++++++ .../worldedit/regions/EllipsoidRegion.java | 37 ++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index 586547ab7..699d412fb 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -213,6 +213,21 @@ public class CylinderRegion extends AbstractRegion { return (int) (2 * radius.getZ()); } + private Vector2D getTotalXZChanges(Vector... changes) throws RegionOperationException { + Vector2D diff = new Vector2D(); + Vector2D total = new Vector2D(); + for (Vector change : changes) { + diff = diff.add(change.toVector2D()); + total = total.add(change.toVector2D().positive()); + } + + if (diff.getBlockX() != 0 || diff.getBlockZ() != 0) { + throw new RegionOperationException("Cylinders changes must be equal for both directions of each horizontal dimensions."); + } + + return total.divide(2).floor(); + } + /** * Expand the region. * @@ -231,6 +246,20 @@ public class CylinderRegion extends AbstractRegion { } } + /** + * Expand the region. + * Expand the region. + * + * @param changes array/arguments with multiple related changes + * @throws RegionOperationException + */ + public void expand(Vector... changes) throws RegionOperationException { + radius = radius.add(getTotalXZChanges(changes)); + for (Vector change : changes) { + expand(new Vector(0, change.getBlockY(), 0)); + } + } + /** * Contract the region. * @@ -249,6 +278,20 @@ public class CylinderRegion extends AbstractRegion { } } + /** + * Contract the region. + * + * @param changes array/arguments with multiple related changes + * @throws RegionOperationException + */ + public void contract(Vector... changes) throws RegionOperationException { + Vector2D newRadius = radius.subtract(getTotalXZChanges(changes)); + radius = Vector2D.getMaximum(new Vector2D(1.5, 1.5), newRadius); + for (Vector change : changes) { + contract(new Vector(0, change.getBlockY(), 0)); + } + } + @Override public void shift(Vector change) throws RegionOperationException { setCenter(getCenter().add(change)); diff --git a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java index 9d4b65fc0..c34087d5b 100644 --- a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java @@ -120,6 +120,22 @@ public class EllipsoidRegion extends AbstractRegion { return (int) (2 * radius.getZ()); } + private Vector getTotalChanges(Vector... changes) throws RegionOperationException { + Vector diff = new Vector(); + Vector total = new Vector(); + for (Vector change : changes) { + diff = diff.add(change); + total = total.add(change.positive()); + } + + if (diff.getBlockX() != 0 || diff.getBlockY() != 0 || diff.getBlockZ() != 0) { + throw new RegionOperationException( + "Ellipsoid changes must be equal for both directions of each dimensions."); + } + + return total.divide(2).floor(); + } + /** * Expands the ellipsoid in a direction. * @@ -128,6 +144,16 @@ public class EllipsoidRegion extends AbstractRegion { public void expand(Vector change) { } + /** + * Expand the region. + * + * @param changes array/arguments with multiple related changes + * @throws RegionOperationException + */ + public void expand(Vector... changes) throws RegionOperationException { + radius = radius.add(getTotalChanges(changes)); + } + /** * Contracts the ellipsoid in a direction. * @@ -136,6 +162,17 @@ public class EllipsoidRegion extends AbstractRegion { public void contract(Vector change) { } + /** + * Contract the region. + * + * @param changes array/arguments with multiple related changes + * @throws RegionOperationException + */ + public void contract(Vector... changes) throws RegionOperationException { + Vector newRadius = radius.subtract(getTotalChanges(changes)); + radius = Vector.getMaximum(new Vector(1.5, 1.5, 1.5), newRadius); + } + @Override public void shift(Vector change) throws RegionOperationException { center = center.add(change);