[UTIL] Getting circle locations

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

Thread Status:
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
    1.  
    2. public ArrayList<Location> getCircle(Location center, double radius, int amount){
    3. World world = center.getWorld();
    4. double increment = (2*Math.PI)/amount;
    5. ArrayList<Location> locations = new ArrayList<Location>();
    6. for(int i = 0;i < amount; i++){
    7. double angle = i*increment;
    8. double x = center.getX() + (radius * Math.cos(angle));
    9. double z = center.getZ() + (radius * Math.sin(angle));
    10. locations.add(new Location(world, x, center.getY(), z));
    11. }
    12. return locations;
    13. }
    14.  
     
  2. Cool :D
    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

    chasechocolate

  4. Offline

    Retherz_

    Sphere.
     
  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

    bobacadodl

    I ported some methods from worledit :)
    Cylinder
    Code:java
    1. public List<Location> getCylinder(Location location, double radiusX, double radiusZ, int height, boolean filled) {
    2. Vector pos = location.toVector();
    3. World world = location.getWorld();
    4. List<Location> blocks = new ArrayList<Location>();
    5. radiusX += 0.5;
    6. radiusZ += 0.5;
    7.  
    8. if (height == 0) {
    9. return blocks;
    10. } else if (height < 0) {
    11. height = -height;
    12. pos = pos.subtract(new Vector(0, height, 0));
    13. }
    14.  
    15. if (pos.getBlockY() < 0) {
    16. pos = pos.setY(0);
    17. } else if (pos.getBlockY() + height - 1 > world.getMaxHeight()) {
    18. height = world.getMaxHeight() - pos.getBlockY() + 1;
    19. }
    20.  
    21. final double invRadiusX = 1 / radiusX;
    22. final double invRadiusZ = 1 / radiusZ;
    23.  
    24. final int ceilRadiusX = (int) Math.ceil(radiusX);
    25. final int ceilRadiusZ = (int) Math.ceil(radiusZ);
    26.  
    27. double nextXn = 0;
    28. forX: for (int x = 0; x <= ceilRadiusX; ++x) {
    29. final double xn = nextXn;
    30. nextXn = (x + 1) * invRadiusX;
    31. double nextZn = 0;
    32. forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {
    33. final double zn = nextZn;
    34. nextZn = (z + 1) * invRadiusZ;
    35.  
    36. double distanceSq = lengthSq(xn, zn);
    37. if (distanceSq > 1) {
    38. if (z == 0) {
    39. break forX;
    40. }
    41. break forZ;
    42. }
    43.  
    44. if (!filled) {
    45. if (lengthSq(nextXn, zn) <= 1 && lengthSq(xn, nextZn) <= 1) {
    46. continue;
    47. }
    48. }
    49.  
    50. for (int y = 0; y < height; ++y) {
    51.  
    52. blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));
    53. blocks.add(pos.add(new Vector(-x, y, z)).toLocation(world));
    54. blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));
    55. blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));
    56. }
    57. }
    58. }
    59.  
    60. return blocks;
    61. }


    Sphere:
    Code:java
    1. public List<Location> getSphere(Location location, double radiusX, double radiusY, double radiusZ, boolean filled){
    2. Vector pos = location.toVector();
    3. World world = location.getWorld();
    4. List<Location> blocks = new ArrayList<Location>();
    5.  
    6. radiusX += 0.5;
    7. radiusY += 0.5;
    8. radiusZ += 0.5;
    9.  
    10. final double invRadiusX = 1 / radiusX;
    11. final double invRadiusY = 1 / radiusY;
    12. final double invRadiusZ = 1 / radiusZ;
    13.  
    14. final int ceilRadiusX = (int) Math.ceil(radiusX);
    15. final int ceilRadiusY = (int) Math.ceil(radiusY);
    16. final int ceilRadiusZ = (int) Math.ceil(radiusZ);
    17.  
    18. double nextXn = 0;
    19. forX: for (int x = 0; x <= ceilRadiusX; ++x) {
    20. final double xn = nextXn;
    21. nextXn = (x + 1) * invRadiusX;
    22. double nextYn = 0;
    23. forY: for (int y = 0; y <= ceilRadiusY; ++y) {
    24. final double yn = nextYn;
    25. nextYn = (y + 1) * invRadiusY;
    26. double nextZn = 0;
    27. forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {
    28. final double zn = nextZn;
    29. nextZn = (z + 1) * invRadiusZ;
    30.  
    31. double distanceSq = lengthSq(xn, yn, zn);
    32. if (distanceSq > 1) {
    33. if (z == 0) {
    34. if (y == 0) {
    35. break forX;
    36. }
    37. break forY;
    38. }
    39. break forZ;
    40. }
    41.  
    42. if (!filled) {
    43. if (lengthSq(nextXn, yn, zn) <= 1 && lengthSq(xn, nextYn, zn) <= 1 && lengthSq(xn, yn, nextZn) <= 1) {
    44. continue;
    45. }
    46. }
    47.  
    48. blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));
    49. blocks.add(pos.add(new Vector(-x,y,z)).toLocation(world));
    50. blocks.add(pos.add(new Vector(x,-y,z)).toLocation(world));
    51. blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));
    52. blocks.add(pos.add(new Vector(-x,-y,z)).toLocation(world));
    53. blocks.add(pos.add(new Vector(x,-y,-z)).toLocation(world));
    54. blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));
    55. blocks.add(pos.add(new Vector(-x,-y,-z)).toLocation(world));
    56. }
    57. }
    58. }
    59.  
    60. return blocks;
    61. }


    These methods are required:
    Code:java
    1. private static final double lengthSq(double x, double y, double z) {
    2. return (x * x) + (y * y) + (z * z);
    3. }
    4.  
    5. private static final double lengthSq(double x, double z) {
    6. return (x * x) + (z * z);
    7. }
     
    ChipDev likes this.
  7. Offline

    Hoolean

    Ivan

    Circles in Minecraft? Blasphemy! ;)

    Nice job, this looks a lot more efficient than my current method :D
     
    Ivan likes this.
  8. Offline

    Cybermaxke

    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); )
     
  10. Offline

    DevRosemberg

    ItzHectik

    Code:java
    1. final ArrayList<Location> blocks = getCircle(player.getLocation(), 10, 100);
    2.  
    3. new BukkitRunnable() {
    4. int loop = 0;
    5.  
    6. @Override
    7. public void run() {
    8. if (loop >= blocks.size()) {
    9. loop = 0;
    10. }
    11.  
    12. Particles.FLAME.display(blocks.get(loop++),0, 0, 0, 0, 10);
    13. }
    14. }.runTaskTimer(this, 0L, 1L);


    And just for fun (broke physics):

    [​IMG]

    Code:java
    1. new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4. for (Location location : blocks) {
    5. Particles.FLAME.display(location, 0, 0 ,0, 0,1);
    6. }
    7. }
    8. }.runTaskTimer(this, 0L, 1L);
     
Thread Status:
Not open for further replies.

Share This Page