[Protection] Cuboid'ful snippets

Discussion in 'Resources' started by Panjab, Dec 30, 2013.

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

    Panjab

    Look one post down, this one wasn't correctable. (A bug happened..)​

    First at all, you need:

    Code:
    World w;
    int minX, minY, minZ, maxX, maxY, maxZ;
    
    Compact Methods for each "size"

    Code:
    public int getX() {
      return maxX - minX + 1;
    }
     
    public int getY() {
      return maxY - minY + 1;
    }
     
    public int getZ() {
      return maxZ - minZ + 1;
    }
    
    Check if a location is inside two points

    Code:
      public boolean isInside(Location loc) {
        if (loc.getWorld() != w) return false;
     
        if ((loc.getBlockX() >= minX) && (loc.getBlockX() <= maxX) &&
          (loc.getBlockY() >= minY) && (loc.getBlockY() <= maxY) &&
          (loc.getBlockZ() >= minZ) && (loc.getBlockZ() <= maxZ)) {
          return true;
        }
     
        return false;
      }
    Get almost exactly center of a region

    Code:
      public Location getCenter() {
        int x1 = maxX + 1;
        int y1 = maxY + 1;
        int z1 = maxZ + 1;
        return new Location(w, minX + (x1 - minX) / 2.0D, minY + (y1 - minY) / 2.0D, minZ + (z1 - minZ) / 2.0D);
      }
    Get the surface (2D) of a region

    Code:
    public int getSurface() {
        return getX() * getZ();
    }
    Get volume

    Code:
    public int getVolume() {
        return getX() * getY() * getZ();
    }

    Will be continued soon.. :)

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

    Not2EXceL

    So you learned basic math? I don't see anything special thats relative other than math.
     
    Ivan, DSH105 and bobacadodl like this.
  3. Offline

    BungeeTheCookie

    This could be helpful to some people.
     
    Panjab likes this.
  4. Offline

    bobacadodl

    This is pretty basic stuff, but along the same lines...
    Code:
     //c is sphere center
        public static boolean isInsideSphere(Location l, Location c, double radius){
            return l.distanceSquared(c)<(radius*radius);
        }
    (this is untested, but i did the math out and it should work...)
    Code:
    /**
        * @param l The point to test
        * @param c1 Axis point 1
        * @param c2 Axis point 2
        * @param radius The radius
        * @return If it is inside the cylinder
        *
        * for multiple usage, precompute lensq and radsq
        */
        boolean isInsideCylinder(Location l, Location c1, Location c2, double radius){
            double lensq = c1.distanceSquared(c2);
            double radsq = radius*radius;
            //vec from c1 to c2
            Location d = c2.subtract(c1);
            //vec from c1 to l
            Location d2 = l.subtract(c1);
            double dot = d2.getX()*d.getX() + d2.getY()*d.getY() + d2.getZ()*d.getZ();
            if(dot<0 || dot>lensq){
                return false;
            }
            else{
                return ((d2.getX()*d2.getX() + d2.getY()*d2.getY() + d2.getZ()*d2.getZ()) - dot*dot/lensq) <= radsq;
            }
        }
     
    DSH105 likes this.
  5. Offline

    Panjab

    I know this is just basic Math, but some guys in these "business" aren't able to calculate ;) There are so many threads out there with the title "How to check if a block/player is inside a region".

    Even simple things can help.
     
Thread Status:
Not open for further replies.

Share This Page