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@inra.fr
0024 % Created: 2007-10-11,    using Matlab 7.4.0.287 (R2007a)
0025 % Copyright 2007 INRA - BIA PV Nantes - MIAJ Jouy-en-Josas.
0026 
0027 
0028 % transform as a cell array of simple polygons
0029 polygons = splitPolygons(poly);
0030 N = length(polygons);
0031 Np = size(point, 1);
0032 
0033 % compute orientation of polygon, and format to have Np*N matrix
0034 areas = zeros(N, 1);
0035 for i = 1:N
0036     areas(i) = polygonArea(polygons{i});
0037 end
0038 ccw = areas > 0;
0039 ccw = repmat(ccw', Np, 1);
0040 
0041 % test if point inside each polygon
0042 in = false(size(point, 1), N);
0043 for i = 1:N
0044     poly = polygons{i};
0045     in(:, i) = inpolygon(point(:,1), point(:,2), poly(:,1), poly(:,2));
0046 end
0047 
0048 % count polygons containing point, weighted by polygon orientation
0049 res = sum(in.*(ccw==1) - in.*(ccw==0), 2);
0050 
0051 varargout{1} = res;

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