annotate _FullBNT/KPMtools/polygon_intersect.m @ 9:4ea6619cb3f5 tip

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