0

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?

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • 1
    Short version: subtract 10 from your coordinates before doing your math. You're applying a negative world translation to compensate for the object really moving. This has nothing to do with "creating" spheres, nor with voxel theory though. – Blindy Feb 03 '14 at 21:18

1 Answers1

0

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.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173