Best way to make a circle

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

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

    4thegame3

    What is the best way to make a circle?
    have you got one?
    here is mine:
    [​IMG]
    Code:java
    1. //PointOnCircleMethod (found on internet)
    2.  
    3. public static Point PointOnCircle(float radius, float angleInDegrees, Point origin)
    4. {
    5. // Convert from degrees to radians via multiplication by PI/180
    6. float x = (float)(radius * Math.cos(angleInDegrees * Math.PI / 180F)) + origin.getX();
    7. float y = (float)(radius * Math.sin(angleInDegrees * Math.PI / 180F)) + origin.getY();
    8. return new Point(Math.round(x), Math.round(y));
    9. }
    10.  
    11. //Point Class (created from myself)
    12.  
    13. public class Point {
    14. int x;
    15. int y;
    16. public Point(int x,int y){
    17. this.x = x;
    18. this.y = y;
    19. }
    20. public Point(){
    21. this(0, 0);
    22. }
    23. public int getX(){
    24. return x;
    25. }
    26. public int getY(){
    27. return y;
    28. }
    29. }
    30.  
    31. //ForCycle (created from myself)
    32.  
    33. for(int i=0;i<360;i++){
    34. Point po = PointOnCircle(6,i,new Point());
    35. Location location = p.getLocation();
    36. location.add(po.getX(),0,po.getY());
    37. Block b = location.getBlock();
    38. b.breakNaturally();
    39. }


    But, as you can see isnt like a world edit perfect circle.

    [​IMG]


    Have you got any ideas for a new simple algorithm? post it here!
     
  2. Offline

    Bammerbom

    4thegame3
    Credits: Rprrr
    Code:java
    1. public static List<Location> circle (Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {
    2. List<Location> circleblocks = new ArrayList<Location>();
    3. int cx = loc.getBlockX();
    4. int cy = loc.getBlockY();
    5. int cz = loc.getBlockZ();
    6. for (int x = cx - r; x <= cx +r; x++)
    7. for (int z = cz - r; z <= cz +r; z++)
    8. for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
    9. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
    10. if (dist < r*r && !(hollow && dist < (r-1)*(r-1))) {
    11. Location l = new Location(loc.getWorld(), x, y + plus_y, z);
    12. circleblocks.add(l);
    13. }
    14. }
    15.  
    16. return circleblocks;
    17. }
     
  3. Offline

    4thegame3

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

    caelum19

    hehehe.
     
  5. Offline

    4thegame3

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

    Bammerbom

    4thegame3
    Try this one
    Credits: pyraetos
    Code:java
    1. package something;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.block.Block;
    9.  
    10. public class Sphere {
    11.  
    12. private int xx;
    13. private int yy;
    14. private int zz;
    15.  
    16. private HashMap<Integer,Block> set;
    17.  
    18. private int radius;
    19.  
    20. public Sphere(Location center, int radius){
    21. this.radius = radius;
    22. int xx = (int)Math.floor(center.getX());
    23. int yy = (int)Math.floor(center.getY());
    24. int zz = (int)Math.floor(center.getZ());
    25.  
    26. Region region = new Region(new Location(center.getWorld(),xx+radius,yy+radius,zz+radius),new Location(center.getWorld(),xx-radius,yy-radius,zz-radius));
    27. HashMap<Integer,Block> set = region.getVolume();
    28.  
    29. this.xx = xx;
    30. this.yy = yy;
    31. this.zz = zz;
    32. this.set = set;
    33.  
    34. }
    35.  
    36. public void set(int id){
    37. for(int counter = 0; counter <= set.size() - 1; counter++){
    38. int a = (set.get(counter).getX() - xx)*(set.get(counter).getX() - xx);
    39. int b = (set.get(counter).getY() - yy)*(set.get(counter).getY() - yy);
    40. int c = (set.get(counter).getZ() - zz)*(set.get(counter).getZ() - zz);
    41. if(a +b +c <= (radius*radius)) set.get(counter).setType(Material.getMaterial(id));
    42. }
    43. }
    44. }

    Region class: http://pastebin.com/pv8JFXXL
     
  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.
     
  8. Offline

    4thegame3

    Bammerbom before i try, this makes a circle or a sphere?
     
  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
Thread Status:
Not open for further replies.

Share This Page