ddhr.org
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);

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

Download

Linked:  Code

About (more)
Hi, my name is Dave Hosier, and this website is where I write my unfounded opinions on trivial matters. Feel free to look around, but please refrain from reading anything.

contact | code | pictures | feed

Discussion (feed)
Rus on Internet echo chamber: The latter also subjects the...
Dave on Internet echo chamber: I read your comments, so...
Rus on Internet echo chamber: If he wasn't feeling so...
Rus on Where grass grows: Isn't there one house up...
Wendy on Where grass grows: Ehem...I suggested a garden in...
Rus on Favre watch: That last line is brilliant.
Dave on Where grass grows: Good point.  It wouldn't take...
Rus on Where grass grows: I feel your pain, but...
Viv on WinXP .mov thumbnails: Thank you, this worked for...
Dave on Belief and behavior: I would say he believed...