annotate toolboxes/FullBNT-1.0.7/KPMtools/polygon_intersect.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [is,S] = isintpl(x1,y1,x2,y2)
wolffd@0 2 % ISINTPL Check for intersection of polygons.
wolffd@0 3 % [IS,S] = ISINTPL(X1,Y1,X2,Y2) Accepts coordinates
wolffd@0 4 % X1,Y1 and X2, Y2 of two polygons. Returns scalar
wolffd@0 5 % IS equal to 1 if these polygons intersect each other
wolffd@0 6 % and 0 if they do not.
wolffd@0 7 % Also returns Boolean matrix S of the size length(X1)
wolffd@0 8 % by length(X2) so that {ij} element of which is 1 if
wolffd@0 9 % line segments i to i+1 of the first polygon and
wolffd@0 10 % j to j+1 of the second polygon intersect each other,
wolffd@0 11 % 0 if they don't and .5 if they "touch" each other.
wolffd@0 12
wolffd@0 13 % Copyright (c) 1995 by Kirill K. Pankratov,
wolffd@0 14 % kirill@plume.mit.edu.
wolffd@0 15 % 06/20/95, 08/25/95
wolffd@0 16
wolffd@0 17
wolffd@0 18 % Handle input ...................................
wolffd@0 19 if nargin==0, help isintpl, return, end
wolffd@0 20 if nargin<4
wolffd@0 21 error(' Not enough input arguments ')
wolffd@0 22 end
wolffd@0 23
wolffd@0 24 % Make column vectors and check sizes ............
wolffd@0 25 x1 = x1(:); y1 = y1(:);
wolffd@0 26 x2 = x2(:); y2 = y2(:);
wolffd@0 27 l1 = length(x1);
wolffd@0 28 l2 = length(x2);
wolffd@0 29 if length(y1)~=l1 | length(y2)~=l2
wolffd@0 30 error('(X1,Y1) and (X2,Y2) must pair-wise have the same length.')
wolffd@0 31 end
wolffd@0 32
wolffd@0 33 % Quick exit if empty
wolffd@0 34 if l1<1 | l2<1, is = []; S = []; return, end
wolffd@0 35
wolffd@0 36 % Check if their ranges are intersecting .........
wolffd@0 37 lim1 = [min(x1) max(x1)]';
wolffd@0 38 lim2 = [min(x2) max(x2)]';
wolffd@0 39 isx = interval(lim1,lim2); % X-ranges
wolffd@0 40 lim1 = [min(y1) max(y1)]';
wolffd@0 41 lim2 = [min(y2) max(y2)]';
wolffd@0 42 isy = interval(lim1,lim2); % Y-ranges
wolffd@0 43 is = isx & isy;
wolffd@0 44 S = zeros(l2,l1);
wolffd@0 45
wolffd@0 46 if ~is, return, end % Early exit if disparate limits
wolffd@0 47
wolffd@0 48 % Indexing .......................................
wolffd@0 49 [i11,i12] = meshgrid(1:l1,1:l2);
wolffd@0 50 [i21,i22] = meshgrid([2:l1 1],[2:l2 1]);
wolffd@0 51 i11 = i11(:); i12 = i12(:);
wolffd@0 52 i21 = i21(:); i22 = i22(:);
wolffd@0 53
wolffd@0 54 % Calculate matrix of intersections ..............
wolffd@0 55 S(:) = iscross([x1(i11) x1(i21)]',[y1(i11) y1(i21)]',...
wolffd@0 56 [x2(i12) x2(i22)]',[y2(i12) y2(i22)]')';
wolffd@0 57
wolffd@0 58 S = S';
wolffd@0 59 is = any(any(S));
wolffd@0 60