现在还是只能分开运行,还在研究怎么合在一起
Xs = input('请输入起点横坐标XIn X0= ');
Ys = input('请输入起点纵坐标YIn Y0= ');
Xe = input('请输入终点横坐标XIn Xe= ');
Ye = input('请输入终点纵坐标YIn Ye= ');
h= input('输入步长\nh= ');
x=[Xs,Xe];
y=[Ys,Ye];
clc;
hold off;
plot(x,y);
hold on;
Xlength=Xe-Xs;
Ylength=Ye-Ys;
NXY= (abs(Xlength)+abs(Ylength))/h;
Fm=0;
X(1)=Xs;
Y(1)=Ys;
xlabel('X');
ylabel('Y');
title(['逐点比较法第一象限直线插补']);
if((Xe-Xs)>0&(Ye-Ys)>0) XOY=1;
end
for(i=1:NXY)
switch XOY
case 1
if(Fm>=0)
X(i+1)=X(i)+h;
Y(i+1)=Y(i);
Fm=Fm-Ylength;
else
X(i+1)=X(i);
Y(i+1)=Y(i)+h;
Fm=Fm+Xlength;
end
text((X(i)+X(i+1))/2,(Y(i)+Y(i+1))/2,[num2str(i)]);
temp_x=[X(i),X(i+1)];
temp_y=[Y(i),Y(i+1)];
plot(temp_x,temp_y,'r-');
hold on;
pause(0.5);
end
end
hold off;
Xs = input('请输入起点横坐标XIn X0= ');
Ys = input('请输入起点纵坐标YIn Y0= ');
Xe = input('请输入终点横坐标XIn Xe= ');
Ye = input('请输入终点纵坐标YIn Ye= ');
h= input('输入步长\nh= ');
x=[Xs,Xe];
y=[Ys,Ye];
clc;
hold off;
plot(x,y);
%axis([0,20,0,20])
hold on; %在原图上继续画图
Xlength=Xs-Xe; %X轴 起点到终点长度
Ylength=Ye-Ys; %Y轴 起点到终点长度
NXY= (abs(Xlength)+abs(Ylength))/h; %进给总步数(abs函数取绝对值)
Fm=0;
X(1)=Xs;
Y(1)=Ys;
xlabel('X');
ylabel('Y');
if((Xe-Xs)<0&(Ye-Ys)>0) XOY=2; %判断第一象限并赋值
end
title(['逐点比较法第二象限直线插补']);
for(i=1:NXY) %步数小于总步数
switch XOY %分支语句
case 2
if(Fm<=0)
X(i+1)=X(i)-h;
Y(i+1)=Y(i); %变化后Y坐标
Fm=Fm+Ylength; %新偏差计算
else
X(i+1)=X(i);
Y(i+1)=Y(i)+h; %变化后Y坐标
Fm=Fm-Xlength; %新偏差计算
end
text((X(i)+X(i+1))/2,(Y(i)+Y(i+1))/2,[num2str(i)]);
temp_x=[X(i+1),X(i)];
temp_y=[Y(i),Y(i+1)];
plot(temp_x,temp_y,'r-');%由此点和前一点坐标组成的2个向量画线
hold on;
pause(0.5); %延时程序形参为每走一步所用时间 end
end
end
%plot(X,Y,'r-');%由此点和前一点坐标组成的2个向量画线
hold off;
Xs = input('请输入起点横坐标XIn X0= ');
Ys = input('请输入起点纵坐标YIn Y0= ');
Xe = input('请输入终点横坐标XIn Xe= ');
Ye = input('请输入终点纵坐标YIn Ye= ');
h= input('输入步长\nh= ');x=[Xs,Xe];
y=[Ys,Ye];
clc;
hold off;
plot(x,y);
hold on;
Xlength=Xe-Xs;
Ylength=Ye-Ys;
NXY= (abs(Xlength)+abs(Ylength))/h;
Fm=0;
X(1)=Xs;
Y(1)=Ys;
xlabel('X');
ylabel('Y');
title(['逐点比较法第三象限直线插补']);
if((Xe-Xs)<0&(Ye-Ys)<0) XOY=3;
end
for(i=1:NXY)
switch XOY
case 3
if(Fm<=0)
X(i+1)=X(i)-h;
Y(i+1)=Y(i);
Fm=Fm-Ylength;
else
X(i+1)=X(i);
Y(i+1)=Y(i)-h;
Fm=Fm+Xlength;
end
text((X(i)+X(i+1))/2,(Y(i)+Y(i+1))/2,[num2str(i)]);
temp_x=[X(i),X(i+1)];
temp_y=[Y(i),Y(i+1)];
plot(temp_x,temp_y,'r-');
hold on;
pause(0.5);
end
end
hold off;
Xs0 = input('请输入起点横坐标XIn X0= ');
Ys0 = input('请输入起点纵坐标YIn Y0= ');
Xe0 = input('请输入终点横坐标XIn Xe= ');
Ye0 = input('请输入终点纵坐标YIn Ye= ');
h= input('输入步长\nh= ');
x=[Xs,Xe];
y=[Ys,Ye];
clc;
hold off;
plot(x,y);
hold on;
Xlength=Xe-Xs;
Ylength=Ye-Ys;
NXY= (abs(Xlength)+abs(Ylength))/h;
Fm=0;
X(1)=Xs;
Y(1)=Ys;
xlabel('X');
ylabel('Y');
title(['逐点比较法第四象限直线插补']);
if((Xe-Xs)>0&(Ye-Ys)<0) XOY=4;
end
for(i=1:NXY)
switch XOY
case 4
if(Fm<=0)
X(i+1)=X(i)+h;
Y(i+1)=Y(i);
Fm=Fm-Ylength;
else
X(i+1)=X(i);
Y(i+1)=Y(i)-h;
Fm=Fm-Xlength;
end
text((X(i)+X(i+1))/2,(Y(i)+Y(i+1))/2,[num2str(i)]);
temp_x=[X(i),X(i+1)];
temp_y=[Y(i),Y(i+1)];
plot(temp_x,temp_y,'r-');
hold on;
pause(0.5);
end
end
hold off;