Question

I've been desperately attempting to create a sphere tool in my voxel engine. I know the general function for creating a sphere:

public bool getPoint (int x, int y, int z) {
    return (x*x+y*y+z*z < r*r) ? true : false;
}

This function will create a sphere assuming the origin is at (0, 0, 0). Yet I want to be able to create a sphere with a custom origin (or user-defined) say the origin is at (10, 10, 10). How would I modify this function to move the origin to a different position?

Was it helpful?

Solution

Let ox, oy, oz be the center of your sphere. Then you just subtract the center from x, y, z before doing your computation:

public bool getPoint (int x, int y, int z, int ox, int oy, int oz)
{
    x -= ox;
    y -= oy;
    z -= oz;
    return (x*x+y*y+z*z < r*r);
}

Note that the expression expr ? true : false is equivalent to expr regardless of what expr is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top