I ran into a little problem today that I can’t seem to solve in an efficient way. I’d like to select all cells of a 3D grid given the center of a sphere and the radius.
I have a cubic grid of cells, which all have the same dimensions, i.e. the cube has same width height and depth and is divided into sub cubes (“cells”) that each have the same width height and depth as well.
Given a 3D position within this grid, I would like to draw all the cells around this position within the radius of the sphere. All cells that are partially contained in the sphere should be included in the drawing.
Calculate the distance of the corners of the box from the center of the sphere:
If smaller or equal to your radius draw the cube…
(EDIT: As Oli comments you can compare to the sqaure of the radius to speed up this test in application)
You can only consider cubes within the bounding r x r x r cube…
Also see:
fast sphere-grid intersection