Solved Generate Cylinder? (Worked yesterday, but it Valon'd)

Discussion in 'Plugin Development' started by valon750, Jul 3, 2014.

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


    Evening all,

    So yesterday I was asking the community about a project regarding HashMaps, where I hinted at generating shapes such as spheres and cubes.

    Now last night before I went to bed, I had a perfectly working Cylinder generator code snippet, however I get back from work to find it now generates a tall cuboid.. hence my use of the word "Valon'd".

    for (int x = blockX - radius; x <= blockX + radius; x++) {
                for (int y = blockY - radius; y <= blockY + radius; y++) {
                    for (int z = blockZ - radius; z <= blockZ + radius; z++) {
                        double distance = ((blockX - x) * (blockX - x) + ((blockZ - z) * (blockZ - z)));
                        if (distance < radius * radius && !(hollow && distance < ((radius - 1) * (radius - 1)))) {
                            Location l = new Location(centerBlock.getWorld(), x, y, z);
    This is my depressing attempt at fixing it, taking reference from my sphere generation. I don't suppose any guru's can point out my obvious mistake whilst I top up on caffeine?

    Much appreciated as always,
  2. Offline


    Well first off, you shouldn't be applying the radius parameter to the y axis, unless your cylinder is lying on it's side, in which case the radius parameter shouldn't be applied to either the x or the x axis. But is should go from a base to a maximum value, fix that and then tell us what changes.
  3. Offline


    It looks like he's trying to make a cylinder that's as tall as its radius. He isn't using the y coordinate in the distance squared calculation.

    Anyways, valon750 I have no idea what would be wrong with your code especially since you say it's creating cuboids. You could try making it solid to see if it's something with how your checking the distance away as it may not be using the bounds correctly since it's distance squared and you are only trying to give it a thickness of one along with trying to retrieve a double with only using integers.

    Since none of that probably makes any sense and I can't see any more sane reason that your code shouldn't be working, I'll post my own (untested) code that I am fairly sure should work as I've used similar code to make circles, spheres, spirals, etc... but never actually cylinders:
    2. Block centerBlock = /*???*/;
    3. Location center = centerBlock.getLocation();
    4. int centerX = center.getBlockX(), centerY = center.getBlockY(), centerZ = center.getBlockZ();
    5. int radius = 10;
    6. int minRadius = (radius - 2) * (radius - 2)
    7. int maxRadius = radius * radius
    8. //I've had weird issues with not getting every single point so I typically use a little more than my wanted radius here
    9. for(int i = - (radius * 1.5); i < (radius * 1.5); i++)
    10. {
    11. for(int a = 0; a < radius; a++)
    12. {
    13. for(int b = -(radius * 1.5); b < (radius * 1.5); b++)
    14. {
    15. int distSqu = ((i * i) + (a * a));
    16. if(distSqu <= maxRadius && distSqu >= minRadius)
    17. {
    18. center.getWorld().getBlockAt(centerX + i, centerY + a, centerZ + b).setType(Material.STONE);
    19. }
    20. }
    21. }
    22. }

    That should create a cylinder with a wall thickness of 2 with a radius and height of 10. This looks pretty much the same as your code with a few modifications so I'm not quite sure. It may actually be that you're checking < and > when you should be using <= and >=
  4. Offline



    Thank you very much for the detailed response :)

    It'll sound stupid, but it was all down to the radius! I altered it from 3 to 5 and you could see it taking shape. However I was under the impression that it could be smaller and still keep the rounded edges.

    I would imagine it's down to my check for if the distance is less than the radius etc, perhaps if I have it check for <= rather than <, it'll work out. However I'm now not near the computer so can't test that theory.

    Regardless, it'll all sorted now so thank you for your response, I very well may use this as reference in the future, you seem to know what you're talking about :)
Thread Status:
Not open for further replies.

Share This Page