Home > matGeom > geom2d > distancePointEdge.m

distancePointEdge

PURPOSE ^

DISTANCEPOINTEDGE Minimum distance between a point and an edge.

SYNOPSIS ^

function [dist, pos] = distancePointEdge(point, edge)

DESCRIPTION ^

DISTANCEPOINTEDGE Minimum distance between a point and an edge.

   DIST = distancePointEdge(POINT, EDGE);
   Return the euclidean distance between edge EDGE and point POINT. 
   EDGE has the form: [x1 y1 x2 y2], and POINT is [x y].

   If EDGE is N-by-4 array, result is 1-by-4 array computed for each edge.
   If POINT is a N-by-2 array, the result is a N-by-1 array.
   If both POINT and EDGE are array, the result is computed for each
   point-edge couple, and stored into a NP-by-NE array.

   [DIST POS] = distancePointEdge(POINT, EDGE);
   Also returns the position of closest point on the edge. POS is
   comprised between 0 (first point) and 1 (last point).

   Eaxmple
     % Distance between a point and an edge
     distancePointEdge([3 4], [0 0 10 0])
     ans =
         4

     % Distance between several points and one edge
     points = [10 15; 15 10; 30 10];
     edge   = [10 10 20 10];
     distancePointEdge(points, edge)
     ans = 
         5 
         0
        10

     % Distance between a point a several edges
     point = [14 33];
     edges  = [10 30 20 30; 20 30 20 40;20 40 10 40;10 40 10 30];
     distancePointEdge(point, edges)
     ans = 
         3    6    7    4


   See also
   edges2d, points2d, distancePoints, distancePointLine

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [dist, pos] = distancePointEdge(point, edge)
0002 %DISTANCEPOINTEDGE Minimum distance between a point and an edge.
0003 %
0004 %   DIST = distancePointEdge(POINT, EDGE);
0005 %   Return the euclidean distance between edge EDGE and point POINT.
0006 %   EDGE has the form: [x1 y1 x2 y2], and POINT is [x y].
0007 %
0008 %   If EDGE is N-by-4 array, result is 1-by-4 array computed for each edge.
0009 %   If POINT is a N-by-2 array, the result is a N-by-1 array.
0010 %   If both POINT and EDGE are array, the result is computed for each
0011 %   point-edge couple, and stored into a NP-by-NE array.
0012 %
0013 %   [DIST POS] = distancePointEdge(POINT, EDGE);
0014 %   Also returns the position of closest point on the edge. POS is
0015 %   comprised between 0 (first point) and 1 (last point).
0016 %
0017 %   Eaxmple
0018 %     % Distance between a point and an edge
0019 %     distancePointEdge([3 4], [0 0 10 0])
0020 %     ans =
0021 %         4
0022 %
0023 %     % Distance between several points and one edge
0024 %     points = [10 15; 15 10; 30 10];
0025 %     edge   = [10 10 20 10];
0026 %     distancePointEdge(points, edge)
0027 %     ans =
0028 %         5
0029 %         0
0030 %        10
0031 %
0032 %     % Distance between a point a several edges
0033 %     point = [14 33];
0034 %     edges  = [10 30 20 30; 20 30 20 40;20 40 10 40;10 40 10 30];
0035 %     distancePointEdge(point, edges)
0036 %     ans =
0037 %         3    6    7    4
0038 %
0039 %
0040 %   See also
0041 %   edges2d, points2d, distancePoints, distancePointLine
0042 %
0043 
0044 % ------
0045 % Author: David Legland
0046 % E-mail: david.legland@inrae.fr
0047 % Created: 2004-04-07
0048 % Copyright 2004-2024 INRA - BIA-BIBS
0049 
0050 % direction vector of each edge (row vectors)
0051 vx = (edge(:, 3) - edge(:,1))';
0052 vy = (edge(:, 4) - edge(:,2))';
0053 
0054 % squared length of edges, with a check of validity
0055 delta = vx .* vx + vy .* vy;
0056 invalidEdges = delta < eps;
0057 delta(invalidEdges) = 1; 
0058 
0059 % difference of coordinates between point and edge first vertex
0060 % (NP-by-NE arrays)
0061 dx  = bsxfun(@minus, point(:, 1), edge(:, 1)');
0062 dy  = bsxfun(@minus, point(:, 2), edge(:, 2)');
0063 
0064 % compute position of points projected on the supporting line, by using
0065 % normalised dot product (NP-by-NE array)
0066 pos = bsxfun(@rdivide, bsxfun(@times, dx, vx) + bsxfun(@times, dy, vy), delta);
0067 
0068 % ensure degenerated edges are correclty processed (consider the first
0069 % vertex is the closest)
0070 pos(:, invalidEdges) = 0;
0071 
0072 % change position to ensure projected point is located on the edge
0073 pos(pos < 0) = 0;
0074 pos(pos > 1) = 1;
0075 
0076 % compute distance between point and its projection on the edge
0077 dist = hypot(bsxfun(@times, pos, vx) - dx, bsxfun(@times, pos, vy) - dy);

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