# [UTIL] Getting circle locations

Discussion in 'Resources' started by Ivan, Sep 17, 2013.

Not open for further replies.
1. Offline

### Ivan

If my calculations are correct, this should return points of a circle in the minecraft world. The higher the amount, the more accurate your results will be. eg. If you enter 3 for the amount you'll probably end up with 3 points drawing a triangle. If you put 100 you'll come pretty close to a circle in minecraft (depends on the radius ofcourse). Note: the points are drawn on the XZ plane.
Code:java
` public ArrayList<Location> getCircle(Location center, double radius, int amount){        World world = center.getWorld();        double increment = (2*Math.PI)/amount;        ArrayList<Location> locations = new ArrayList<Location>();        for(int i = 0;i < amount; i++){        double angle = i*increment;        double x = center.getX() + (radius * Math.cos(angle));        double z = center.getZ() + (radius * Math.sin(angle));        locations.add(new Location(world, x, center.getY(), z));        }        return locations;    } `

#1
2. Offline

### MinecraftShamrock

Cool Could you add a 3d feature?

Ivan Oh, and could you add something that adds a pitch that always looks away from the center?
That would be AWESOME

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 4, 2016
3. Offline

#3
4. Offline

Sphere.

#4
5. Offline

### TomTheDeveloper

Ivan So Ivan, you can add the sphere method from chasechocolate. So you can make a sphere and a circle. But be sure to use the correct way of reffering to chasechocolate like we learned it on school today!

6. Offline

I ported some methods from worledit Cylinder
Code:java
` public List<Location> getCylinder(Location location, double radiusX, double radiusZ, int height, boolean filled) {Vector pos = location.toVector();World world = location.getWorld();List<Location> blocks = new ArrayList<Location>();radiusX += 0.5;radiusZ += 0.5; if (height == 0) {return blocks;} else if (height < 0) {height = -height;pos = pos.subtract(new Vector(0, height, 0));} if (pos.getBlockY() < 0) {pos = pos.setY(0);} else if (pos.getBlockY() + height - 1 > world.getMaxHeight()) {height = world.getMaxHeight() - pos.getBlockY() + 1;} final double invRadiusX = 1 / radiusX;final double invRadiusZ = 1 / radiusZ; final int ceilRadiusX = (int) Math.ceil(radiusX);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 nextZn = 0;forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {final double zn = nextZn;nextZn = (z + 1) * invRadiusZ; double distanceSq = lengthSq(xn, zn);if (distanceSq > 1) {if (z == 0) {break forX;}break forZ;} if (!filled) {if (lengthSq(nextXn, zn) <= 1 && lengthSq(xn, nextZn) <= 1) {continue;}} for (int y = 0; y < height; ++y) { blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));blocks.add(pos.add(new Vector(-x, y, z)).toLocation(world));blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));}}} return blocks;}`

Sphere:
Code:java
`public List<Location> getSphere(Location location, double radiusX, double radiusY, double radiusZ, boolean filled){Vector pos = location.toVector();World world = location.getWorld();List<Location> blocks = new ArrayList<Location>(); 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;}} blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));blocks.add(pos.add(new Vector(-x,y,z)).toLocation(world));blocks.add(pos.add(new Vector(x,-y,z)).toLocation(world));blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));blocks.add(pos.add(new Vector(-x,-y,z)).toLocation(world));blocks.add(pos.add(new Vector(x,-y,-z)).toLocation(world));blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));blocks.add(pos.add(new Vector(-x,-y,-z)).toLocation(world));}}} return blocks;}`

These methods are required:
Code:java
`    private static final double lengthSq(double x, double y, double z) {        return (x * x) + (y * y) + (z * z);    }     private static final double lengthSq(double x, double z) {        return (x * x) + (z * z);    }`

#6
ChipDev likes this.
7. Offline

### Hoolean

Ivan

Circles in Minecraft? Blasphemy! Nice job, this looks a lot more efficient than my current method #7
Ivan likes this.
8. Offline

### Cybermaxke

Here were the utils I made https://gist.github.com/Cybermaxke/6800852
It also supports shapes, like triangles, pentagons, etc. I didn't add cylinders yet.

#8
LCastr0 likes this.
9. Offline

### ItzHectik

Erm, this may seem like a bit of a silly question, but how would I use this to create flame particles travelling around in a circle? I'm using the ParticleEffect library by DarkBlade12 to create my particles. ( ParticleEffect.FLAME.display(p.getLocation(), 16, 0, 0, 0, 0.0F, 1); )

#9
10. Offline

### DevRosemberg

ItzHectik

Code:java
`final ArrayList<Location> blocks = getCircle(player.getLocation(), 10, 100);         new BukkitRunnable() {            int loop = 0;             @Override            public void run() {                if (loop >= blocks.size()) {                    loop = 0;                }                 Particles.FLAME.display(blocks.get(loop++),0, 0, 0, 0, 10);            }        }.runTaskTimer(this, 0L, 1L);`

And just for fun (broke physics): Code:java
`new BukkitRunnable() {            @Override            public void run() {                for (Location location : blocks) {                    Particles.FLAME.display(location, 0, 0 ,0, 0,1);                }            }        }.runTaskTimer(this, 0L, 1L);`

#10