Home > matGeom > geom2d > linePosition.m

linePosition

PURPOSE ^

LINEPOSITION Position of a point on a line.

SYNOPSIS ^

function pos = linePosition(point, line, varargin)

DESCRIPTION ^

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, linePoint, projPointOnLine, isPointOnLine, createLine

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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, linePoint, projPointOnLine, isPointOnLine, createLine
0037 
0038 % ------
0039 % Author: David Legland
0040 % E-mail: david.legland@inrae.fr
0041 % Created: 2004-05-25
0042 % Copyright 2004-2024 INRA - TPV URPOI - BIA IMASTE
0043 
0044 % if diag is true, we need only to compute position of i-th point with i-th
0045 % line.
0046 diag = ~isempty(varargin) && ischar(varargin{1}) && strcmpi(varargin{1}, 'diag');
0047 
0048 if diag
0049     % In the case of 'diag' option, use direct correspondence between
0050     % points and lines
0051     
0052     % check input have same size
0053     np = size(point, 1);
0054     nl = size(line, 1);
0055     if np ~= nl
0056         error('matGeom:linePosition', ...
0057             'Using diag option, number of points and lines should be the same');
0058     end
0059     
0060     % direction vector of the lines
0061     vx = line(:, 3);
0062     vy = line(:, 4);
0063     
0064     % difference of coordinates between point and line origins
0065     dx = point(:, 1) - line(:, 1);
0066     dy = point(:, 2) - line(:, 2);
0067     
0068 else
0069     % General case -> return NP-by-NL array
0070     
0071     % direction vector of the lines
0072     vx = line(:, 3)';
0073     vy = line(:, 4)';
0074     
0075     % difference of coordinates between point and line origins
0076     dx = bsxfun(@minus, point(:, 1), line(:, 1)');
0077     dy = bsxfun(@minus, point(:, 2), line(:, 2)');
0078 
0079 end
0080 
0081 % squared norm of direction vector, with a check of validity
0082 delta = vx .* vx + vy .* vy;
0083 invalidLine = delta < eps;
0084 delta(invalidLine) = 1; 
0085 
0086 % compute position of points projected on the line, by using normalised dot
0087 % product (NP-by-NL array)
0088 pos = bsxfun(@rdivide, bsxfun(@times, dx, vx) + bsxfun(@times, dy, vy), delta);
0089 
0090 % ensure degenerated edges are correclty processed (consider the first
0091 % vertex is the closest one)
0092 if diag
0093     pos(invalidLine) = 0;
0094 else
0095     pos(:, invalidLine) = 0;
0096 end

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