Home > matGeom > geom2d > distancePointLine.m

distancePointLine

PURPOSE ^

DISTANCEPOINTLINE Minimum distance between a point and a line.

SYNOPSIS ^

function [dist, pos] = distancePointLine(point, line)

DESCRIPTION ^

DISTANCEPOINTLINE Minimum distance between a point and a line.

   D = distancePointLine(POINT, LINE)
   Return the euclidean distance between line LINE and point POINT. 

   LINE has the form: [x0 y0 dx dy], and POINT is [x y].

   If LINE is N-by-4 array, result is N-by-1 array computes for each line.

   If POINT is N-by-2, then result is computed for each point.

   If both POINT and LINE are array, result is computed for each couple of
   point and line, and is returned in a NP-by-NL array, where NP is the
   number of points, and NL is the number of lines.


   See also:
   lines2d, points2d, distancePoints, distancePointEdge

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [dist, pos] = distancePointLine(point, line)
0002 %DISTANCEPOINTLINE Minimum distance between a point and a line.
0003 %
0004 %   D = distancePointLine(POINT, LINE)
0005 %   Return the euclidean distance between line LINE and point POINT.
0006 %
0007 %   LINE has the form: [x0 y0 dx dy], and POINT is [x y].
0008 %
0009 %   If LINE is N-by-4 array, result is N-by-1 array computes for each line.
0010 %
0011 %   If POINT is N-by-2, then result is computed for each point.
0012 %
0013 %   If both POINT and LINE are array, result is computed for each couple of
0014 %   point and line, and is returned in a NP-by-NL array, where NP is the
0015 %   number of points, and NL is the number of lines.
0016 %
0017 %
0018 %   See also:
0019 %   lines2d, points2d, distancePoints, distancePointEdge
0020 %
0021    
0022 % ------
0023 % Author: David Legland
0024 % e-mail: david.legland@nantes.inra.fr
0025 % Created: 2005-06-24
0026 % Copyright 2016 INRA - BIA-BIBS.
0027 
0028 %   HISTORY:
0029 %   2012-10-24 rewrite using bsxfun
0030 
0031 % direction vector of each line (row vectors)
0032 vx = line(:, 3)';
0033 vy = line(:, 4)';
0034 
0035 % squared norm of direction vectors, with a check of validity
0036 delta = (vx .* vx + vy .* vy);
0037 invalidEdges = delta < eps;
0038 delta(invalidEdges) = 1; 
0039 
0040 % difference of coordinates between point and line origins
0041 % (NP-by-NE arrays)
0042 dx  = bsxfun(@minus, point(:, 1), line(:, 1)');
0043 dy  = bsxfun(@minus, point(:, 2), line(:, 2)');
0044 
0045 % compute position of points projected on the line, by using normalised dot
0046 % product
0047 % (result is a NP-by-NL array)
0048 pos = bsxfun(@rdivide, bsxfun(@times, dx, vx) + bsxfun(@times, dy, vy), delta);
0049 
0050 % ensure degenerated lines are correclty processed (consider the line
0051 % origin as closest point)
0052 pos(:, invalidEdges) = 0;
0053 
0054 % compute distance between point and its projection on the line
0055 dist = hypot(bsxfun(@times, pos, vx) - dx, bsxfun(@times, pos, vy) - dy);
0056 
0057 
0058 % if size(line, 1)==1 && size(point, 1)>1
0059 %     line = repmat(line, [size(point, 1) 1]);
0060 % end
0061 %
0062 % if size(point, 1)==1 && size(line, 1)>1
0063 %     point = repmat(point, [size(line, 1) 1]);
0064 % end
0065 %
0066 % dx = line(:, 3);
0067 % dy = line(:, 4);
0068 %
0069 % % compute position of points projected on line
0070 % tp = ((point(:, 2) - line(:, 2)).*dy + (point(:, 1) - line(:, 1)).*dx) ./ (dx.*dx+dy.*dy);
0071 % p0 = line(:, 1:2) + [tp tp].*[dx dy];
0072 %
0073 %
0074 % % compute distances between points and their projections
0075 % dx = point - p0;
0076 % dist  = sqrt(sum(dx.*dx, 2));
0077 
0078 
0079

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