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]