给定平面上的一个有向线段AB,起点为A(x1,y1),终点为B(x2,y2),绘制以AB的中点为圆心,AB的半长为半径的左侧半圆和右侧半圆。
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) %x1=x2,这条直线垂直于x轴,倾斜角为90度
thet=pi*0.5;
else
slope=(l(2,2)-l(1,2))/(l(2,1)-l(1,1));%slope 斜率
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 %添加了一条直径线