Based on a known {x,y,z,...} coordinate, I'm looking for the index of a location. A 2-dimensional (2d) solution, is provided here. I'm now trying to extend this to two other dimensions: 1d and 3d (and possibly to generalize to higher dimensions).
For 1d, I ended up with the following algorithm (Matlab code), where the walk alternates between the right and left side of the axis:
n = 20; %number of values
X = -n/2:n/2; %X values (1d)
%we want 'p' the index of the location:
for i=1:numel(X)
if(X(i) > 0)
p(i) = 2*X(i)-1;
else
p(i) = -2*X(i);
end
end
resulting in the following indexes:
However, I have difficulties in vizualizing how the indexation should takes place in 3d (i.e. how the index walks through the nodes in 3d). I'm primarily interested in a C/C++ solution but any other language is fine.
EDIT Reflecting @Spektre comments and suggestions: I aim at finding the indexes of a set of 3d coordinates {x,y,z}. This can be seen as a way to map the 3d coordinates into a set of indexes (1d). The spiral provides a convenient way to perform such a task in 2d, but cannot be extended in 3d.