日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MATLAB轻松绘制地图路线——已知及未知坐标下的处理方法(1)

發布時間:2023/12/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MATLAB轻松绘制地图路线——已知及未知坐标下的处理方法(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 已知坐標的情況
    • 未知坐標的情況
    • 完整工程文件下載鏈接:

要想繪制地圖路線, 最基本的要素就是 各點的坐標,有了坐標,還要知道哪個點和哪個點相連,最后將各點相連即可;
但有時候我們有的往往只是一張地圖,這樣其實也不難,MATLAB中有一個 [x,y]=ginput() 函數,將地圖以圖片的形式導入,鼠標點擊我們要標記的點,即可返回點坐標,這樣就將未知坐標的情況轉化成了第一種情況。

已知坐標的情況

假如已給我們想要的各節點的起點坐標和終點坐標(即橫縱坐標),我們首先將Excel表導入進MATLAB,然后以列矩陣的形式來保存數據,并處理。

這里將Excel中的數據先拆成了三個工作區數據data1,data2,data3,并導入數組x1,x2,x3,分別用來存節點橫坐標、節點縱坐標,要相連的節點和節點標號。

clc,clear,close all load data1 load data2 load data3 x1=[data1(:,1),data1(:,2),data1(:,3)]; %節點橫坐標,第三列未用到 x2=[data2(:,1),data2(:,2)]; %要相連的節點 x3=[data3(:,1)]; %節點標號 xx=[0;0]; %變量類型初始化 xy=[0;0]; backcolor=[0.5,0.5,0.5]; %設置背景色 fontcolor=[1,1,1]; %設置字體顏色 for i=1:92 %獲取坐標,以A區為例,92個節點xx(i)=x1(i,2); %導入橫坐標xy(i)=x1(i,3); %導入縱坐標count=1:20;k=find(x3,i);if i<=20plot(xx(i),xy(i),'^m','markerface','g','markersize',20)elseif i>20plot(xx(i),xy(i),'.m','markerface','k','markersize',25)endif k~=0plot(xx(k),xy(k),'xr','markerface','r','markersize',22)endhold ontext(xx(i),xy(i),num2str(x1(i,1)),'color',fontcolor); end for j=1:length(data2(:,1)); %以循環的方式逐一連接所要連接的點dotx=[xx(data2(j,1)), xx(data2(j,2))]; %確定橫坐標doty=[xy(data2(j,1)), xy(data2(j,2))]; %確定縱坐標plot(dotx ,doty,'-c','Linewidth',1.5); %連線 end set(gca, 'color', backcolor); title('A區','Fontsize',14)


要想畫多個區的路線也是類似的操作,只需導入更多的節點數據:

clc,clear,close all load data4 load data5 load data3 load data6 load data7 x1=[data4(:,1),data4(:,2),data4(:,3)]; %節點標號,x坐標,y坐標 x2=[data5(:,1),data5(:,2)]; %指定節點路線 x3=data3(:,1); %出入A區的路口標號 x4=[data6(:,1),data6(:,2)]; %交巡警平臺編號,交巡警平臺位置標號 x5=data7(:,1); %出入市區的路口標號 xx=[0;0]; xy=[0;0]; backcolor=[0.2,0.2,0.2]; fontcolor=[1,1,1]; zonecolor=[1,0.5,0]; for i=1:length(data4(:,1)) %遍歷獲取坐標xx(i)=x1(i,2); xy(i)=x1(i,3);k=find(x3,i);k1=find(x4,i);k2=find(x5,i);%描點 plot(xx(i),xy(i),'.m','markerface','k','markersize',25) plot(xx(k),xy(k),'*r','markerface','r','markersize',16) %出入A區的路口標號plot(xx(k1),xy(k1),'^m','markerface','g','markersize',8) %交巡警平臺編號,交巡警平臺位置標號plot(xx(k2),xy(k2),'xy','markerface','r','markersize',22) %出入市區的路口標號hold ontext(xx(i),xy(i),num2str(x1(i,1)),'color',fontcolor,'fontsize',6);text(369,388,'A','color',zonecolor,'fontsize',25);text(170,90, 'B','color',zonecolor,'fontsize',25);text(340,450,'C','color',zonecolor,'fontsize',25);text(85,380, 'D','color',zonecolor,'fontsize',25);text(140,210,'E','color',zonecolor,'fontsize',25);text(370,188,'F','color',zonecolor,'fontsize',25); end for j=1:length(data5(:,1)) %連線dotx=[xx(data5(j,1)), xx(data5(j,2))];doty=[xy(data5(j,1)), xy(data5(j,2))];if data5(j,1)<=92 %按各區顏色分配plot(dotx ,doty,'-c','Linewidth',1.5)elseif (166<=data5(j,1))&&(data5(j,1)<=319)|| (372<=data5(j,1))&&(data5(j,1)<=474)plot(dotx ,doty,'-b','Linewidth',1.5)elseif (320<=data5(j,1))&&(data5(j,1)<=371)||data5(j,1)>=475plot(dotx ,doty,'-r','Linewidth',1.5)elseplot(dotx ,doty,'-c','Linewidth',1.5)end end set(gca, 'color', backcolor); title('全市','Fontsize',14)

未知坐標的情況

這里我們要用到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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。