DISTANCEPOINTPOLYLINE Compute shortest distance between a point and a polyline. DIST = distancePointPolyline(POINT, POLYLINE) Returns the shortest distance between a point given as a 1-by-2 row vector, and a polyline given as a NV-by-2 array of coordinates. If POINT is a NP-by-2 array, the result DIST is a NP-by-1 array, containig the distance of each point to the polyline. [DIST, POS] = distancePointPolyline(POINT, POLYLINE) Also returns the relative position of the point projected on the polyline, between 0 and NV, the number of polyline vertices. ... = distancePointPolyline(POINT, POLYLINE, CLOSED) Specifies if the polyline is closed or not. CLOSED can be one of: * 'closed' -> the polyline is closed * 'open' -> the polyline is open a column vector of logical with the same number of elements as the number of points -> specify individually if each polyline is closed (true=closed). Example: pt1 = [30 20]; pt2 = [30 5]; poly = [10 10;50 10;50 50;10 50]; distancePointPolyline([pt1;pt2], poly) ans = 10 5 See also polygons2d, points2d distancePointEdge, distancePointPolygon, projPointOnPolyline
0001 function [minDist, pos] = distancePointPolyline(point, poly, varargin) 0002 %DISTANCEPOINTPOLYLINE Compute shortest distance between a point and a polyline. 0003 % 0004 % DIST = distancePointPolyline(POINT, POLYLINE) 0005 % Returns the shortest distance between a point given as a 1-by-2 row 0006 % vector, and a polyline given as a NV-by-2 array of coordinates. 0007 % 0008 % If POINT is a NP-by-2 array, the result DIST is a NP-by-1 array, 0009 % containig the distance of each point to the polyline. 0010 % 0011 % [DIST, POS] = distancePointPolyline(POINT, POLYLINE) 0012 % Also returns the relative position of the point projected on the 0013 % polyline, between 0 and NV, the number of polyline vertices. 0014 % 0015 % ... = distancePointPolyline(POINT, POLYLINE, CLOSED) 0016 % Specifies if the polyline is closed or not. CLOSED can be one of: 0017 % * 'closed' -> the polyline is closed 0018 % * 'open' -> the polyline is open 0019 % a column vector of logical with the same number of elements as the 0020 % number of points -> specify individually if each polyline is 0021 % closed (true=closed). 0022 % 0023 % 0024 % Example: 0025 % pt1 = [30 20]; 0026 % pt2 = [30 5]; 0027 % poly = [10 10;50 10;50 50;10 50]; 0028 % distancePointPolyline([pt1;pt2], poly) 0029 % ans = 0030 % 10 0031 % 5 0032 % 0033 % See also 0034 % polygons2d, points2d 0035 % distancePointEdge, distancePointPolygon, projPointOnPolyline 0036 % 0037 0038 % ------ 0039 % Author: David Legland, Juan Pablo Carbajal 0040 % E-mail: david.legland@inrae.fr, ajuanpi+dev@gmail.com 0041 % Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b) 0042 % Copyright 2009-2024 INRA - Cepia Software Platform 0043 0044 % check if input polyline is closed or not 0045 closed = false; 0046 if ~isempty(varargin) 0047 c = varargin{1}; 0048 if strcmp('closed', c) 0049 closed = true; 0050 elseif strcmp('open', c) 0051 closed = false; 0052 elseif islogical(c) 0053 closed = c; 0054 end 0055 end 0056 0057 % closes the polyline if necessary 0058 if closed 0059 poly = [poly; poly(1,:)]; 0060 end 0061 0062 % number of points 0063 Np = size(point, 1); 0064 0065 % construct the set of edges 0066 edges = [poly(1:end-1, :) poly(2:end, :)]; 0067 0068 % compute distance between current each point and all edges, and also 0069 % returns the position of projection on corresponding edge, between 0 and 1 0070 [dist, edgePos] = distancePointEdge(point, edges); 0071 0072 % get the minimum distance, and index of edge providing minimum distance 0073 [minDist, edgeIndex] = min(dist, [], 2); 0074 0075 % if required, compute projections 0076 pos = []; 0077 if nargout == 2 0078 Ne = size(edgePos, 2); 0079 j = sub2ind([Np, Ne], (1:Np)', edgeIndex); 0080 pos = edgeIndex - 1 + edgePos(j); 0081 end