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@inrae.fr
0025 % Created: 2005-06-24
0026 % Copyright 2005-2024 INRA - BIA-BIBS
0027 
0028 % direction vector of each line (row vectors)
0029 vx = line(:, 3)';
0030 vy = line(:, 4)';
0031 
0032 % squared norm of direction vectors, with a check of validity
0033 delta = (vx .* vx + vy .* vy);
0034 invalidEdges = delta < eps;
0035 delta(invalidEdges) = 1; 
0036 
0037 % difference of coordinates between point and line origins
0038 % (NP-by-NE arrays)
0039 dx  = bsxfun(@minus, point(:, 1), line(:, 1)');
0040 dy  = bsxfun(@minus, point(:, 2), line(:, 2)');
0041 
0042 % compute position of points projected on the line, by using normalised dot
0043 % product
0044 % (result is a NP-by-NL array)
0045 pos = bsxfun(@rdivide, bsxfun(@times, dx, vx) + bsxfun(@times, dy, vy), delta);
0046 
0047 % ensure degenerated lines are correclty processed (consider the line
0048 % origin as closest point)
0049 pos(:, invalidEdges) = 0;
0050 
0051 % compute distance between point and its projection on the line
0052 dist = hypot(bsxfun(@times, pos, vx) - dx, bsxfun(@times, pos, vy) - dy);
0053 
0054 
0055 % if size(line, 1)==1 && size(point, 1)>1
0056 %     line = repmat(line, [size(point, 1) 1]);
0057 % end
0058 %
0059 % if size(point, 1)==1 && size(line, 1)>1
0060 %     point = repmat(point, [size(line, 1) 1]);
0061 % end
0062 %
0063 % dx = line(:, 3);
0064 % dy = line(:, 4);
0065 %
0066 % % compute position of points projected on line
0067 % tp = ((point(:, 2) - line(:, 2)).*dy + (point(:, 1) - line(:, 1)).*dx) ./ (dx.*dx+dy.*dy);
0068 % p0 = line(:, 1:2) + [tp tp].*[dx dy];
0069 %
0070 %
0071 % % compute distances between points and their projections
0072 % dx = point - p0;
0073 % dist  = sqrt(sum(dx.*dx, 2));
0074 
0075 
0076

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