Search code examples
javamathbukkit

Calculate dots on cuboid's grid


I have an issue with my math skills here. So I need to calculate the positions (x, y, z) of a specific amount of dots per aixs. For example take this imageenter image description here

final double gapX = lengthX / dotsPerXAxis;
final double gapY = lengthY / dotsPerYAxis;
final double gapZ = lengthZ / dotsPerZAxis;

for (BlockFace blockFace : BlockHandler.DIRECT_RELATIVES) {
    final DecimalVector3D minCorner = new DecimalVector3D(
        blockFace.getModX(),
        blockFace.getModY(),
        blockFace.getModZ()
    );

for (int x = 0; x < dotsPerXAxis || x == 0; x++) {
    for (int y = 0; y < dotsPerYAxis; y++) {
        for (int z = 0; z < dotsPerZAxis; z++) {

        }
    }
}

My question now is: how can I iterate over all the dots except those that are inside the cuboid and calculate their position and put them in an ImmutableList?


Solution

  • You need to treat point if at least one coordinate of it is zero or dotsPerZAxis.

    So set flags - if X-coordinate lies on face, if Y-coordinate lies on face. If both flags are not set - get only the first and the last Z-coordinates, otherwise walk through all Z-coordinates.

    Unchecked Java:

    for (int x = 0; x < dotsPerXAxis; x++) {
        bool atX = (x == 0) || (x == dotsPerXAxis - 1);
        for (int y = 0; y < dotsPerYAxis; y++) {
            bool atY = (y == 0) || (y == dotsPerYAxis - 1);
    
            int zstep = (atX || atY)? 1: dotsPerZAxis - 1;
    
            for (int z = 0; z < dotsPerZAxis; z+=zstep) {
               treat(x,y,z)
            }
        }
    }
    

    Ideone Python working code as proof-of-concept gives n^3 - (n-2)^3 points (26 surface points for n=3, 56 for n=4, 98 for n=5)