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
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@nantes.inra.fr 0047 % Created: 2004-04-07 0048 % Copyright 2016 INRA - BIA-BIBS. 0049 % 0050 0051 % HISTORY 0052 % 2005-06-24 rename, and change arguments sequence 0053 % 2009-04-30 add possibility to return position of closest point 0054 % 2011-04-14 add checkup for degenerate edges, improve speed, update doc 0055 0056 0057 % direction vector of each edge (row vectors) 0058 vx = (edge(:, 3) - edge(:,1))'; 0059 vy = (edge(:, 4) - edge(:,2))'; 0060 0061 % squared length of edges, with a check of validity 0062 delta = vx .* vx + vy .* vy; 0063 invalidEdges = delta < eps; 0064 delta(invalidEdges) = 1; 0065 0066 % difference of coordinates between point and edge first vertex 0067 % (NP-by-NE arrays) 0068 dx = bsxfun(@minus, point(:, 1), edge(:, 1)'); 0069 dy = bsxfun(@minus, point(:, 2), edge(:, 2)'); 0070 0071 % compute position of points projected on the supporting line, by using 0072 % normalised dot product (NP-by-NE array) 0073 pos = bsxfun(@rdivide, bsxfun(@times, dx, vx) + bsxfun(@times, dy, vy), delta); 0074 0075 % ensure degenerated edges are correclty processed (consider the first 0076 % vertex is the closest) 0077 pos(:, invalidEdges) = 0; 0078 0079 % change position to ensure projected point is located on the edge 0080 pos(pos < 0) = 0; 0081 pos(pos > 1) = 1; 0082 0083 % compute distance between point and its projection on the edge 0084 dist = hypot(bsxfun(@times, pos, vx) - dx, bsxfun(@times, pos, vy) - dy);