Perimeter of a Region

Discussion in 'Plugin Development' started by 97WaterPolo, Oct 19, 2014.

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

    97WaterPolo

    Hello!

    Well not sure how to phrase this, but I am trying to get the perimeter of a region, just the blocks on the outermost shell of a cuboid region. I am not entirely sure how I would do that, any help would be greatly appreciated.

    I currently use:
    Code:java
    1. public static List<Block> blocksFromTwoPointsChange(Location loc1, Location loc2) {
    2. List<Block> blocks = new ArrayList<>();
    3. int topBlockX = (loc1.getBlockX() < loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
    4. int bottomBlockX = (loc1.getBlockX() > loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
    5. int topBlockY = (loc1.getBlockY() < loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
    6. int bottomBlockY = (loc1.getBlockY() > loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
    7. int topBlockZ = (loc1.getBlockZ() < loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
    8. int bottomBlockZ = (loc1.getBlockZ() > loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
    9. for(int x = bottomBlockX; x <= topBlockX; x++) {
    10. for(int z = bottomBlockZ; z <= topBlockZ; z++) {
    11. for(int y = bottomBlockY; y <= topBlockY; y++) {
    12. Block block = loc1.getWorld().getBlockAt(x, y, z);
    13. blocks.add(block);
    14. }
    15. }
    16. }
    17. return blocks;
    18. }


    I would like to just get the blocks on the outside. The red stars in the diagram below. What check would I need to add to just get the outside?

    **********
    **********
    **********
    **********
    **********
    **********
     
  2. 97WaterPolo
    Code:java
    1. for (int x = bottomBlockX; x <= topBlockX; x++) {
    2. for (int z = bottomBlockZ; z <= topBlockZ; z++) {
    3. for (int y = bottomBlockY; y <= topBlockY; y++) {
    4. if ((x == bottomBlockX || x == topBlockX) && (z == bottomBlockZ || z == topBlockZ) && (y == bottomBlockY || Y == topBlockY)) {
    5. // ...
    6. }
    7. }
    8. }
    9. }
     
  3. Offline

    97WaterPolo

    Assist
    Thanks!

    Assist
    That seems to only get the two corners, not the whole perimeter. Any ideas?

    I may have not been clear, but I am trying to get just the north south east and west of a cube, not the top and bottom side of it, kinda hard to explain that one. The walls of sorts of the cube.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  4. Offline

    rbrick

    97WaterPolo While ternary is great, Java's Math class has these two nifty methods called Math#min and Math#max, Pass the method 2 numbers and it returns the smallest(if you use min) or largest(if you use max) :)
     
  5. Offline

    97WaterPolo

    rbrick
    Will do, but is that for creating the cuboid, or getting the walls, not entirely sure how you would use the Math#min or Math#max to get the walls.
     
  6. Offline

    rbrick

    97WaterPolo It is just to reduce boiler plate code(The ternary for getting the highest and lowest points) as for the perimeter i am not the best at math, sorry.
     
  7. Offline

    teej107

    1. Get the two location points.
    2. For <insert axis here>, get both the location's <insert axis here> value and find the difference of those two numbers.
    3. Get the lowest <insert axis here> value from the 2 locations.
    4. For-loop through the locations starting from the number gotten from 3. going up and if the index of the for-loop goes above the number from 2. stop.
    5. Repeat steps 2 - 4 to get the other values from the axis.
     
    rbrick and 97WaterPolo like this.
  8. Offline

    97WaterPolo

    rbrick Ah, well thanks anyways!
     
  9. Offline

    rbrick

    97WaterPolo Hope you figure it out :) now then, off to learn basics of math again! xD
     
  10. Offline

    97WaterPolo

    rbrick
    *le sigh* If I had known this much math would be required in programming, I might have actually tried to pay attention in class :p
     
    rbrick likes this.
  11. Offline

    Konato_K

    97WaterPolo
    Code:java
    1. minX = minX;
    2. for(int i=minZ; i<maxZ; i++)
    3. { for(int j=minY; i<maxY; i++)
    4. { //Add block and stuff
    5. }
    6. }
    7. maxX = maxX;
    8. for(int i=minZ; i<maxZ; i++)
    9. { for(int j=minY; i<maxY; i++)
    10. { //Add block and stuff
    11. }
    12. }
    13. minZ = minZ;
    14. for(int i=minX; i<minX; i++)
    15. { for(int j=minY; i<maxY; i++)
    16. { //Add block and stuff
    17. }
    18. }
    19. maxZ = maxZ;
    20. for(int i=minX; i<minX; i++)
    21. { for(int j=minY; i<maxY; i++)
    22. { //Add block and stuff
    23. }
    24. }


    I didn't tested it, but I think it can work fine
     
  12. Offline

    teej107

    The "this much math" part of programming allows you to develop much cooler things. You can develop programs with little or no math, but they'll be boring.
     
  13. Offline

    97WaterPolo

    teej107
    True, I mean you MUST have algebra for variable solving.

    Konato_K
    I would need to do that for each axis? Why must everything include multiple for loops with min and max variables :L thanks
     
  14. Offline

    Konato_K

    97WaterPolo If you mean the redundant minX = minX code remove it, I was just messing around, and well, since you don't want to fill the cuboid I'm using a cycle for each wall, I think that's what you want, right?

    Edit: Ok I put the code in one of my plugins and tested it, the final code looks like this
    Code:
    x = minX;
    for(z=minZ; z<=maxZ; z++)
    { for(y=minY; y<=maxY; y++)
      { Block block = world.getBlockAt(x, y, z);
        block.setType(type);
        block.setData(data);
      }
    }
    x = maxX;
    for(z=minZ; z<=maxZ; z++)
    { for(y=minY; y<=maxY; y++)
      { Block block = world.getBlockAt(x, y, z);
        block.setType(type);
        block.setData(data);
      }
    }
    z = minZ;
    for(x=minX; x<=maxX; x++)
    { for(y=minY; y<=maxY; y++)
      { Block block = world.getBlockAt(x, y, z);
        block.setType(type);
        block.setData(data);
      }
    }
    z = maxZ;
    for(x=minX; x<=maxX; x++)
    { for(y=minY; y<=maxY; y++)
      { Block block = world.getBlockAt(x, y, z);
        block.setType(type);
        block.setData(data);
      }
    }
    
    And it makes something like this http://i.imgur.com/lzgsIAf.png
     
    97WaterPolo likes this.
Thread Status:
Not open for further replies.

Share This Page