| ¶ Code - linepoints | Wednesday, Feb 4, 2009 4:29 pm |
This is a simple Matlab function that calculates points along a line between two endpoints.
function output = linepoints(x1,y1,z1,x2,y2,z2,n)
%LINEPOINTS Calculate points along a line.
% Calculate n points along a line from (x1,y1) to (x2,y2)
% or (x1,y1,z1) to (x2,y2,z2). Results can be used to
% plot the line (as an alternative to the LINE() function)
% or for interpolation and/or line of sight analysis.
%
% Version: 0.1
%
% Example:
% points = linepoints(0,0,3,5,10);
% plot(points(:,1),points(:,2),'rx'); hold on
% line([0 3],[0 5],'marker','o','markeredgecolor','b')
% points = linepoints(0,0,0,3,5,8,100);
%LINEPOINTS Calculate points along a line.
% Calculate n points along a line from (x1,y1) to (x2,y2)
% or (x1,y1,z1) to (x2,y2,z2). Results can be used to
% plot the line (as an alternative to the LINE() function)
% or for interpolation and/or line of sight analysis.
%
% Version: 0.1
%
% Example:
% points = linepoints(0,0,3,5,10);
% plot(points(:,1),points(:,2),'rx'); hold on
% line([0 3],[0 5],'marker','o','markeredgecolor','b')
% points = linepoints(0,0,0,3,5,8,100);
if nargin == 5
n = y2;
y2 = x2;
x2 = z1;
xs = linspace(x1,x2,n);
xyangle = atand((y2-y1)/(x2-x1));
ys = (xs-x1)*tand(xyangle)+y1;
output = [xs' ys'];
elseif nargin == 7
xs = linspace(x1,x2,n);
xyangle = atand((y2-y1)/(x2-x1));
ys = (xs-x1)*tand(xyangle)+y1;
xyzangle = atand((z2-z1)/sqrt((x2-x1)^2+(y2-y1)^2));
zs = sqrt((xs-x1).^2+(ys-y1).^2).*tand(xyzangle)+z1;
output = [xs' ys' zs'];
else
error('Wrong number of input parameters.');
end
Linked: Code