There is a version of Breshenem's algorithm for drawing circles. Consider a two-dimensional place at z = 0 (suppose that at the moment the sphere is 0,0,0), and look only at the xy plane of the grid points. Starting from x = R, y = 0, follow the Breschenem algorithm to y = y_R, x = 0, except that instead of drawing, you simply use the result to know that all grid points with lower x coordinates are inside the circle, down to x = x_center. Put them on the list, count or otherwise pay attention. When this is done with a two-dimensional problem, repeat with a change in z and using the radius R (z) = sqrt (R ^ 2-z ^ 2) instead of R, until z = R.
If the center of the sphere is really located at the grid point, you know that each grid point inside or outside the right half of the sphere has a mirror partner on the left side, as well as top / bottom, so you can do half counting / enumeration per measurement. You can also save time by running Bresenham only on a 45-degree line, because any point x, y relative to the center has a partner y, x. If the sphere can be anywhere, you will have to calculate the results for each octantant.
source share