matlab画左右半圆

matlab画左右半圆

题目描述

给定一个平面上有向线段l,其起点坐标为(x1,y1),终点坐标为(x2,y2),绘制以l的中点为圆心,l的半长为半径的左侧半圆和右侧半圆。

思路

没有根据三角形面积的正负判断左右,‘线段’的逆时针方向就是左,转过pi度后就是右。使用参数方程的方法来画半圆。大概思路就是这样,看代码吧。

function circle=plot_arc()
l=input('Enter the start and end coordinates of the line segment:[x1,y1;x2,y2;]\n:');
if l(1,2)>l(2,2)
    l([1,2],:)=l([2,1],:);
end
if l(1,2)==l(2,2)&&l(1,1)>l(2,1)
    l([1,2],:)=l([2,1],:);
end
x0=(l(1,1)+l(2,1))/2;
y0=(l(1,2)+l(2,2))/2;%圆心坐标
r=1.0/2*((l(2,1)-l(1,1))^2+(l(2,2)-l(1,2))^2)^0.5;%圆的半径
if l(1,1)==l(2,1)
    thet=pi*0.5;
else
    slope=(l(2,2)-l(1,2))/(l(2,1)-l(1,1));
    if slope>=0
        thet=atan(slope);
    elseif slope<0
        thet=atan(slope)+pi;
    end
end
%左半部分圆
alpha=thet:pi*1.0/40:thet+pi;
x1=x0+r*cos(alpha);
y1=y0+r*sin(alpha);
%右半部分圆
beta=thet+pi:pi*1.0/40:thet+2*pi;
x2=x0+r*cos(beta);
y2=y0+r*sin(beta);
plot(x1,y1,x2,y2),
text(x0+r*cos(thet+pi/2),y0+r*sin(thet+pi/2),'\leftarrow left half circle'),
text(x0+r*cos(thet+pi*1.5),y0+r*sin(thet+pi*1.5),'\rightarrow right half circle'),
line([l(1,1) l(2,1)],[l(1,2) l(2,2)],'Color','black'),grid on,axis equal

运行的结果

数据1
[1,1;6,6]
在这里插入图片描述
数据2
[1,5;6,-30]
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值