Solved Check to see if loc is in a region

Discussion in 'Plugin Development' started by NoLiver92, Jun 21, 2014.

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

    NoLiver92

    For some reason my mind has gone blank, i have a region defined by the two corners pos1, pos2 (both locations). I then have the input Location loc.

    I need to check if the location is inside the defined region but I cant remember how to do it.

    Could someone please help me and tell me how to do it (code not necessary but would be appreciated).

    Cheers,
    NoLiver
     
  2. NoLiver92
    Something along the lines of
    Code:java
    1. public boolean isInside(Location loc, Location l1, Location l2) {
    2. int x1 = Math.min(l1.getBlockX(), l2.getBlockX());
    3. int y1 = Math.min(l1.getBlockY(), l2.getBlockY());
    4. int z1 = Math.min(l1.getBlockZ(), l2.getBlockZ());
    5. int x2 = Math.max(l1.getBlockX(), l2.getBlockX());
    6. int y2 = Math.max(l1.getBlockY(), l2.getBlockY());
    7. int z2 = Math.max(l1.getBlockZ(), l2.getBlockZ());
    8.  
    9. return x >= x1 && x <= x2 && y >= y1 && y <= y2 && z >= z1 && z <= z2;
    10. }
     
    NoLiver92 likes this.
  3. Offline

    NoLiver92

    Assist Thats perfect, I couldn't remember how to get the max and min. Brilliant Thanks
     
  4. NoLiver92
    Now that I look at the code, x y and z aren't defined, but those would be the "loc" coordinates I believe. Grabbed this code from one of my project's classes
     
  5. Offline

    NoLiver92


    Not a problem I understood :) got it working thanks
     
  6. Offline

    Panjab

    Actually it's waaay easier:

    Code:java
    1.  
    2. public boolean isInside(Location l1, Location l2, Location check) {
    3. if (!check.getWorld().equals(l1.getWorld()) || !check.getWorld().equals(l2.getWorld())
    4. return false;
    5.  
    6. return check.toVector().isInAABB(l1.toVector(), l2.toVector());
    7. }
     
  7. Panjab
    Contents of that method:
    Code:java
    1. return x >= min.x && x <= max.x && y >= min.y && y <= max.y && z >= min.z && z <= max.z;
    the method I showed:
    Code:java
    1. return x >= x1 && x <= x2 && y >= y1 && y <= y2 && z >= z1 && z <= z2;
     
Thread Status:
Not open for further replies.

Share This Page