Hello, currently I'm working on a plots plugin with per-block-prices. To calculate these prices I have to know how many blocks (including AIR) there are in a com.sk89q.worldguard.protection.regions.ProtectedRegion Does anyone know how to realize this?
Assuming it's a cuboid, you could just use ProtectedRegion#getMinimumPoint() and ProtectedRegion#getMaximumPoint() and compute the volume of the cube by multiplying the width, height and depth.
@GermanElectronix you can probably still compute the surface from the points, and multiply it with the height. How? No clue, google probably knows .
Actually I just need the area. I already found this method: Code (Text): int blocks = ProtectedRegion.volume() / 256; It's working for cuboid but for polygonal regions it returns 0. I could calculate the area with vectors, but I have no idea how I could do that ^^
Do you need something like this? http://stackoverflow.com/questions/17579053/draw-polygon-and-calculate-area-in-java
That's not working for me. It returns wrong values. Code (Text): ProtectedRegion reg; float sum_but_no_result=0; for(int i=0; i<(reg.getPoints().size()-1); i++) { sum_but_no_result += reg.getPoints().get(i).getBlockX() * reg.getPoints().get(i + 1).getBlockZ() + reg.getPoints().get(i).getBlockZ() * reg.getPoints().get(i + 1).getBlockX(); } sum_but_no_result += reg.getPoints().get(reg.getPoints().size()-1).getBlockX() * reg.getPoints().get(0).getBlockZ() + reg.getPoints().get(reg.getPoints().size()-1).getBlockZ() * reg.getPoints().get(0).getBlockX(); float sum = (Math.abs(sum_but_no_result) / 2.0f) / 256; System.out.println("BLOCKS: " + String.valueOf(sum));
@GermanElectronix you could always pick an arbitrary point in the polygon, split the polygon up in triangles, compute the area of each triangle and sum the results. Or (and this is probably a pretty precise but slow method) recursively find all blocks in a slice of the polygon (so where all y is equal). Code (pseudo (Unknown Language)): // This assumes the initial call uses a block inside the polygon findArea(block, visited) found = 1 for each neighbour if visited contains neighbour continue add neighbour to visited found = found + findArea(neighbour, visited) return found Note: Java wise, visited should be a HashSet for best performance
I finally found a way to compute the area of polygons (learned at school ^^): Code (Text): public static int getArea(final List<BlockVector2D> points){ int[] x = new int[points.size()]; int[] z = new int[points.size()]; for(int i = 0; i<points.size(); i++){ x[i] = points.get(i).getBlockX(); z[i] = points.get(i).getBlockZ(); } int firstSum = 0; for(int i = 0; i<points.size()-1; i++) firstSum = x[i]*z[i+1]; int secondSum = 0; for(int i = 0; i<points.size()-1; i++) secondSum = z[i]*x[i+1]; firstSum -= secondSum; return firstSum / 2; } The problem is, that this method is returning wrong values. Maybe, ProtectedRegion.getPoints(); is returning an unsorted list?