LINEPOSITION Position of a point on a line. POS = linePosition(POINT, LINE); Computes position of point POINT on the line LINE, relative to origin point and direction vector of the line. LINE has the form [x0 y0 dx dy], POINT has the form [x y], and is assumed to belong to line. POS = linePosition(POINT, LINES); If LINES is an array of NL lines, return NL positions, corresponding to each line. POS = linePosition(POINTS, LINE); If POINTS is an array of NP points, return NP positions, corresponding to each point. POS = linePosition(POINTS, LINES); If POINTS is an array of NP points and LINES is an array of NL lines, return an array of [NP NL] position, corresponding to each couple point-line. POS = linePosition(POINTS, LINES, 'diag'); When POINTS and LINES have the same number of rows, computes positions only for couples POINTS(i,:) and LINES(i,:). The result POS is a column vector with as many rows as the number of points/lines. Example line = createLine([10 30], [30 90]); linePosition([20 60], line) ans = .5 See also: lines2d, createLine, projPointOnLine, isPointOnLine --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 25/05/2004.
0001 function pos = linePosition(point, line, varargin) 0002 %LINEPOSITION Position of a point on a line. 0003 % 0004 % POS = linePosition(POINT, LINE); 0005 % Computes position of point POINT on the line LINE, relative to origin 0006 % point and direction vector of the line. 0007 % LINE has the form [x0 y0 dx dy], 0008 % POINT has the form [x y], and is assumed to belong to line. 0009 % 0010 % POS = linePosition(POINT, LINES); 0011 % If LINES is an array of NL lines, return NL positions, corresponding to 0012 % each line. 0013 % 0014 % POS = linePosition(POINTS, LINE); 0015 % If POINTS is an array of NP points, return NP positions, corresponding 0016 % to each point. 0017 % 0018 % POS = linePosition(POINTS, LINES); 0019 % If POINTS is an array of NP points and LINES is an array of NL lines, 0020 % return an array of [NP NL] position, corresponding to each couple 0021 % point-line. 0022 % 0023 % POS = linePosition(POINTS, LINES, 'diag'); 0024 % When POINTS and LINES have the same number of rows, computes positions 0025 % only for couples POINTS(i,:) and LINES(i,:). The result POS is a column 0026 % vector with as many rows as the number of points/lines. 0027 % 0028 % 0029 % Example 0030 % line = createLine([10 30], [30 90]); 0031 % linePosition([20 60], line) 0032 % ans = 0033 % .5 0034 % 0035 % See also: 0036 % lines2d, createLine, projPointOnLine, isPointOnLine 0037 % 0038 % --------- 0039 % 0040 % author : David Legland 0041 % INRA - TPV URPOI - BIA IMASTE 0042 % created the 25/05/2004. 0043 % 0044 0045 % HISTORY 0046 % 2005-07-07 manage multiple input 0047 % 2011-06-15 avoid the use of repmat when possible 0048 % 2012-10-24 rewrite using bsxfun 0049 % 2012-11-22 add support for the diag option 0050 0051 % if diag is true, we need only to compute position of i-th point with i-th 0052 % line. 0053 diag = ~isempty(varargin) && ischar(varargin{1}) && strcmpi(varargin{1}, 'diag'); 0054 0055 if diag 0056 % In the case of 'diag' option, use direct correspondence between 0057 % points and lines 0058 0059 % check input have same size 0060 np = size(point, 1); 0061 nl = size(line, 1); 0062 if np ~= nl 0063 error('matGeom:linePosition', ... 0064 'Using diag option, number of points and lines should be the same'); 0065 end 0066 0067 % direction vector of the lines 0068 vx = line(:, 3); 0069 vy = line(:, 4); 0070 0071 % difference of coordinates between point and line origins 0072 dx = point(:, 1) - line(:, 1); 0073 dy = point(:, 2) - line(:, 2); 0074 0075 else 0076 % General case -> return NP-by-NL array 0077 0078 % direction vector of the lines 0079 vx = line(:, 3)'; 0080 vy = line(:, 4)'; 0081 0082 % difference of coordinates between point and line origins 0083 dx = bsxfun(@minus, point(:, 1), line(:, 1)'); 0084 dy = bsxfun(@minus, point(:, 2), line(:, 2)'); 0085 0086 end 0087 0088 % squared norm of direction vector, with a check of validity 0089 delta = vx .* vx + vy .* vy; 0090 invalidLine = delta < eps; 0091 delta(invalidLine) = 1; 0092 0093 % compute position of points projected on the line, by using normalised dot 0094 % product (NP-by-NL array) 0095 pos = bsxfun(@rdivide, bsxfun(@times, dx, vx) + bsxfun(@times, dy, vy), delta); 0096 0097 % ensure degenerated edges are correclty processed (consider the first 0098 % vertex is the closest one) 0099 if diag 0100 pos(invalidLine) = 0; 0101 else 0102 pos(:, invalidLine) = 0; 0103 end