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@inrae.fr 0034 % Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b) 0035 % Copyright 2009-2024 INRA - Cepia Software Platform 0036 0037 % eventually copy first point at the end to ensure closed polygon 0038 if sum(poly(end, :) == poly(1,:)) ~= 2 0039 poly = [poly; poly(1,:)]; 0040 end 0041 0042 % compute position wrt outline 0043 [pos, minDist] = projPointOnPolyline(point, poly); 0044 0045 % process output arguments 0046 if nargout <= 1 0047 varargout{1} = pos; 0048 elseif nargout == 2 0049 varargout{1} = pos; 0050 if inpolygon(point(:,1), point(:,2), poly(:,1), poly(:,2)) 0051 minDist = -minDist; 0052 end 0053 varargout{2} = minDist; 0054 end