문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top