Home > matGeom > geom2d > createLine.m

createLine

PURPOSE ^

CREATELINE Create a straight line from 2 points, or from other inputs.

SYNOPSIS ^

function line = createLine(varargin)

DESCRIPTION ^

CREATELINE Create a straight line from 2 points, or from other inputs.

   Line is represented in a parametric form : [x0 y0 dx dy]
   x = x0 + t*dx
   y = y0 + t*dy;


   L = createLine(p1, p2);
   Returns the line going through the two given points.
   
   L = createLine(x0, y0, dx, dy);
   Returns the line going through point (x0, y0) and with direction
   vector(dx, dy).

   L = createLine(LINE);
   where LINE is an array of 4 values, creates the line going through the
   point (LINE(1) LINE(2)), and with direction given by vector (LINE(3)
   LINE(4)). 
   
   L = createLine(THETA);
   Create a polar line originated at (0,0) and with angle THETA.

   L = createLine(p, THETA);
   Create a polar line originated at p and with angle THETA.

   L = createLine(RHO, THETA);
   Create a polar line with normal theta, and with min distance to origin
   equal to rho. rho can be negative, in this case, the line is the same
   as with CREATELINE(-rho, theta+pi), but the orientation is different.


   Note: in all cases, parameters can be vertical arrays of the same
   dimension. The result is then an array of lines, of dimensions [N*4].


   See also:
   lines2d, createEdge, createRay

   ---------
   author : David Legland
   INRA - TPV URPOI - BIA IMASTE
   created the 31/10/2003.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function line = createLine(varargin)
0002 %CREATELINE Create a straight line from 2 points, or from other inputs.
0003 %
0004 %   Line is represented in a parametric form : [x0 y0 dx dy]
0005 %   x = x0 + t*dx
0006 %   y = y0 + t*dy;
0007 %
0008 %
0009 %   L = createLine(p1, p2);
0010 %   Returns the line going through the two given points.
0011 %
0012 %   L = createLine(x0, y0, dx, dy);
0013 %   Returns the line going through point (x0, y0) and with direction
0014 %   vector(dx, dy).
0015 %
0016 %   L = createLine(LINE);
0017 %   where LINE is an array of 4 values, creates the line going through the
0018 %   point (LINE(1) LINE(2)), and with direction given by vector (LINE(3)
0019 %   LINE(4)).
0020 %
0021 %   L = createLine(THETA);
0022 %   Create a polar line originated at (0,0) and with angle THETA.
0023 %
0024 %   L = createLine(p, THETA);
0025 %   Create a polar line originated at p and with angle THETA.
0026 %
0027 %   L = createLine(RHO, THETA);
0028 %   Create a polar line with normal theta, and with min distance to origin
0029 %   equal to rho. rho can be negative, in this case, the line is the same
0030 %   as with CREATELINE(-rho, theta+pi), but the orientation is different.
0031 %
0032 %
0033 %   Note: in all cases, parameters can be vertical arrays of the same
0034 %   dimension. The result is then an array of lines, of dimensions [N*4].
0035 %
0036 %
0037 %   See also:
0038 %   lines2d, createEdge, createRay
0039 %
0040 %   ---------
0041 %   author : David Legland
0042 %   INRA - TPV URPOI - BIA IMASTE
0043 %   created the 31/10/2003.
0044 %
0045 
0046 %   HISTORY :
0047 %   18/02/2004 : add more possibilities to create lines (4 parameters,
0048 %      all param in a single tab, and point + dx + dy.
0049 %      Also add support for creation of arrays of lines.
0050 
0051 %   NOTE : A line can also be represented with a 1*5 array :
0052 %   [x0 y0 dx dy t].
0053 %   whith 't' being one of the following :
0054 %   - t=0 : line is a singleton (x0,y0)
0055 %   - t=1 : line is an edge segment, between points (x0,y0) and (x0+dx,
0056 %   y0+dy).
0057 %   - t=Inf : line is a Ray, originated from (x0,y0) and going to infinity
0058 %   in the direction(dx,dy).
0059 %   - t=-Inf : line is a Ray, originated from (x0,y0) and going to infinity
0060 %   in the direction(-dx,-dy).
0061 %   - t=NaN : line is a real straight line, and contains all points
0062 %   verifying the above equation.
0063 %   This seems us a convenient way to represent uniformly all kind of lines
0064 %   (including edges, rays, and even point).
0065 %
0066 
0067 %   NOTE2 : Any line object can be represented using a 1x6 array :
0068 %   [x0 y0 dx dy t0 t1]
0069 %   the first 4 parameters define the supporting line,
0070 %   t0 represent the position of the first point on the line,
0071 %   and t1 the position of the last point.
0072 %   * for edges : t0 = 0, and t1=1
0073 %   * for straight lines : t0 = -inf, t1=inf
0074 %   * for rays : t0=0, t1=inf (or t0=-inf,t1=0 for inverted ray).
0075 %   I propose to call these objects 'lineArc'
0076 
0077 if length(varargin)==1
0078     % Only one input parameter. It can be :
0079     % - line angle
0080     % - array of four parameters
0081     % TODO : add control for arrays of lines.
0082     var = varargin{1};
0083     
0084     if size(var, 2)==4
0085         % 4 parameters of the line in a single array.
0086         line = var;
0087     elseif size(var, 2)==1
0088         % 1 parameter : angle of the line, going through origin.
0089         line = [zeros(size(var)) zeros(size(var)) cos(var) sin(var)];
0090     else
0091         error('wrong number of dimension for arg1 : can be 1 or 4');
0092     end
0093     
0094 elseif length(varargin)==2    
0095     % 2 input parameters. They can be :
0096     % - line angle and signed distance to origin.
0097     % - 2 points, then 2 arrays of 1*2 double.
0098     v1 = varargin{1};
0099     v2 = varargin{2};
0100     if size(v1, 2)==2 && size(v2, 2)==1
0101         % first param is point, and second param is angle of line
0102         line = [v1(:,1), v1(:,2) cos(v2) sin(v2)];
0103     elseif size(v1, 2)==1
0104         % first param is angle of line, and second param is signed distance
0105         % to origin.
0106         line = [v1.*cos(v2) v1.*sin(v2) -sin(v2) cos(v2)];
0107     elseif size(v1, 2)==3 || size(v2, 2)==3
0108         error('The 1st or 2nd input argument has 3 columns. You may want to try createLine3d.');
0109     else
0110         % first input parameter is first point, and second input is the
0111         % second point.
0112         line = [v1(:,1), v1(:,2), v2(:,1)-v1(:,1), v2(:,2)-v1(:,2)];    
0113     end
0114     
0115 elseif length(varargin)==3
0116     % 3 input parameters :
0117     % first one is a point belonging to the line,
0118     % second and third ones are direction vector of the line (dx and dy).
0119     p = varargin{1};
0120     line = [p(:,1) p(:,2) varargin{2} varargin{3}];
0121    
0122 elseif length(varargin)==4
0123     % 4 input parameters :
0124     % they are x0, y0 (point belongng to line) and dx, dy (direction vector
0125     % of the line).
0126     % All parameters should have the same size.
0127     line = [varargin{1} varargin{2} varargin{3} varargin{4}];
0128 else
0129     error('Wrong number of arguments in ''createLine'' ');
0130 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019