Home > matGeom > polygons2d > distancePointPolyline.m

distancePointPolyline

PURPOSE ^

DISTANCEPOINTPOLYLINE Compute shortest distance between a point and a polyline.

SYNOPSIS ^

function [minDist, pos] = distancePointPolyline(point, poly, varargin)

DESCRIPTION ^

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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
0040 % e-mail: david.legland@nantes.inra.fr
0041 % Created: 2009-04-30,    using Matlab 7.7.0.471 (R2008b)
0042 % Copyright 2009 INRA - Cepia Software Platform.
0043 % Author: Juan Pablo Carbajal
0044 % e-mail: ajuanpi+dev@gmail.com
0045 
0046 %   HISTORY
0047 %   2009-06-23 compute all distances in one call
0048 %   2016-02-04 Vectorize
0049 
0050 % check if input polyline is closed or not
0051 closed = false;
0052 if ~isempty(varargin)
0053     c = varargin{1};
0054     if strcmp('closed', c)
0055         closed = true;
0056     elseif strcmp('open', c)
0057         closed = false;
0058     elseif islogical(c)
0059         closed = c;
0060     end
0061 end
0062 
0063 % closes the polyline if necessary
0064 if closed
0065     poly = [poly; poly(1,:)];
0066 end
0067 
0068 % number of points
0069 Np = size(point, 1);
0070 
0071 % construct the set of edges
0072 edges = [poly(1:end-1, :) poly(2:end, :)];
0073 
0074 % compute distance between current each point and all edges, and also
0075 % returns the position of projection on corresponding edge, between 0 and 1
0076 [dist, edgePos] = distancePointEdge(point, edges);
0077 
0078 % get the minimum distance, and index of edge providing minimum distance
0079 [minDist, edgeIndex] = min(dist, [], 2);
0080 
0081 % if required, compute projections
0082 pos = [];
0083 if nargout == 2
0084     Ne = size(edgePos, 2);
0085     j  = sub2ind([Np, Ne], (1:Np)', edgeIndex);
0086     pos = edgeIndex - 1 + edgePos(j);
0087 end

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