课后习题1

给定平面上的一个有向线段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  %添加了一条直径线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值