After computing the distance map with the previous function, the following one finds the minimum distance route from the start point to the final one.
function route = findroute(startcell,finalcell,distmap)
global map;
q = startcell;
cell = startcell;
d = 100000;
while(cell(1)~=finalcell(1) || cell(2)~=finalcell(2))
n = neighbours(cell);
for i=1:size(n,1)
if(map(n(i,1),n(i,2))==0 && distmap(n(i,1),n(i,2))~=0 && distmap(n(i,1),n(i,2))<d)
d = distmap(n(i,1),n(i,2));
next = n(i,:)
end
end
q = insert(next,q);
cell = next;
end
for i=1:size(q,1)
map(q(i,1),q(i,2))=2;
end
map
route = q
end
The function returns the list of cells that form the route as well as the map which shows the route (represented by values of 2)
No comments:
Post a Comment