DISTANCEPOINTPOLYGON Shortest distance between a point and a polygon. DIST = distancePointPolygon(POINT, POLYGON) Computes the shortest distance between the point POINT and the polygon given by POLYGON. POINT is a 1-by-2 row vector, and POLYGON is a N-by-2 array containing vertex coordinates. The distance is computed as the minimal distance to the boundary edges. Example % Computes the distance between a point and a square square = [0 0; 10 0;10 10;0 10]; p0 = [16 3]; distancePointPolygon(p0, square) ans = 6 See also polygons2d, points2d, distancePointPolyline, distancePointEdge, projPointOnPolyline
0001 function minDist = distancePointPolygon(point, poly) 0002 %DISTANCEPOINTPOLYGON Shortest distance between a point and a polygon. 0003 % 0004 % DIST = distancePointPolygon(POINT, POLYGON) 0005 % Computes the shortest distance between the point POINT and the polygon 0006 % given by POLYGON. POINT is a 1-by-2 row vector, and POLYGON is a N-by-2 0007 % array containing vertex coordinates. 0008 % The distance is computed as the minimal distance to the boundary edges. 0009 % 0010 % Example 0011 % % Computes the distance between a point and a square 0012 % square = [0 0; 10 0;10 10;0 10]; 0013 % p0 = [16 3]; 0014 % distancePointPolygon(p0, square) 0015 % ans = 0016 % 6 0017 % 0018 % See also 0019 % polygons2d, points2d, distancePointPolyline, distancePointEdge, 0020 % projPointOnPolyline 0021 % 0022 0023 % ------ 0024 % Author: David Legland 0025 % e-mail: david.legland@nantes.inra.fr 0026 % Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b) 0027 % Copyright 2009 INRA - Cepia Software Platform. 0028 0029 % eventually copy first point at the end to ensure closed polygon 0030 if sum(poly(end, :) == poly(1,:)) ~= 2 0031 poly = [poly; poly(1,:)]; 0032 end 0033 0034 % call to distancePointPolyline 0035 minDist = distancePointPolyline(point, poly);