40 lines
1.2 KiB
Matlab
40 lines
1.2 KiB
Matlab
function plotTour(positions, route)
|
||
% 绘制遍历路径的示意图
|
||
% 输入:positions - 包含城市坐标的矩阵
|
||
% route - 遍历路径
|
||
|
||
% 创建新图形窗口
|
||
figure;
|
||
|
||
% 绘制所有城市点
|
||
plot(positions(:, 2), positions(:, 3), 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'blue');
|
||
hold on;
|
||
|
||
% 绘制路径连线
|
||
for i = 1:length(route)-1
|
||
current = route(i);
|
||
next = route(i+1);
|
||
|
||
% 绘制从当前城市到下一个城市的线
|
||
line([positions(current, 2), positions(next, 2)], ...
|
||
[positions(current, 3), positions(next, 3)], ...
|
||
'Color', 'red', 'LineWidth', 1.5);
|
||
end
|
||
|
||
% 标记起点和终点(相同城市)
|
||
plot(positions(route(1), 2), positions(route(1), 3), 'gs', 'MarkerSize', 12, 'MarkerFaceColor', 'green');
|
||
|
||
% 添加城市标签
|
||
for i = 1:size(positions, 1)
|
||
text(positions(i, 2), positions(i, 3), num2str(i), ...
|
||
'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');
|
||
end
|
||
|
||
% 添加图例和标题
|
||
xlabel('X坐标');
|
||
ylabel('Y坐标');
|
||
title('城市遍历路径');
|
||
grid on;
|
||
legend('城市', '路径', '起点/终点');
|
||
hold off;
|
||
end |