MATLAB轻松绘制地图路线——已知及未知坐标下的处理方法(1)
生活随笔
收集整理的這篇文章主要介紹了
MATLAB轻松绘制地图路线——已知及未知坐标下的处理方法(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 已知坐標的情況
- 未知坐標的情況
- 完整工程文件下載鏈接:
要想繪制地圖路線, 最基本的要素就是 各點的坐標,有了坐標,還要知道哪個點和哪個點相連,最后將各點相連即可;
但有時候我們有的往往只是一張地圖,這樣其實也不難,MATLAB中有一個 [x,y]=ginput() 函數,將地圖以圖片的形式導入,鼠標點擊我們要標記的點,即可返回點坐標,這樣就將未知坐標的情況轉化成了第一種情況。
已知坐標的情況
假如已給我們想要的各節點的起點坐標和終點坐標(即橫縱坐標),我們首先將Excel表導入進MATLAB,然后以列矩陣的形式來保存數據,并處理。
這里將Excel中的數據先拆成了三個工作區數據data1,data2,data3,并導入數組x1,x2,x3,分別用來存節點橫坐標、節點縱坐標,要相連的節點和節點標號。
要想畫多個區的路線也是類似的操作,只需導入更多的節點數據:
未知坐標的情況
這里我們要用到ginput()函數,將地圖以圖片的形式導入,運行后點擊想要設置的點,最后就可自動獲取按圖片比例返回的橫縱坐標;
clc,clear,close all; Im=imread('map1.png'); %導入圖片 imshow(Im) disp('請連續點擊27次需要選取的坐標') [x,y]=ginput(27); %獲取區域中心點坐標 map_position = [0,0;0,0]; %預分配內存 for i = 1:27map_position(i,1)=x(i); %將坐標保存在map_position數組中map_position(i,2)=y(i); end以下例程是根據這個函數畫出的地圖全連接圖,以及用Dijkstra算法求出其中一種目標終點下的最短路線(圖中假設必須經過村莊和礦山)。
clc;clear;close all; %% 繪制地圖及基本路線 load map1_data load map1_position Im=imread('map1.png'); imshow(Im) hold on figure(1) for count_1 = 1:length(map1_position)plot(map1_position(count_1,2),map1_position(count_1,3),'.','Markersize',25); %繪制散點圖 end for count_2 = 1:length(map1_data)dotx = [map1_position(map1_data(count_2,1),2),map1_position(map1_data(count_2,2),2)]; %繪制所有路線doty = [map1_position(map1_data(count_2,1),3),map1_position(map1_data(count_2,2),3)]; plot(dotx,doty,'Linewidth',1.5) end hold off %% Dijkstra算法求目標下最優路徑 n=27; %設置鄰接矩陣大小 temp=1; %設置起點 %定義鄰接矩陣lines lines = zeros(27); lines(1,2)=1; lines(1,25)=1; lines(2,1)=1; lines(2,3)=1; lines(3,4)=1; lines(3,25)=1; lines(4,3)=1; lines(4,25)=1; lines(5,4)=1; lines(5,24)=1; lines(6,5)=1; lines(6,24)=1; lines(6,23)=1; lines(7,6)=1; lines(7,22)=1; lines(7,8)=1; lines(8,7)=1; lines(8,9)=1; lines(8,22)=1; lines(9,8)=1; lines(9,22)=1; lines(9,10)=1; lines(9,15)=1; lines(9,16)=1; lines(9,17)=1; lines(9,21)=1; lines(9,22)=1; lines(10,9)=1; lines(10,15)=1; lines(10,11)=1; lines(10,13)=1; lines(11,10)=1; lines(11,13)=1; lines(11,12)=1; lines(12,11)=1; lines(12,13)=1; lines(12,14)=1; lines(13,10)=1; lines(13,11)=1; lines(13,15)=1; lines(13,12)=1; lines(14,13)=1; lines(14,15)=1; lines(14,12)=1; lines(14,16)=1; lines(15,9)=1; lines(15,10)=1; lines(23,26)=1; lines(15,13)=1; lines(15,14)=1; lines(15,16)=1; lines(16,9)=1; lines(16,15)=1; lines(16,14)=1; lines(16,17)=1; lines(16,18)=1; lines(17,9)=1; lines(17,18)=1; lines(17,16)=1; lines(17,21)=1; lines(18,17)=1; lines(18,16)=1; lines(18,20)=1; lines(18,19)=1; lines(19,18)=1; lines(19,20)=1; lines(20,19)=1; lines(20,18)=1; lines(20,21)=1; lines(21,20)=1; lines(21,9)=1; lines(21,22)=1; lines(21,23)=1; lines(21,27)=1; lines(21,17)=1; lines(22,23)=1; lines(22,6)=1; lines(22,7)=1; lines(22,8)=1; lines(22,9)=1; lines(22,21)=1; lines(23,6)=1; lines(23,24)=1; lines(23,26)=1; lines(24,23)=1; lines(24,6)=1; lines(24,5)=1; lines(24,4)=1; lines(24,25)=1; lines(24,26)=1; lines(25,24)=1; lines(25,4)=1; lines(25,3)=1; lines(25,1)=1; lines(26,25)=1; lines(26,24)=1; lines(26,23)=1; lines(26,27)=1; lines(27,26)=1; lines(27,21)=1; lines(25,26)=1; lines(23,22)=1; %繪制圖節點和邊 G = digraph(lines); [path1,d1] = shortestpath(G,1,15); [path2,d2] = shortestpath(G,15,12); [path4,d4] = shortestpath(G,12,15); [path3,d3] = shortestpath(G,15,27); path3 = [path4, path3]; path1 = [path1,path2(2:end)]; figure(2) p = plot(G,'Linewidth',3); hold on highlight(p,path1,'EdgeColor','m') highlight(p,path2,'EdgeColor','m') highlight(p,path3,'EdgeColor','m') lines(lines==0)=inf; %將lines=0的數全部替換為無強大 hold offfor i=1:nfor j=1:nif(lines(i,j)==0)lines(i,j)=inf;endend end for i=1:nlines(i,i)=0; end pb(1:length(lines))=0;pb(temp)=1; %求出最短路徑的點為1,未求出的為0 destination(1:length(lines))=0; path(1:length(lines))=0;%存放各點最短路徑的上一點標號 while sum(pb)<n %判斷每一點是否都已找到最短路徑tb=find(pb==0);%找到還未找到最短路徑的點fb=find(pb); %找出已找到最短路徑的點min=inf;for i=1:length(fb)for j=1:length(tb)plus=destination(fb(i))+lines(fb(i),tb(j)); %比較已確定的點與其相鄰未確定點的距離if((destination(fb(i))+lines(fb(i),tb(j)))<min)min=destination(fb(i))+lines(fb(i),tb(j));lastpoint=fb(i);newpoint=tb(j);endendenddestination(newpoint)=min; %找出起點與其它點的距離pb(newpoint)=1;path(newpoint)=lastpoint; %找出終點前的連接點 end fprintf('起點1到各點之間的距離:') disp(destination) path; fprintf('1到12的路線:') disp(path1) fprintf('12到27的路線:') disp(path3)%% 繪制地圖及目標下最優路線 figure(3) Im=imread('map1.png'); imshow(Im) hold on for count_3 = 1:length(map1_position)plot(map1_position(count_3,2),map1_position(count_3,3),'.','Markersize',25); end for count_4 = 1:length(path1)if count_4 + 1 <= length(path1)dotx2 = [map1_position(path1(count_4),2),map1_position(path1(count_4+1),2)]; doty2 = [map1_position(path1(count_4),3),map1_position(path1(count_4+1),3)]; plot(dotx2,doty2,'r','Linewidth',1.5)endif count_4 + 1 <= length(path3)dotx3 = [map1_position(path3(count_4),2),map1_position(path3(count_4+1),2)]; doty3 = [map1_position(path3(count_4),3),map1_position(path3(count_4+1),3)]; plot(dotx3,doty3,'r','Linewidth',1.5)end end hold off
完整工程文件下載鏈接:
(支撐材料可直接運行)
MATLAB輕松繪制地圖路線——已知及未知坐標下的處理方法(2)
總結
以上是生活随笔為你收集整理的MATLAB轻松绘制地图路线——已知及未知坐标下的处理方法(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL数据库讲解(三)
- 下一篇: R实现一次性合并多个数据框