Solved How to check if part of a cuboid region is in another region?

Discussion in 'Spigot Plugin Development' started by Snowmite, May 7, 2017.

Thread Status:
Not open for further replies.
  1. I am currently making a custom region plugin, and I want to know how to detect if a cuboid region is in part of another?

    Here is my current code for getting if a player is in an existing region:
    Code (Text):
        public static House getHouseHere(Location loc){
            List<House> regionList = Houses.getHouses ();
            for(House rg : regionList){
                Location p1 = rg.getP1 ();
                Location p2 = rg.getP2 ();

                int minX = p1.getBlockX () < p2.getBlockX () ? p1.getBlockX () : p2.getBlockX ();
                int minY = p1.getBlockY () < p2.getBlockY () ? p1.getBlockY () : p2.getBlockY ();
                int minZ = p1.getBlockZ () < p2.getBlockZ () ? p1.getBlockZ () : p2.getBlockZ ();

                int maxX = p1.getBlockX () > p2.getBlockX () ? p1.getBlockX () : p2.getBlockX ();
                int maxY = p1.getBlockY () > p2.getBlockY () ? p1.getBlockY () : p2.getBlockY ();
                int maxZ = p1.getBlockZ () > p2.getBlockZ () ? p1.getBlockZ () : p2.getBlockZ ();

                if(loc.getBlockX () >= minX && loc.getBlockX () <= maxX){
                    if(loc.getBlockY () >= minY && loc.getBlockY () <= maxY){
                        if(loc.getBlockZ () >= minZ && loc.getBlockZ () <= maxZ){
                            return rg;
                        }
                    }
                }
            }
            return null;
        }
    How would I check if a region is in another region?
     
  2. Choco

    Moderator

    Math#min() and Math#max() can help you out a lot here
    Code (Java):
    int minX = Math.min(p1.getBlockX(), p2.getBlockX());
    // etc.

    int maxX = Math.max(p1.getBlockX(), p2.getBlockX());
    // etc.
    Though your min/max points should be determined upon the creation of the House object. I see no reason for you to have to do that every single time you want to retrieve a House. Also... really not loving the static abuse ;)

    Lastly, you have access to Vector#isInAABB() which allows you to determine whether a Vector is within the confines of two specified vectors.
    Code (Java):
    Vector reference = location.toVector();

    // iterate over houses
    if (reference.isInAABB(house.getMin().toVector(), house.getMax().toVector()))
        return house;
     
  3. Didn't really answer my question...
     
  4. Choco

    Moderator

    You could probably take some inspiration from Java AWT's Rectangle#intersects() method. See it's source here. It should not be difficult to manipulate that to take into consideration of a third axis.
     
  5. Thats simple, use Java SDK!

    http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Area.html

    Construct Area objects from every region you have. Then call the intersect(Area other) method :)

    Edit: Or take Rectangle (to make it more easy). To get the third axis, create two objects: one with XY and one with XZ. When both intersect another regions two objects, the regions intersect. No need to change implementations.
     
  6. Solved. I just checked if the region the location was in is not null. In that case, it returns another region. Thanks for all of your help anyway!
     
  7. So you didnt want region intersections, but the method you already had? o_O
     
    • Agree Agree x 1
  8. I didn't even know what to do, so I tried random stuff and it ended up working.
     
    • Funny Funny x 1
  9. [​IMG]
     
    • Funny Funny x 1
Thread Status:
Not open for further replies.