I have a vector of `int`s that represents a point on a N-dimensional grid. I need to find all grid points that are within a given radius. I wrote the following, but it is rather inefficient, because it iterates through all points in a cube and checks the radius for each point:

```
typedef std::vector<int> VectI;

bool getNextInNSphere(VectI& x,int radius){
    int pos = 0;
    bool done = false;
    while (!done && pos < x.size()){
        x[pos]++;
        if (x[pos]>radius){ x[pos] = -radius; pos++; }
        else              { done = true; }
    }
    if (done){
        int sqsum = 0;
        for (size_t i=0;i<x.size();i++){ sqsum += x[i]*x[i]; }
        if (sqsum > radius*radius){ return getNextInNSphere(x,radius); }
    }
    return done;
}

```

usage:

```
void foo(const VectI& x,int radius){
    VectI d = VectI(x.size(),-radius);
    while (getNextInNSphere(d,radius)){
        // x+d is inside a sphere of given radius around x
    }
}

```

Any ideas how to get this more efficient and/or in a nicer way?