用MATLAB照书上代码打后提示:"错误: 此上下文中不允许函数定义."
functionsegs=calLength(P1,P2)segs.length=[];segs.index_x=[];segs.index_y=[];segs.inde...
function segs=calLength(P1,P2)
segs.length=[];
segs.index_x =[];
segs.index_y =[];
segs.index =[];
P1_x = P1(1);
P1_y = P1(2);
P2_x = P2(1);
P2_y = P2(2);
xmin = min(P1_x,P2_x);
ymin = min(P1_y,P2_y);
xmax = max(P1_x,P2_x);
ymax = max(P1_y,P2_y);
if(P1_x == P2_x) && (round(P1_x) == P1_x)...
|| (P1_y == P2_y) && (round(P1_y) == P1_y)
return;
end
pos =reshape(1:100,10,10);
if(P1_x == P2_x) && (round(P1_x) ~= P1_x)
SP = unique([ymin,ymax,ceil(ymin):floor(ymax)]);
for t =1:size(SP,2)-1
segs(t).length= SP(t+1)-SP(t);
segs(t).index_x =ceil(P1_x);
segs(t).index_y = max(ceil(SP(t+1)),ceil(SP(t)));
segs(t).index = pos(segs(t).index_x, segs(t).index_y);
end
end
if(P1_x ~= P2_x)
K = polyfit([P1_x,P2_x],[P1_y,P2_y],1);
xpx =[];
xpy =[];
for i=ceil(xmin):floor(xmax)
xpx(i-ceil(xmin)+1) =i;
xpy(i-ceil(xmin)+1) = K(1)*i+K(2);
end
ypx =[];
ypy =[];
for j=ceil(ymin):floor(ymax)
ypy(j-ceil(ymin)+1) =j;
syms x;
ypx(j-ceil(ymin)+1) = double(solve(K(1)*x + K(2) -j, x));
end
SP = unique([P1_x,P2_x,xpx,ypx;P1_y,P2_y,xpy,ypy]','rows');
L = @(x)sqrt((SP(x+1,1) - SP(x,1)).^2+ (SP(x+1,2) - SP(x,2)).^2);
for t =1:size(SP,1)-1
segs(t).length= L(t);
segs(t).index_x = max(ceil(SP(t+1,1)),ceil(SP(t,1)));
segs(t).index_y = max(ceil(SP(t+1,2)),ceil(SP(t,2)));
segs(t).index = pos(segs(t).index_x, segs(t).index_y);
end
end
n =[];
for i=1:size(segs,2)
if segs(i).length<eps
n =[n i];
end
end
segs(n) =[];
end
clear;
clc;
clf;
x =linspace(0,10,11);
y =linspace(0,10,11);
[X,Y]=meshgrid(x,y);
line(X,Y,'color','b');
line(X',Y','color','b');
axis equal;
axis([0 10 0 10]);
set(gca,'xtick',0:10);
gridindex =reshape(1:100,10,10)';
numposx = 0.5*(X(1:end-1,2:end)+X(1:end-1,1:end-1))-0.1;
numposy = 0.5*(Y(2:end,1:end-1)+Y(1:end-1,1:end-1));
for i = 1 : 10
for j = 1 : 10
text(numposx(i,j),numposy(i,j),num2str(gridindex(i,j)));
end
end
P1 = input('P1=');
P2 = input('P2=');
segs = calLength(P1,P2);
line([P1(1) P2(1)],[P1(2) P2(2)],'color','r');
display('所经过的网格序号\长度分别为:');
for i=1:size(segs,2)
display(['序号: ',num2str(segs(i).index)]);
display(['长度: ',num2str(segs(i).length)]);
end 展开
segs.length=[];
segs.index_x =[];
segs.index_y =[];
segs.index =[];
P1_x = P1(1);
P1_y = P1(2);
P2_x = P2(1);
P2_y = P2(2);
xmin = min(P1_x,P2_x);
ymin = min(P1_y,P2_y);
xmax = max(P1_x,P2_x);
ymax = max(P1_y,P2_y);
if(P1_x == P2_x) && (round(P1_x) == P1_x)...
|| (P1_y == P2_y) && (round(P1_y) == P1_y)
return;
end
pos =reshape(1:100,10,10);
if(P1_x == P2_x) && (round(P1_x) ~= P1_x)
SP = unique([ymin,ymax,ceil(ymin):floor(ymax)]);
for t =1:size(SP,2)-1
segs(t).length= SP(t+1)-SP(t);
segs(t).index_x =ceil(P1_x);
segs(t).index_y = max(ceil(SP(t+1)),ceil(SP(t)));
segs(t).index = pos(segs(t).index_x, segs(t).index_y);
end
end
if(P1_x ~= P2_x)
K = polyfit([P1_x,P2_x],[P1_y,P2_y],1);
xpx =[];
xpy =[];
for i=ceil(xmin):floor(xmax)
xpx(i-ceil(xmin)+1) =i;
xpy(i-ceil(xmin)+1) = K(1)*i+K(2);
end
ypx =[];
ypy =[];
for j=ceil(ymin):floor(ymax)
ypy(j-ceil(ymin)+1) =j;
syms x;
ypx(j-ceil(ymin)+1) = double(solve(K(1)*x + K(2) -j, x));
end
SP = unique([P1_x,P2_x,xpx,ypx;P1_y,P2_y,xpy,ypy]','rows');
L = @(x)sqrt((SP(x+1,1) - SP(x,1)).^2+ (SP(x+1,2) - SP(x,2)).^2);
for t =1:size(SP,1)-1
segs(t).length= L(t);
segs(t).index_x = max(ceil(SP(t+1,1)),ceil(SP(t,1)));
segs(t).index_y = max(ceil(SP(t+1,2)),ceil(SP(t,2)));
segs(t).index = pos(segs(t).index_x, segs(t).index_y);
end
end
n =[];
for i=1:size(segs,2)
if segs(i).length<eps
n =[n i];
end
end
segs(n) =[];
end
clear;
clc;
clf;
x =linspace(0,10,11);
y =linspace(0,10,11);
[X,Y]=meshgrid(x,y);
line(X,Y,'color','b');
line(X',Y','color','b');
axis equal;
axis([0 10 0 10]);
set(gca,'xtick',0:10);
gridindex =reshape(1:100,10,10)';
numposx = 0.5*(X(1:end-1,2:end)+X(1:end-1,1:end-1))-0.1;
numposy = 0.5*(Y(2:end,1:end-1)+Y(1:end-1,1:end-1));
for i = 1 : 10
for j = 1 : 10
text(numposx(i,j),numposy(i,j),num2str(gridindex(i,j)));
end
end
P1 = input('P1=');
P2 = input('P2=');
segs = calLength(P1,P2);
line([P1(1) P2(1)],[P1(2) P2(2)],'color','r');
display('所经过的网格序号\长度分别为:');
for i=1:size(segs,2)
display(['序号: ',num2str(segs(i).index)]);
display(['长度: ',num2str(segs(i).length)]);
end 展开
展开全部
其实代码是正确的,问题在于代码包括两个文件,一个是function文件“calLength.m”,一个是script文件(可以命名为“tmp_01.m”);并且执行tmp_01.m时,输入也要满足特定格式。具体如下:
“calLength.m”的内容是:
function segs=calLength(P1,P2)
segs.length=[];
segs.index_x =[];
segs.index_y =[];
segs.index =[];
P1_x = P1(1);
P1_y 枣差陵= P1(2);
P2_x = P2(1);
P2_y = P2(2);
xmin = min(P1_x,P2_x);
ymin = min(P1_y,P2_y);
xmax = max(P1_x,P2_x);
ymax 凳戚= max(P1_y,P2_y);
if(P1_x == P2_x) && (round(P1_x) == P1_x)...
|| (P1_y == P2_y) && (round(P1_y) == P1_y)
return;
end
pos =reshape(1:100,10,10);
if(P1_x == P2_x) && (round(P1_x) ~= P1_x)
SP = unique([ymin,ymax,ceil(ymin):floor(ymax)]);
for t =1:size(SP,2)-1
segs(t).length= SP(t+1)-SP(t);
segs(t).index_x =ceil(P1_x);
segs(t).index_y = max(ceil(SP(t+1)),ceil(SP(t)));
segs(t).index = pos(segs(t).index_x, segs(t).index_y);
end
end
if(P1_x ~= P2_x)
K = polyfit([P1_x,P2_x],[P1_y,P2_y],1);
xpx =[];
xpy =[];
for i=ceil(xmin):floor(xmax)
xpx(i-ceil(xmin)+1) =i;
xpy(i-ceil(xmin)+1) = K(1)*i+K(2);
end
ypx =[];
ypy =[];
for j=ceil(ymin):floor(ymax)
ypy(j-ceil(ymin)+1) =j;
syms x;
ypx(j-ceil(ymin)+1) = double(solve(K(1)*x + K(2) -j, x));
end
SP = unique([P1_x,P2_x,xpx,ypx;P1_y,P2_y,xpy,ypy]','rows');
L = @(x)sqrt((SP(x+1,1) - SP(x,1)).^2+ (SP(x+1,2) - SP(x,2)).^2);
for t =1:size(SP,1)-1
segs(t).length= L(t);
segs(t).index_x = max(ceil(SP(t+1,1)),ceil(SP(t,1)));
segs(t).index_y = max(ceil(SP(t+1,2)),ceil(SP(t,2)));
segs(t).index = pos(segs(t).index_x, segs(t).index_y);
end
end
n =[];
for i=1:size(segs,2)
if segs(i).length<eps
n =[n i];
end
end
segs(n) =[];
end
如图所示:
“tmp_01.m”的内庆携容是:
clear;
clc;
clf;
x =linspace(0,10,11);
y =linspace(0,10,11);
[X,Y]=meshgrid(x,y);
line(X,Y,'color','b');
line(X',Y','color','b');
axis equal;
axis([0 10 0 10]);
set(gca,'xtick',0:10);
gridindex =reshape(1:100,10,10)';
numposx = 0.5*(X(1:end-1,2:end)+X(1:end-1,1:end-1))-0.1;
numposy = 0.5*(Y(2:end,1:end-1)+Y(1:end-1,1:end-1));
for i = 1 : 10
for j = 1 : 10
text(numposx(i,j),numposy(i,j),num2str(gridindex(i,j)));
end
end
P1 = input('P1=');
P2 = input('P2=');
segs = calLength(P1,P2);
line([P1(1) P2(1)],[P1(2) P2(2)],'color','r');
display('所经过的网格序号\长度分别为:');
for i=1:size(segs,2)
display(['序号: ',num2str(segs(i).index)]);
display(['长度: ',num2str(segs(i).length)]);
end
如图所示:
按F5执行tmp_01.m,命名行窗口,出现P1待输入的提示,如下图所示:
作为例子,输入[1,2],表示坐标为[1,2]的点,按enter键结束输入。
可以发现,有提示输入P2,如下图所示:
作为例子,输入[6,8],按enter键结束输入。就可以正确的得到结果。
命名行窗口:
图形窗口:
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询