Taking d as the number of dimensions and r as the radius, I know at least two different approaches that you can use:
Using a spatial hash:
Imagine a task space divided into hypercubes by a grid of size s such that s = r / sqrt(d) .
The interesting thing about s = r / sqrt(d) is that any two points inside a hypercube of this size are guaranteed to be at a distance equal to or less than r .
You can number the grid divisions so that each hypercube can be identified by a tuple formed by the indices of one of its angles. These index tuples can be used as keys in a hash structure (spatial hash).
Now, and this is the difficult part, you should notice that for any hypercube of grid A there are many neighboring hypercubes N = (N1, N2, ...) whose minimum distance to a given hypercube is equal to or less than a given radius, geometrically they look like a hypersphere . Elements of the set N can be represented as deltas associated with the grid indices, with A Note that these delta indices depend only on d .
For example, in the two-dimensional case, you have the following geometric structure:
NNN index deltas: (-1, 2) ( 0, 2) ( 1, 2) NNNNN (-2, 1) (-1, 1) ( 0, 1) ( 1, 1) ( 2, 1) NNANN (-2, 0) (-1, 0) ( 0, 0) ( 1, 0) ( 2, 0) NNNNN (-2, -1) (-1, -1) ( 0, -1) ( 1, -1) ( 2, -1) NNN (-1, -2) ( 0, -2) ( 1, -2)
So, you already have everything you need to effectively search for points within the hypersphere:
Press the entry points into the spatial hash using the index cube of the hypercube containing them as a key.
For point p search method is to determine the hypercube where it lies A , and then, using the set of delta indices, obtain a set of neighbor hypercubes N , which can potentially contain points closer than r .
Extract points belonging to the hypercubes N from the spatial hash and check which ones are sufficiently close to p .
There are several additional optimizations that can be performed because they do not check the points in A, since they are guaranteed to be close enough. Pre-filtering N can be performed depending on the position of p relative to A
Note that choosing s = r / sqrt(d) provides a good compromise between having small hypercubes and a not-too-large set of N
Using the kd tree or quad / octo /...- tree:
Each time you go down one level in the tree, you check to see if it crosses the space with the query hypersphere. If so, you continue to decline, otherwise you completely drop it in search.