function ind=insider(x,y,xv,yv,tol); % INSIDER determines if points are inside/outside a polygon. % IND=INSIDER(X,Y,XV,YV) is a vector of flags corresponding to % the vector of points X,Y. If % % - X(i),Y(i) is outside the polygon XV,YV : IND(i)=0 % - X(i),Y(i) is inside " " " " : IND(i)=1 % - X(i),Y(i) is on an edge of the polygon : IND(i)=2 % - X(i),Y(i) is a vertex of the polygon : IND(i)=4 % % If a point falls into several categories, it is classified as % with the flag of greatest numerical value (i.e. a point on an % interior edge is given a flag value of 2). % % XV,YV is an ordered list of polygon vertices. The last point % in the list (identical to the first point) may be omitted. % There is no restriction on the shape of the polygon. % % IND=INSIDER(...,TOL) sets a tolerance for assigning points % to edges or vertices. (the default is 1e-6). %RP (WHOI) 19/Dec/91 % - this routine uses the fact that the sum of angles between % lines from a point to successive vertices of a polygon is 0 % for a point outside the polygon, and +/- 2*pi for a point % inside. A point on an edge will have one such succesive angle % equal to +/- pi. %RP (WHOI) 5/Dec/92 % - some bulletproofing for really wierd checkerboard polygons and % single point "polygons". if (nargin<5), tol=1e-6; end; % Put things into rows or columns... x=x(:)'; y=y(:)'; xv=xv(:); yv=yv(:); NP=length(x); NV=length(xv); if (NP~=length(y)), error('Point x,y vectors of unequal length!'); end; if (NV~=length(yv)), error('Polygon vertex x,y vectors of unequal length!');end; % If last point is missing append it if ( (xv(NV) ~= xv(1)) | (yv(NV) ~= yv(1))), xv=[xv;xv(1)]; yv=[yv;yv(1)]; NV=NV+1; end; % distances from points to vertices dx=ones(NV,1)*x-xv*ones(1,NP); dy=ones(NV,1)*y-yv*ones(1,NP); % If we have only 1 point in the polygon, we can only search % for matches to the "vertex". Otherwise we do all the tests. if (NV==1), vertexpt=( abs(dx)