CREATEEDGE Create an edge between two points, or from a line. The internal format for edge representation is given by coordinates of two points : [x1 y1 x2 y2]. This function can serve as a line to edge converter. E = createEdge(P1, P2); Returns the edge between the two given points P1 and P2. E = createEdge(x0, y0, dx, dy); Returns the edge going through point (x0, y0) and with direction vector (dx,dy). E = createEdge(param); where param is an array of 4 values, creates the edge going through the point (param(1) param(2)), and with direction vector given by (param(3) param(4)). E = createEdge(LINE, D); create the edge contained in LINE, with same direction and start point, but with length given by D. Note: in all cases, parameters can be vertical arrays of the same dimension. The result is then an array of edges, of dimensions N-by-4. See also: edges2d, lines2d, drawEdge, clipEdge, createLine
0001 function edge = createEdge(varargin) 0002 %CREATEEDGE Create an edge between two points, or from a line. 0003 % 0004 % The internal format for edge representation is given by coordinates of 0005 % two points : [x1 y1 x2 y2]. 0006 % This function can serve as a line to edge converter. 0007 % 0008 % 0009 % E = createEdge(P1, P2); 0010 % Returns the edge between the two given points P1 and P2. 0011 % 0012 % E = createEdge(x0, y0, dx, dy); 0013 % Returns the edge going through point (x0, y0) and with direction 0014 % vector (dx,dy). 0015 % 0016 % E = createEdge(param); 0017 % where param is an array of 4 values, creates the edge going through the 0018 % point (param(1) param(2)), and with direction vector given by 0019 % (param(3) param(4)). 0020 % 0021 % E = createEdge(LINE, D); 0022 % create the edge contained in LINE, with same direction and start point, 0023 % but with length given by D. 0024 % 0025 % 0026 % Note: in all cases, parameters can be vertical arrays of the same 0027 % dimension. The result is then an array of edges, of dimensions N-by-4. 0028 % 0029 % 0030 % See also: 0031 % edges2d, lines2d, drawEdge, clipEdge, createLine 0032 % 0033 0034 % --------- 0035 % author : David Legland 0036 % INRA - TPV URPOI - BIA IMASTE 0037 % created the 31/10/2003. 0038 % 0039 0040 % HISTORY 0041 % 18/02/2004 : add more possibilities to create edges, not only from 2 0042 % points. Also add support for arrays. 0043 % 31/03/2004 : convert to [P1 P2] format 0044 0045 if nargin == 1 0046 % Only one input parameter. It can be : 0047 % - line angle 0048 % - array of four parameters 0049 % TODO : add control for arrays of lines. 0050 var = varargin{1}; 0051 0052 if size(var, 2)==4 0053 % 4 parameters of the line in a single array. 0054 %edge = var; 0055 edge = zeros(size(var)); 0056 edge(:, 1:2) = var(:,1:2); 0057 edge(:, 3:4) = edge(:, 1:2)+var(:,3:4); 0058 elseif size(var, 2)==1 0059 % 1 parameter : angle of the line, going through origin. 0060 edge = [zeros(size(var,1)) zeros(size(var,1)) cos(var) sin(var)]; 0061 else 0062 error('wrong number of dimension for arg1 : can be 1 or 4'); 0063 end 0064 0065 elseif nargin == 2 0066 % 2 input parameters. They can be : 0067 % - 2 points, then 2 arrays of 1*2 double, 0068 % - a line, and a distance. 0069 0070 % extract the two arguments 0071 v1 = varargin{1}; 0072 v2 = varargin{2}; 0073 0074 if size(v1, 2) == 2 0075 % first input parameter is first point, and second input is the 0076 % second point. 0077 %edge = [v1(:,1), v1(:,2), v2(:,1), v2(:,2)]; 0078 edge = [v1 v2]; 0079 else 0080 % first input parameter is a line, and second one a distance. 0081 angle = atan2(v1(:,4), v1(:,3)); 0082 edge = [v1(:,1), v1(:,2), v1(:,1)+v2.*cos(angle), v1(:,2)+v2.*sin(angle)]; 0083 end 0084 0085 elseif nargin == 3 0086 % 3 input parameters : 0087 % first one is a point belonging to the line, 0088 % second and third ones are direction vector of the line (dx and dy). 0089 p = varargin{1}; 0090 edge = [p(:,1) p(:,2) p(:,1)+varargin{2} p(:,2)+varargin{3}]; 0091 0092 elseif nargin == 4 0093 % 4 input parameters : 0094 % they are x0, y0 (point belonging to line) and dx, dy (direction 0095 % vector of the line). 0096 % All parameters should have the same size. 0097 edge = [varargin{1} varargin{2} varargin{1}+varargin{3} varargin{2}+varargin{4}]; 0098 else 0099 error('Wrong number of arguments in ''createEdge'' '); 0100 end