
:
function dist = manhattan_dist( p, q )
y1 = p(1);
y2 = q(1);
x1 = p(2);
x2 = q(2);
du = x2 - x1;
dv = (y2 - floor(x2 / 2)) - (y1 - floor(x1 / 2));
if ((du >= 0 && dv >= 0) || (du < 0 && dv < 0))
dist = abs(du) + abs(dv);
else
dist = max(abs(du), abs(dv));
end
end
:
, , manhattan_dist:
function all_paths = find_paths( from, to, i )
if i == 1
all_paths = to;
return;
end
all_paths = [];
neighbors = neighbor_nodes(from, 8);
for j = 1:length(neighbors)
if manhattan_dist(neighbors(j,:), to) <= i - 1
paths = find_paths(neighbors(j,:), to, i - 1);
for k = 1:size(paths, 1)
all_paths = [all_paths; {neighbors(j,:)} paths(k,:)];
end
end
end
end
, , :
function neighbors = neighbor_nodes( node, n )
y = node(1);
x = node(2);
neighbors = [];
neighbors = [neighbors; [y, x]];
if mod(x,2) == 1
neighbors = [neighbors; [y, x-1]];
if y > 0
neighbors = [neighbors; [y-1, x]];
end
if x < n - 1
neighbors = [neighbors; [y, x+1]];
neighbors = [neighbors; [y+1, x+1]];
end
neighbors = [neighbors; [y+1, x-1]];
if y < n - 1
neighbors = [neighbors; [y+1, x]];
end
else
if y > 0
neighbors = [neighbors; [y-1, x]];
neighbors = [neighbors; [y-1, x+1]];
if x > 0
neighbors = [neighbors; [y-1, x-1]];
end
end
if y < n
neighbors = [neighbors; [y+1, x]];
neighbors = [neighbors; [y, x+1]];
if x > 0
neighbors = [neighbors; [y, x-1]];
end
end
end
end
- node, node n . , (1, 1) (0, 3) (n = 2), , (1, 2), .