Generating a (flat) circle of blocks..

Discussion in 'Plugin Development' started by Eballer48, Aug 13, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Eballer48

    How the hell do I do that..
     
  2. Offline

    Splated

    Heres how i did it.

    You can add a for loop for y also and make spheres.

    Code:
     
              Block block = this.FenceCenter.getWorld().getBlockAt(this.FenceCenter);
                int radius = (int) this.FenceDistance;
               
     
               
                for(int x = -radius; x <= radius; x++) {
                    for(int z = -radius; z <= radius; z++) {
     
                        Block b = block.getRelative(x, 1, z);
                        double d = this.FenceCenter.distance(b.getLocation());
                       
                        if ((d > radius -1 ) && (d < radius +1 ))
                        b.setType(Material.FENCE);
                       
                    }
                }
     
  3. Offline

    DarkBladee12

    That's my method for generating cylinders:

    Code:
    public void createCyl(Location loc, int r, Material mat) {
            int cx = loc.getBlockX();
            int cy = loc.getBlockY();
            int cz = loc.getBlockZ();
            World w = loc.getWorld();
            int rSquared = r * r;
            for (int x = cx - r; x <= cx + r; x++) {
                for (int z = cz - r; z <= cz + r; z++) {
                    if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rSquared) {
                        Location cylBlock = new Location(w, x, cy, z);
                         w.getBlockAt(x, cy, z).setType(mat);
                    }
                }
            }
        }
     
  4. Offline

    Jnorr44

    Here, I made a 3d sphere, just remove the Y loop, and you will have a 2d circle:
    Code:
    public class Sphere {//TODO annotations
        private ArrayList<Block> sphere = new ArrayList<Block>();
        private Location center;
        private int radius;
       
        public Sphere(Location center, int radius) {
            this.center = center;
            this.radius = radius;
            for(int X = -radius; X < radius; X++) {
                for(int Y = -radius; Y < radius; Y++) {
                    for(int Z = -radius; Z < radius; Z++) {
                        if(Math.sqrt((X * X) + (Y * Y) + (Z * Z)) <= radius) {
                            Block block = center.getWorld().getBlockAt(X + center.getBlockX(), Y + center.getBlockY(), Z + center.getBlockZ());
                            sphere.add(block);
                        }
                    }
                }
            }
        }
       
        public Location getCenter() {
            return center;
        }
       
        public int getRadius() {
            return radius;
        }
       
        public ArrayList<Block> getBlocks() {
            return sphere;
        }
       
        private boolean isInside(int X, int Y, int Z) {
            return Math.sqrt((X * X) + (Y * Y) + (Z * Z)) <= radius;
        }
       
        public boolean overlaps(Sphere other) {
            for (Block block : other.getBlocks())
                if (contains(block.getLocation()))
                        return true;
            return false;
        }
       
        public boolean contains(Location loc) {
            return isInside(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page