Equation for a sphere Math checking

Discussion in 'Plugin Development' started by Prgr, Jul 3, 2012.

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

    Prgr

    So based on a single location this code will make a sphere with the pBlock as the center.
    PHP:
     
    public void act(pBlock pB) {
    double bsd this.bp.brushSize;
    int bsi = (int) Math.round(bsd);
     
    //Y
    for (int by bsiby > -bsiby--) {
    double xzRadius HalfCircleFunction(bsd,by);
    int xzRadiusi = (int) Math.round(xzRadius);
    for(
    int bx xzRadiusibx > -xzRadiusibx--) {
    double zLength HalfCircleFunction(xzRadiusbx);
    int zLengthi = (int) Math.round(zLength);
    for(
    int bz zLengthibz > -zLengthibz--) {
    addBlockRelative(pB,bx,by,bz);
    }
    }
    }
     
    perform();
    }
     
    private 
    Double HalfCircleFunction(Double radiusint x){
    return 
    Math.sqrt((radius*radius)-(x*x));
    }
    often times the circles are not symmetrical is my problem.
     
  2. I use this function to make planets at my multiworld plugin:
    Code:java
    1. /**
    2. * Numbers used to make X^2 faster
    3. */
    4. public static final int[] SQUARES =
    5. {
    6. 0, 1, 4, 9, 16,
    7. 25, 36, 49, 64, 81, 100, 121, 144, 169, 196,
    8. 225, 256, 289, 324, 361,
    9. 400, 441, 484, 529, 576,
    10. 625, 676, 729, 784, 841,
    11. 900// Why do I not use the Matyh class, because this code is old!
    12. };
    13.  
    14. /**
    15. * Makes the input positive
    16. * @param i the input
    17. * @return The positive input
    18. */
    19. private int makePositive(int i)
    20. {
    21. if (i < 0)
    22. {
    23. return 0 - i;
    24. }
    25. return i;
    26. }
    27. protected void makePlanet(final World workingWorld,
    28. final int planetX,
    29. final int planetY,
    30. final int planetZ,
    31. final int size,
    32. final byte blockTop,
    33. final byte blockDown,
    34. final byte blockSpecial)
    35. {
    36. int mainDistance;
    37. boolean isTop = true;
    38. Block workingBlock;
    39.  
    40.  
    41. int posAX = planetX - size;
    42. int posAY = planetY - size;
    43. int posAZ = planetZ - size;
    44. int posBX = planetX + size;
    45. int posBY = planetY + size;
    46. int posBZ = planetZ + size;
    47. int comparatorSize = SQUARES[size];
    48. for (int x = posAX; x <= posBX; x++)
    49. {
    50. for (int z = posAZ; z <= posBZ; z++)
    51. {
    52. mainDistance = SQUARES[this.makePositive(x - planetX)] + SQUARES[this.makePositive(z - planetZ)];
    53. if (mainDistance <= comparatorSize)
    54. {
    55. isTop = true;
    56. for (int y = posBY; y >= posAY; y--)
    57. {
    58. if (comparatorSize < mainDistance + SQUARES[this.makePositive(y - planetY)])
    59. {
    60. continue;
    61. }
    62. workingBlock = workingWorld.getBlockAt(x, y, z);
    63. if (isTop)
    64. {
    65. workingBlock.setTypeIdAndData(blockTop,(byte)0,false);
    66. isTop = false;
    67. }
    68. else
    69. {
    70. workingBlock.setTypeIdAndData(blockDown,(byte)0,false);
    71. }
    72.  
    73. }
    74. }
    75.  
    76. }
    77. }
    78. workingWorld.getBlockAt(planetX, planetY, planetZ).setTypeIdAndData(blockSpecial,(byte)0,false);
    79.  
    80. }
    mayby you can edit it, so it does exactly what you want whit circles
     
  3. Offline

    Jnorr44

    do:
    Code:
    double volume = Math.PI * radius * radius * radius / 3 / 4;
    (this gives you an integer of the volume of a sphere)
    At least, thats what Wolvereness told me on irc yesterday
     
  4. Offline

    andf54

    double volume = Math.PI * radius * radius * radius * 4 / 3;
     
  5. Offline

    Jnorr44

    oh, just flip the 3 and the 4....
     
  6. Offline

    Prgr

    ferrybig I think I might try your SQUARES idea to test if it becomes more efficient. Otherwise I think I was wrong to ask for people's methods of achieving this task because I don't want to figure out how to use these methods :) Thanks Jnorr44 but I don't know what that is for :p
     
Thread Status:
Not open for further replies.

Share This Page