PROJPOINTONPOLYGON Compute position of a point projected on a polygon. POS = projPointOnPolygon(POINT, POLYGON) Compute the position of the orthogonal projection of a point on a polygon. POINT is a 1-by-2 row vector containing point coordinates POLYGON is a N-by-2 array containing coordinates of polygon vertices When POINT is an array of points, returns a column vector with as many rows as the number of points. [POS, DIST] = projPointOnPolygon(...) Also returns the distance between POINT and POLYGON. The distance is negative if the point is located inside of the polygon. Example poly = [10 10; 20 10;20 20;10 20]; projPointOnPolygon([15 0], poly) ans = 0.5000 projPointOnPolygon([0 16], poly) ans = 3.4000 See also points2d, polygons2d, polygonPoint, projPointOnPolyline distancePointpolygon
0001 function varargout = projPointOnPolygon(point, poly, varargin) 0002 %PROJPOINTONPOLYGON Compute position of a point projected on a polygon. 0003 % 0004 % POS = projPointOnPolygon(POINT, POLYGON) 0005 % Compute the position of the orthogonal projection of a point on a 0006 % polygon. 0007 % POINT is a 1-by-2 row vector containing point coordinates 0008 % POLYGON is a N-by-2 array containing coordinates of polygon vertices 0009 % 0010 % When POINT is an array of points, returns a column vector with as many 0011 % rows as the number of points. 0012 % 0013 % [POS, DIST] = projPointOnPolygon(...) 0014 % Also returns the distance between POINT and POLYGON. The distance is 0015 % negative if the point is located inside of the polygon. 0016 % 0017 % Example 0018 % poly = [10 10; 20 10;20 20;10 20]; 0019 % projPointOnPolygon([15 0], poly) 0020 % ans = 0021 % 0.5000 0022 % projPointOnPolygon([0 16], poly) 0023 % ans = 0024 % 3.4000 0025 % 0026 % See also 0027 % points2d, polygons2d, polygonPoint, projPointOnPolyline 0028 % distancePointpolygon 0029 % 0030 0031 % ------ 0032 % Author: David Legland 0033 % e-mail: david.legland@grignon.inra.fr 0034 % Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b) 0035 % Copyright 2009 INRA - Cepia Software Platform. 0036 0037 0038 % eventually copy first point at the end to ensure closed polygon 0039 if sum(poly(end, :) == poly(1,:)) ~= 2 0040 poly = [poly; poly(1,:)]; 0041 end 0042 0043 % compute position wrt outline 0044 [pos, minDist] = projPointOnPolyline(point, poly); 0045 0046 % process output arguments 0047 if nargout <= 1 0048 varargout{1} = pos; 0049 elseif nargout == 2 0050 varargout{1} = pos; 0051 if inpolygon(point(:,1), point(:,2), poly(:,1), poly(:,2)) 0052 minDist = -minDist; 0053 end 0054 varargout{2} = minDist; 0055 end