Question

I have a 3d array with dimensions M x N x L, which defines the values on a cyclical volume (i.e. the M-1'th point is next to the 0th point, if we're zero indexing). If I'm given the coordinates of a point (X, Y, Z), how can I neatly find all the next door points (i.e. points which are one away in one dimension only)?

my initial solution was just to for loop over all values in the array (the array is fairly small) and then have something like:

if abs(X-M) + abs(Y-N) + abs(Z-L) == 1
  do neighboring point stuff
end

but this doesn't take into account of the cyclic aspect. Thoughts on a quick, neat way to do this would be fab.

thanks in advance!

Was it helpful?

Solution

this is what I've ended up doing for those interested (don't know why I didn't think of it sooner):

for dimension=1:3
  for direction=-1:2:1
    vec = zeros(3,1);
    vec(dimension) = direction;

    this_point = point + vec;
    this_point(dimension) = mod(this_point(dimension)-1,size(obj.int_ref,dimension))+1;
  end
end

note that this is matlab code, and so the arrays' indeces start at 1 (hence the unusual mod line).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top