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 in radians.

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

   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

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 in radians.
0023 %
0024 %   L = createLine(p, THETA);
0025 %   Create a polar line originated at p and with angle THETA in radians.
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 % E-mail: david.legland@inrae.fr
0043 % Created: 2003-10-31
0044 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE
0045 
0046 %   NOTE : A line can also be represented with a 1*5 array :
0047 %   [x0 y0 dx dy t].
0048 %   whith 't' being one of the following :
0049 %   - t=0 : line is a singleton (x0,y0)
0050 %   - t=1 : line is an edge segment, between points (x0,y0) and (x0+dx,
0051 %   y0+dy).
0052 %   - t=Inf : line is a Ray, originated from (x0,y0) and going to infinity
0053 %   in the direction(dx,dy).
0054 %   - t=-Inf : line is a Ray, originated from (x0,y0) and going to infinity
0055 %   in the direction(-dx,-dy).
0056 %   - t=NaN : line is a real straight line, and contains all points
0057 %   verifying the above equation.
0058 %   This seems us a convenient way to represent uniformly all kind of lines
0059 %   (including edges, rays, and even point).
0060 %
0061 
0062 %   NOTE2 : Any line object can be represented using a 1x6 array :
0063 %   [x0 y0 dx dy t0 t1]
0064 %   the first 4 parameters define the supporting line,
0065 %   t0 represent the position of the first point on the line,
0066 %   and t1 the position of the last point.
0067 %   * for edges : t0 = 0, and t1=1
0068 %   * for straight lines : t0 = -inf, t1=inf
0069 %   * for rays : t0=0, t1=inf (or t0=-inf,t1=0 for inverted ray).
0070 %   I propose to call these objects 'lineArc'
0071 
0072 if isscalar(varargin)
0073     % Only one input parameter. It can be :
0074     % - line angle
0075     % - array of four parameters
0076     % TODO : add control for arrays of lines.
0077     var = varargin{1};
0078     
0079     if size(var, 2)==4
0080         % 4 parameters of the line in a single array.
0081         line = var;
0082     elseif size(var, 2)==1
0083         % 1 parameter : angle of the line, going through origin.
0084         line = [zeros(size(var)) zeros(size(var)) cos(var) sin(var)];
0085     else
0086         error('wrong number of dimension for arg1 : can be 1 or 4');
0087     end
0088     
0089 elseif length(varargin)==2    
0090     % 2 input parameters. They can be :
0091     % - line angle and signed distance to origin.
0092     % - 2 points, then 2 arrays of 1*2 double.
0093     v1 = varargin{1};
0094     v2 = varargin{2};
0095     if size(v1, 2)==2 && size(v2, 2)==1
0096         % first param is point, and second param is angle of line
0097         line = [v1(:,1), v1(:,2) cos(v2) sin(v2)];
0098     elseif size(v1, 2)==1
0099         % first param is angle of line, and second param is signed distance
0100         % to origin.
0101         line = [v1.*cos(v2) v1.*sin(v2) -sin(v2) cos(v2)];
0102     elseif size(v1, 2)==3 || size(v2, 2)==3
0103         error('The 1st or 2nd input argument has 3 columns. You may want to try createLine3d.');
0104     else
0105         % first input parameter is first point, and second input is the
0106         % second point.
0107         line = [v1(:,1), v1(:,2), v2(:,1)-v1(:,1), v2(:,2)-v1(:,2)];    
0108     end
0109     
0110 elseif length(varargin)==3
0111     % 3 input parameters :
0112     % first one is a point belonging to the line,
0113     % second and third ones are direction vector of the line (dx and dy).
0114     p = varargin{1};
0115     line = [p(:,1) p(:,2) varargin{2} varargin{3}];
0116    
0117 elseif length(varargin)==4
0118     % 4 input parameters :
0119     % they are x0, y0 (point belongng to line) and dx, dy (direction vector
0120     % of the line).
0121     % All parameters should have the same size.
0122     line = [varargin{1} varargin{2} varargin{3} varargin{4}];
0123 else
0124     error('Wrong number of arguments in ''createLine'' ');
0125 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022