#[RESOLVED] How can i make a Sphere made of blocks with radius (WorldEdit)

1 messages · Page 1 of 1 (latest)

glossy copper
#

I'm trying to make a WorldEdit, I want a function, That can create spheres in blocks with a specefied size/radius.
I am not good in maths, So can anyone help me?

waxen stone
#

iterate over all blocks and see if it's in a radius from the spheres center
The easiest way

#

pseudocode

iterate over all positions from -r,-r,-r to r,r,r {
    if x,y,z distance to center is < r {
         world.getBlock().setType or smth
    } 
}
#

pretty sure you can have it a lil more optimised but don't think it's worth it

foggy raft
#

use runjob for that

glossy copper
waxen stone
#

the distance you calculate using the formula sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)

#

and the iterating is just a for loop on all coordinates from center + (-r,-r,-r) to center + (r,r,r)

glossy copper
#

Oh thanks, thats now clear to me!

glossy copper
# waxen stone and the iterating is just a for loop on all coordinates from center + (-r,-r,-r)...
function createSphere(center, radius, blockType, dimension) {
    const { x: cx, y: cy, z: cz } = center;

    for (let x = -radius; x <= radius; x++) {
        for (let y = -radius; y <= radius; y++) {
            for (let z = -radius; z <= radius; z++) {
                const dx = x;
                const dy = y;
                const dz = z;

                const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);

                if (distance <= radius) {
                    const blockPos = {
                        x: cx + x,
                        y: cy + y,
                        z: cz + z
                    };
                    
                    try {
                        dimension.getBlock(blockPos).setType(blockType);
                    } catch (e){}
                }
            }
        }
    }
}
#

Can you check if that function is right?

waxen stone
#

should be fine, yeah

glossy copper
#
function* createSphere(center, radius, blockType, dimension) {
    const { x: cx, y: cy, z: cz } = center;

    for (let x = -radius; x <= radius; x++) {
        for (let y = -radius; y <= radius; y++) {
            for (let z = -radius; z <= radius; z++) {

                const distance = Math.sqrt(x * x + y * y + z * z);
                if (distance > radius) continue;

                yield dimension.getBlock({
                    x: cx + x,
                    y: cy + y,
                    z: cz + z
                }).setType(blockType);
            }
        }
    }
}

Final code.
Post resolved ✅