matlab根據圓心和半徑畫圓三種方式


% 已知圓心和半徑畫圓:參考:https://blog.csdn.net/ZLK961543260/article/details/70216089
% 對比三種畫圓方法,運算時長對比如下
% viscircle:0.22;比較快
% rectangle:0.21;比較快
% function:0.39:慢

[Type Sheet Format]=xlsfinfo('現狀OD數據及其他數據.xls');
OD=xlsread('現狀OD數據及其他數據.xls',Sheet{1});
center_area=xlsread('現狀OD數據及其他數據.xls',Sheet{2});

x_pos=center_area(:,4);   %數據第四列為x坐標(米)
y_pos=center_area(:,5);   %數據第五列為y坐標(米)
area=center_area(:,3);    %數據第3列為面積(平方米):注意與x,y的坐標對應
r_all=sqrt(area/pi);      %計算節點覆蓋半徑,為后文畫圓做准備
nanbool=isnan(r_all);     %數據1到4行為四個物流園區,通過r為nan,提取出四個物流園區
first_node_num=length(find(nanbool==1)); %統計物流園區個數
r_index=find(nanbool==0,1);              %節點開始的行數index
r=r_all(r_index:end);                    %r存儲節點的覆蓋半徑
jam_coef=center_area(r_index:end,6);     %數據第六列為交通擁堵系數,只有節點才有,具體在散點圖中的標注用text實現,
% 具體標注可參考https://zhidao.baidu.com/question/924100748051179779.html
% 散點圖顏色可參考:https://zhidao.baidu.com/question/521200141403285445.html?qbl=relate_question_1&word=matlabscatter%B1%EA%D7%A2
% 具體標注和顏色還未完善

% 方法一:viscircle
%用viscircle畫節點的覆蓋范圍,用scatter畫物流園區和節點
x_2_pos=x_pos(r_index:end);     %節點x
y_2_pos=y_pos(r_index:end);     %節點y
centers=[x_2_pos,y_2_pos];      %節點圓心位置
tic
fig1=figure
colors = {'b','r','g','y','k'};
%定義colors,后文畫圖方便,也可以只是掉,直接在后文加入'k','g'等,具體可查詢matlab畫圖顏色標記表示
viscircles(centers,r,'color',colors{1});    %matlab自帶函數:已知圓心和半徑畫圓
hold on
scatter(x_2_pos,y_2_pos,'filled');
%已知x,y坐標,畫散點圖命令scatter(x,y),filled表示圓是實心填充
hold on                                     %畫多個圓時用hold on命令
scatter(x_pos(1:4),y_pos(1:4),'filled');    %數字4指的是物流園區的個數,均可以替換成first_node_num
%scatter(x_pos(1:first_node_num),y_pos(1:first_node_num),'filled'); 
toc

% % 方法二:rectangle
% % 用矩形函數rectangle 畫圓,rectangle函數一次只能畫一個矩形/圓;需要用到循環,與viscircle比較計算時長,tic,toc
tic
fig2=figure
%矩形函數畫多個圓只能用for命令進行循環,用矩陣讀取會發生錯誤
for i=1:length(r)
rectangle('Position',[x_2_pos(i)-r(i),y_2_pos(i)-r(i),2*r(i),2*r(i)],'Curvature',[1,1],'linewidth',1,'EdgeColor','b')  %rectangle('position',[x_pos,y_pos,length,width])
end  %[1,1]表示構造圓/橢圓
hold on
scatter(x_2_pos,y_2_pos,'filled');
hold on
scatter(x_pos(1:4),y_pos(1:4),'filled');
%scatter(x_pos(1:first_node_num),y_pos(1:first_node_num),'filled'); 
toc

% 方法三:構建function 
tic
fig3=figure
for j=1:length(r)
x=x_2_pos(j);
y=y_2_pos(j);
r1=r(j);
% function [] = plot1( x,y,r1 )
theta=0:0.1:2*pi;
Circle1=x+r1*cos(theta);
Circle2=y+r1*sin(theta);
c=[123,14,52]; %color:BGR? RBG?help
plot(Circle1,Circle2,'c','linewidth',1);
hold on
end
scatter(x_2_pos,y_2_pos,'filled');
hold on
scatter(x_pos(1:4),y_pos(1:4),'filled');
toc

繪圖結果:

figure1:

figure2:

figure3:

賽題具體地圖:

 

 

參考資料:

1.https://blog.csdn.net/ZLK961543260/article/details/70216089

2.數據來源為2017年研究生數學建模競賽F題賽題數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM