这里是一个假设
dx=1
,
dy=1
,合并任意x和y步长应该不难
% //I am assuming here that you know how to get your Z
z=peaks(60);
% //start point
spoint=[30,30];
% //user given angle
angle=pi/4;
% // distance you want
distance=10;
%// this is the furthes the poitn can be
endpoint=[spoint(1)+distance*cos(angle) spoint(2)+distance*sin(angle)];
%// we will need to discretize, so choose your "accuracy"
npoints=100;
%//compute the path integral over the line defined by startpoitn and endpoint
[cx,cy,cz]=improfile(z,[spoint(1) endpoint(1)],[spoint(2) endpoint(2)],npoints);
% // this computes distances between adjacent points and then computes the cumulative sum
dcx=diff(cx);
dcy=diff(cy);
dcz=diff(cz);
totaldist=cumsum(sqrt(dcx.^2+dcy.^2+dcz.^2));
%// here it is! the last index before it gets to the desired distance
ind=find(totaldist<distance,1,'last');
可视化代码
imagesc(z);axis xy;colormap gray
hold on;
plot(spoint(1),spoint(2),'r.','markersize',10)
plot(endpoint(1),endpoint(2),'r*','markersize',5)
plot([spoint(1) endpoint(1)],[spoint(2) endpoint(2)],'b')
plot(cx(ind),cx(ind),'g*','markersize',10)