Home > matGeom > polygons2d > polygonContains.m

polygonContains

PURPOSE ^

POLYGONCONTAINS Test if a point is contained in a multiply connected polygon.

SYNOPSIS ^

function varargout = polygonContains(poly, point)

DESCRIPTION ^

POLYGONCONTAINS Test if a point is contained in a multiply connected polygon.

   B = polygonContains(POLYGON, POINT);
   Returns TRUE if the (possibly multi-connected) polygon POLYGON contains
   the point(s) given by POINT.
   This is an extension of the Matlab function inpolygon for the case of
   polygons with holes.

   Example
   POLY = [0 0; 10 0;10 10;0 10;NaN NaN;3 3;3 7;7 7;7 3];
   PT = [5 1;5 4];
   polygonContains(POLY, PT);
   ans =
        1
        0

   See also
   polygons2d, inpolygon, isPointInPolygon

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = polygonContains(poly, point)
0002 %POLYGONCONTAINS Test if a point is contained in a multiply connected polygon.
0003 %
0004 %   B = polygonContains(POLYGON, POINT);
0005 %   Returns TRUE if the (possibly multi-connected) polygon POLYGON contains
0006 %   the point(s) given by POINT.
0007 %   This is an extension of the Matlab function inpolygon for the case of
0008 %   polygons with holes.
0009 %
0010 %   Example
0011 %   POLY = [0 0; 10 0;10 10;0 10;NaN NaN;3 3;3 7;7 7;7 3];
0012 %   PT = [5 1;5 4];
0013 %   polygonContains(POLY, PT);
0014 %   ans =
0015 %        1
0016 %        0
0017 %
0018 %   See also
0019 %   polygons2d, inpolygon, isPointInPolygon
0020 
0021 % ------
0022 % Author: David Legland
0023 % E-mail: david.legland@inrae.fr
0024 % Created: 2007-10-11, using Matlab 7.4.0.287 (R2007a)
0025 % Copyright 2007-2024 INRA - BIA PV Nantes - MIAJ Jouy-en-Josas
0026 
0027 % transform as a cell array of simple polygons
0028 polygons = splitPolygons(poly);
0029 N = length(polygons);
0030 Np = size(point, 1);
0031 
0032 % compute orientation of polygon, and format to have Np*N matrix
0033 areas = zeros(N, 1);
0034 for i = 1:N
0035     areas(i) = polygonArea(polygons{i});
0036 end
0037 ccw = areas > 0;
0038 ccw = repmat(ccw', Np, 1);
0039 
0040 % test if point inside each polygon
0041 in = false(size(point, 1), N);
0042 for i = 1:N
0043     poly = polygons{i};
0044     in(:, i) = inpolygon(point(:,1), point(:,2), poly(:,1), poly(:,2));
0045 end
0046 
0047 % count polygons containing point, weighted by polygon orientation
0048 res = sum(in.*(ccw==1) - in.*(ccw==0), 2);
0049 
0050 varargout{1} = res;

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022