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