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

Hello
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.

about | contact | feed

Comments (feed)
Dave on Cubeicals wine rack: I ended up cutting a...
Harry on Cubeicals wine rack: So Dave, how did you...
Emily on Constant vs. average speed: I can give you sun...
Dave on Accents that annoy me: I'm pretty much saying I...
Wendy on Accents that annoy me: Are you passively aggressively saying...
JSW on Accents that annoy me: KInda like I can't tell...
Dave on Accents that annoy me: Understood, but my untrained ears...
Dave on Constant vs. average speed: I don't think so.  There...
JSW on Accents that annoy me: I realize with you being...
Emily on Constant vs. average speed: You don't think the beaches...