matlab已知圆心和半径怎么作圆,matlab根据圆心和半径画圆三种方式

该博客介绍了在MATLAB中根据已知圆心和半径绘制圆的三种方法:使用viscircle、rectangle和自定义function。通过示例代码和时间对比,展示了每种方法的效率和适用场景,并提供了散点图和物流园区覆盖的绘制。此外,还提到了数据来源和相关参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

% 已知圆心和半径画圆:参考:https://blog.youkuaiyun.com/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:

02db8894deae0fd26425b8658f7470d1.png

figure2:

277c40a38610e5735df9b318bb0edb2f.png

figure3:

307ad2d0cee487e96783fe91462e0c01.png

赛题具体地图:

393e96b3dfecedd28bcdf6f614fe1e22.png

参考资料:

1.https://blog.youkuaiyun.com/ZLK961543260/article/details/70216089

2.数据来源为2017年研究生数学建模竞赛F题赛题数据

来源:https://www.cnblogs.com/feynmania/p/12970209.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值