The function to find the neighbours of a cell is:
function list = neighbours(cell)
global map;
x = cell(1);
y = cell(2);
if(x==1 && y==1)
list = [ x y+1;
x+1 y+1;
x+1 y];
elseif(x==1 && y==size(map,2))
list = [x+1 y;
x y-1;
x+1 y-1];
elseif(x==1)
list = [ x y+1;
x+1 y+1;
x+1 y;
x y-1;
x+1 y-1];
elseif(y==1 && x==size(map,1))
list = [x-1 y+1;
x y+1;
x-1 y];
elseif(y==1)
list = [x-1 y+1;
x y+1;
x+1 y+1;
x-1 y;
x+1 y];
elseif(x==size(map,1) && y==size(map,2))
list = [x-1 y;
x-1 y-1;
x y-1];
elseif(x==size(map,1))
list = [x-1 y+1;
x y+1;
x-1 y;
x-1 y-1;
x y-1];
elseif(y==size(map,2))
list = [x-1 y;
x+1 y;
x-1 y-1;
x y-1;
x+1 y-1];
else
list = [x-1 y+1;
x y+1;
x+1 y+1;
x-1 y;
x+1 y;
x-1 y-1;
x y-1;
x+1 y-1];
end
end
Note: Only the valid neighbours of the cell are taken, i.e. the ones that do not exceed the borders of the matrix.
No comments:
Post a Comment