Efficient way to check if a location is within a cuboid zone (defined by two other locs)

Discussion in 'Plugin Development' started by captainawesome7, Aug 16, 2011.

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

    captainawesome7

    Basically I failed at coming up with a Boolean method that checks to see if the location is "within" the other two.
    Code:
    public Boolean isInside(Location loc, Location corner1, Location corner2) {
    //If loc is "inside" the cuboid region defined by the two corner locations, return true
    
    }
    I know that I could loop through it, checking if any location is equal to loc, but that would be terrible, right?
     
  2. Offline

    bassfader

    Thats how I would go about it (written down in Notepad++ in several minutes, so it may include some errors, but well I think you get what I mean :)):

    Code:java
    1. public Boolean isInside(Location loc, Location corner1, Location corner2) {
    2. double xMin = 0;
    3. double xMax = 0;
    4. double yMin = 0;
    5. double yMax = 0;
    6. double zMin = 0;
    7. double zMax = 0;
    8. double x = loc.getX();
    9. double y = loc.getY();
    10. double z = loc.getZ();
    11.  
    12. xMin = Math.min(corner1.getX(), corner2.getX());
    13. xMax = Math.max(corner1.getX(), corner2.getX());
    14.  
    15. yMin = Math.min(corner1.getY(), corner2.getY());
    16. yMax = Math.max(corner1.getY(), corner2.getY());
    17.  
    18. zMin = Math.min(corner1.getZ(), corner2.getZ());
    19. zMax = Math.max(corner1.getZ(), corner2.getZ());
    20.  
    21. return (x >= xMin && x <= xMax && y >= yMin && y <= yMax && z >= zMin && z <= zMax);
    22. }
     
  3. Offline

    captainawesome7

    Looks good, I'll try it out.
     
Thread Status:
Not open for further replies.

Share This Page