In theory, it is easy to generalize, and recursion (by the number of dimensions) seems to me a suitable tool.
I would have done something like this (I didn’t test it with pseudocode either, sorry, I’m not a php expert):
process_points(int currdim, point currpoint, point center, double radius) { var r2 = radius * radius; var currpointR2 = 0; for (var dim = 0; dim < currdim - 1; dim++) currpointR2 += (currpoint[dim] - center[dim]) * (currpoint[dim] - center[dim]); var restR2 = r2 - currpointR2; if (restR2 < 0) return; var restR = sqrt(restR2); var min = floor(center[currdim] - restR); var max = ceil(center[currdim] + restR); for (var xx = min; xx <= max; xx++) { point newCurrPoint = currpoint; newCurrPoint[dim] = xx; if (currdim < totaldims - 1) process_points(currdim + 1, newCurrPoint, center, radius); else // last dim paint_point(newCurrPoint); } } process_points(0, center, center, r);