-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortest_path.m
More file actions
88 lines (67 loc) · 3.29 KB
/
shortest_path.m
File metadata and controls
88 lines (67 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function shortest_path(m, n, startCell, goalCell, obstacles)
% Display the grid with distances
display_distances(m, n, startCell, goalCell, obstacles);
% Generate the distance matrix using the Grassfire algorithm
distances = grassfire_algorithm(m, n, goalCell, obstacles);
% Convert goalCell to row and column indices
[goalRow, goalCol] = ind2sub([m, n], goalCell);
% Initialize current position at the start cell
[currentRow, currentCol] = ind2sub([m, n], startCell);
% Hold on to overlay the path on top of the existing grid
hold on;
% Mark the start cell as a robot shape
draw_robot(currentCol - 0.5, m - currentRow + 0.5);
% Path tracking
path = [currentRow, currentCol]; % Initialize path with start cell
% Shortest path logic: follow the neighbor with the smallest distance
while true
if currentRow == goalRow && currentCol == goalCol
break; % Exit if we reach the goal cell
end
% Directions for moving up, down, left, right
directions = [0, 1; 0, -1; -1, 0; 1, 0];
% Initialize variables to find the next cell
minDistance = Inf;
nextCell = [currentRow, currentCol]; % Default to current cell
% Check all 4 adjacent cells
for i = 1:size(directions, 1)
newRow = currentRow + directions(i, 1);
newCol = currentCol + directions(i, 2);
% Check if the new cell is within bounds
if newRow >= 1 && newRow <= m && newCol >= 1 && newCol <= n
% Only consider valid cells that are not obstacles (Inf)
if distances(newRow, newCol) < minDistance
minDistance = distances(newRow, newCol);
nextCell = [newRow, newCol];
end
end
end
% Move to the next cell with the smallest distance
currentRow = nextCell(1);
currentCol = nextCell(2);
% Store the path
path(end + 1, :) = [currentRow, currentCol];
% Plot the path cell as a robot shape
draw_robot(currentCol - 0.5, m - currentRow + 0.5);
% Pause to visualize the movement
pause(0.75); % Adjust pause time as needed for better visibility
end
% Mark the goal cell in robot shape
draw_robot(goalCol - 0.5, m - goalRow + 0.5); % Keep goal as a robot shape
% Display the distance value at the goal cell (0 in black)
text(goalCol - 0.5, m - goalRow + 0.5, '0', ...
'HorizontalAlignment', 'center', 'Color', 'k', ... % Change to black
'FontSize', 12, 'FontWeight', 'bold');
% Display values for the path
for idx = 1:size(path, 1)
row = path(idx, 1);
col = path(idx, 2);
% Get the distance value
distanceValue = distances(row, col);
% Display the distance at the center of each path cell in black
text(col - 0.5, m - row + 0.5, num2str(distanceValue), ...
'HorizontalAlignment', 'center', 'Color', 'k', ... % Change to black
'FontSize', 12, 'FontWeight', 'bold');
end
hold off; % Release the plot
end