I can't seem to generate a sphere. I know there have been threads about this, but none have provided working code. This is my code: Code: public int makeSphere(Location pos, Pattern block, double radiusX, double radiusY, double radiusZ, boolean filled) { int affected = 0; radiusX += 0.5; radiusY += 0.5; radiusZ += 0.5; final double invRadiusX = 1 / radiusX; final double invRadiusY = 1 / radiusY; final double invRadiusZ = 1 / radiusZ; final int ceilRadiusX = (int) Math.ceil(radiusX); final int ceilRadiusY = (int) Math.ceil(radiusY); final int ceilRadiusZ = (int) Math.ceil(radiusZ); double nextXn = 0; forX: for (int x = 0; x <= ceilRadiusX; ++x) { final double xn = nextXn; nextXn = (x + 1) * invRadiusX; double nextYn = 0; forY: for (int y = 0; y <= ceilRadiusY; ++y) { final double yn = nextYn; nextYn = (y + 1) * invRadiusY; double nextZn = 0; forZ: for (int z = 0; z <= ceilRadiusZ; ++z) { final double zn = nextZn; nextZn = (z + 1) * invRadiusZ; double distanceSq = lengthSq(xn, yn, zn); if (distanceSq > 1) { if (z == 0) { if (y == 0) { break forX; } break forY; } break forZ; } if (!filled) { if (lengthSq(nextXn, yn, zn) <= 1 && lengthSq(xn, nextYn, zn) <= 1 && lengthSq(xn, yn, nextZn) <= 1) { continue; } } pos.add(x,y,z).getBlock().setType(Material.OBSIDIAN); pos.add(-x,y,z).getBlock().setType(Material.OBSIDIAN); pos.add(x,-y,z).getBlock().setType(Material.OBSIDIAN); pos.add(x,y,-z).getBlock().setType(Material.OBSIDIAN); pos.add(-x,-y,z).getBlock().setType(Material.OBSIDIAN); pos.add(x,-y,-z).getBlock().setType(Material.OBSIDIAN); pos.add(-x,y,-z).getBlock().setType(Material.OBSIDIAN); pos.add(-x,-y,-z).getBlock().setType(Material.OBSIDIAN); } } } return affected; } , which I copied directly from worldedit. When used, it generates a seemingly random set of blocks(seen here). What am I doing wrong? Any help? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
this code gets a list of blocks, which you can then set to whatever material. r is radius. Code: public static List<Location> sphere(Location centre, double r) { List<Location> sphereblocks = new ArrayList<Location>(); double cx = centre.getX(); double cy = centre.getY(); double cz = centre.getZ(); for (double x = cx - r; x <= cx + r; x++) { for (double y = cy - r; y <= cy + r; y++) { for (double z = cz - r; z <= cz + r; z++) { Location l = new Location(centre.getWorld(), x, y, z); double dist = centre.distance(l); if (dist <= r) { sphereblocks.add(l); } } } } return sphereblocks; } to use it, do Code: for(Location l : sphere(sphere_centre_location, radius)){ l.getBlock().setType(Material.whatever); }