# Best way to make a circle

Discussion in 'Plugin Development' started by 4thegame3, Sep 15, 2014.

Not open for further replies.
1. Offline

### 4thegame3

What is the best way to make a circle?
have you got one?
here is mine: Code:java
` //PointOnCircleMethod (found on internet) public static Point PointOnCircle(float radius, float angleInDegrees, Point origin){// Convert from degrees to radians via multiplication by PI/180float x = (float)(radius * Math.cos(angleInDegrees * Math.PI / 180F)) + origin.getX();float y = (float)(radius * Math.sin(angleInDegrees * Math.PI / 180F)) + origin.getY();return new Point(Math.round(x), Math.round(y));} //Point Class (created from myself) public class Point {int x;int y;public Point(int x,int y){this.x = x;this.y = y;}public Point(){this(0, 0);}public int getX(){return x;}public int getY(){return y;}} //ForCycle (created from myself) for(int i=0;i<360;i++){Point po = PointOnCircle(6,i,new Point());Location location = p.getLocation();location.add(po.getX(),0,po.getY());Block b = location.getBlock();b.breakNaturally();}`

But, as you can see isnt like a world edit perfect circle. Have you got any ideas for a new simple algorithm? post it here!

#1
2. Offline

### Bammerbom

4thegame3
Credits: Rprrr
Code:java
`public static List<Location> circle (Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {        List<Location> circleblocks = new ArrayList<Location>();        int cx = loc.getBlockX();        int cy = loc.getBlockY();        int cz = loc.getBlockZ();        for (int x = cx - r; x <= cx +r; x++)            for (int z = cz - r; z <= cz +r; z++)                for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {                    double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);                    if (dist < r*r && !(hollow && dist < (r-1)*(r-1))) {                        Location l = new Location(loc.getWorld(), x, y + plus_y, z);                        circleblocks.add(l);                        }                    }         return circleblocks;    }`

#2
3. Offline

### 4thegame3

Bammerbom nice! going to try and post a screen of the result.

#3
4. Offline

hehehe.

#4
5. Offline

### 4thegame3

here's the resutl of Bammerbom 's code with 6 of radius Not bad but world edit's circle is still better, any other ideas?

#5
6. Offline

### Bammerbom

4thegame3
Try this one
Credits: pyraetos
Code:java
`package something; import java.util.HashMap; import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.World;import org.bukkit.block.Block; public class Sphere { private int xx;private int yy;private int zz; private HashMap<Integer,Block> set; private int radius; public Sphere(Location center, int radius){this.radius = radius;int xx = (int)Math.floor(center.getX());int yy = (int)Math.floor(center.getY());int zz = (int)Math.floor(center.getZ()); Region region = new Region(new Location(center.getWorld(),xx+radius,yy+radius,zz+radius),new Location(center.getWorld(),xx-radius,yy-radius,zz-radius));HashMap<Integer,Block> set = region.getVolume(); this.xx = xx;this.yy = yy;this.zz = zz;this.set = set; } public void set(int id){for(int counter = 0; counter <= set.size() - 1; counter++){int a = (set.get(counter).getX() - xx)*(set.get(counter).getX() - xx);int b = (set.get(counter).getY() - yy)*(set.get(counter).getY() - yy);int c = (set.get(counter).getZ() - zz)*(set.get(counter).getZ() - zz);if(a +b +c <= (radius*radius)) set.get(counter).setType(Material.getMaterial(id));}}}`

Region class: http://pastebin.com/pv8JFXXL

#6
7. Offline

### Assist

Bammerbom
Actually the first snippet you posted is by chasechocolate, I believe. I remember him posting that on a "show-off" thread. Unless he stole it from Rprrr of course.

#7
8. Offline

### 4thegame3

Bammerbom before i try, this makes a circle or a sphere?

#8
9. Offline

### Bammerbom

Assist
Then Rprrr posted his code without mentioning credits

4thegame3
I dont know, but changing it from circle to sphere shouldnt be too hard.

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

Last edited by a moderator: Jun 14, 2016
#9