Add support for expand with reverse dir for Cylinder & Ellipsoid

This commit is contained in:
aumgn
2012-03-14 11:58:26 +01:00
committed by TomyLobo
parent 760f77e681
commit 857f721bb5
3 changed files with 58 additions and 84 deletions

View File

@ -213,39 +213,28 @@ public class CylinderRegion extends AbstractRegion {
return (int) (2 * radius.getZ());
}
private Vector2D getTotalXZChanges(Vector... changes) throws RegionOperationException {
private Vector2D calculateDiff2D(Vector... changes) throws RegionOperationException {
Vector2D diff = new Vector2D();
Vector2D total = new Vector2D();
for (Vector change : changes) {
diff = diff.add(change.toVector2D());
}
if ((diff.getBlockX() & 1) + (diff.getBlockZ() & 1) != 0) {
throw new RegionOperationException("Cylinders changes must be even for each horizontal dimensions.");
}
return diff.divide(2).floor();
}
private Vector2D calculateChanges2D(Vector... changes) {
Vector2D total = new Vector2D();
for (Vector change : changes) {
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.
*
* @param change
*/
public void expand(Vector change) throws RegionOperationException {
if (change.getBlockX() != 0 || change.getBlockZ() != 0) {
throw new RegionOperationException("Cylinders can only be expanded vertically.");
}
int changeY = change.getBlockY();
if (changeY > 0) {
maxY += changeY;
} else {
minY += changeY;
}
}
/**
* Expand the region.
* Expand the region.
@ -254,27 +243,15 @@ public class CylinderRegion extends AbstractRegion {
* @throws RegionOperationException
*/
public void expand(Vector... changes) throws RegionOperationException {
radius = radius.add(getTotalXZChanges(changes));
setCenter(getCenter().add(calculateDiff2D(changes).toVector()));
radius = radius.add(calculateChanges2D(changes));
for (Vector change : changes) {
expand(new Vector(0, change.getBlockY(), 0));
}
}
/**
* Contract the region.
*
* @param change
*/
public void contract(Vector change) throws RegionOperationException {
if (change.getBlockX() != 0 || change.getBlockZ() != 0) {
throw new RegionOperationException("Cylinders can only be expanded vertically.");
}
int changeY = change.getBlockY();
if (changeY > 0) {
minY += changeY;
} else {
maxY += changeY;
int changeY = change.getBlockY();
if (changeY > 0) {
maxY += changeY;
} else {
minY += changeY;
}
}
}
@ -285,10 +262,16 @@ public class CylinderRegion extends AbstractRegion {
* @throws RegionOperationException
*/
public void contract(Vector... changes) throws RegionOperationException {
Vector2D newRadius = radius.subtract(getTotalXZChanges(changes));
setCenter(getCenter().subtract(calculateDiff2D(changes).toVector()));
Vector2D newRadius = radius.subtract(calculateChanges2D(changes));
radius = Vector2D.getMaximum(new Vector2D(1.5, 1.5), newRadius);
for (Vector change : changes) {
contract(new Vector(0, change.getBlockY(), 0));
int changeY = change.getBlockY();
if (changeY > 0) {
minY += changeY;
} else {
maxY += changeY;
}
}
}