From af2429467dc1836e803051a9a9272efa4cb2d4ea Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Fri, 12 Aug 2011 08:16:53 +0200 Subject: [PATCH] Optimized //(h)sphere a bit and made the shell for //hsphere as thin as possible. --- .../java/com/sk89q/worldedit/EditSession.java | 72 +++++++++++-------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index 8b08b3fd5..f95ddc766 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -1813,38 +1813,50 @@ public class EditSession { boolean filled) throws MaxChangedBlocksException { int affected = 0; - int ceilRadius = (int) Math.ceil(radius); + radius += 0.5; + final double radiusSq = radius*radius; + final double radius1Sq = (radius - 1)*(radius - 1); + + final int ceilRadius = (int) Math.ceil(radius); for (int x = 0; x <= ceilRadius; ++x) { for (int y = 0; y <= ceilRadius; ++y) { for (int z = 0; z <= ceilRadius; ++z) { - Vector vec = pos.add(x, y, z); - double d = vec.distance(pos); + double dSq = lengthSq(x, y, z); - if (d <= radius + 0.5 && (filled || d >= radius - 0.5)) { - if (setBlock(vec, block)) { - ++affected; - } - if (setBlock(pos.add(-x, y, z), block)) { - ++affected; - } - if (setBlock(pos.add(x, -y, z), block)) { - ++affected; - } - if (setBlock(pos.add(x, y, -z), block)) { - ++affected; - } - if (setBlock(pos.add(-x, -y, z), block)) { - ++affected; - } - if (setBlock(pos.add(x, -y, -z), block)) { - ++affected; - } - if (setBlock(pos.add(-x, y, -z), block)) { - ++affected; - } - if (setBlock(pos.add(-x, -y, -z), block)) { - ++affected; - } + if (dSq > radiusSq) + continue; + + if (!filled) { + if (dSq < radius1Sq) + continue; + + if (lengthSq(x+1, y, z) <= radiusSq && lengthSq(x, y+1, z) <= radiusSq && lengthSq(x, y, z+1) <= radiusSq) + continue; + } + + if (setBlock(pos.add(x, y, z), block)) { + ++affected; + } + if (setBlock(pos.add(-x, y, z), block)) { + ++affected; + } + if (setBlock(pos.add(x, -y, z), block)) { + ++affected; + } + if (setBlock(pos.add(x, y, -z), block)) { + ++affected; + } + if (setBlock(pos.add(-x, -y, z), block)) { + ++affected; + } + if (setBlock(pos.add(x, -y, -z), block)) { + ++affected; + } + if (setBlock(pos.add(-x, y, -z), block)) { + ++affected; + } + if (setBlock(pos.add(-x, -y, -z), block)) { + ++affected; } } } @@ -1853,6 +1865,10 @@ public class EditSession { return affected; } + private static final double lengthSq(int x, int y, int z) { + return x*x + y*y + z*z; + } + /** * Makes a pyramid. *