wolffd@0: function [is,S] = isintpl(x1,y1,x2,y2) wolffd@0: % ISINTPL Check for intersection of polygons. wolffd@0: % [IS,S] = ISINTPL(X1,Y1,X2,Y2) Accepts coordinates wolffd@0: % X1,Y1 and X2, Y2 of two polygons. Returns scalar wolffd@0: % IS equal to 1 if these polygons intersect each other wolffd@0: % and 0 if they do not. wolffd@0: % Also returns Boolean matrix S of the size length(X1) wolffd@0: % by length(X2) so that {ij} element of which is 1 if wolffd@0: % line segments i to i+1 of the first polygon and wolffd@0: % j to j+1 of the second polygon intersect each other, wolffd@0: % 0 if they don't and .5 if they "touch" each other. wolffd@0: wolffd@0: % Copyright (c) 1995 by Kirill K. Pankratov, wolffd@0: % kirill@plume.mit.edu. wolffd@0: % 06/20/95, 08/25/95 wolffd@0: wolffd@0: wolffd@0: % Handle input ................................... wolffd@0: if nargin==0, help isintpl, return, end wolffd@0: if nargin<4 wolffd@0: error(' Not enough input arguments ') wolffd@0: end wolffd@0: wolffd@0: % Make column vectors and check sizes ............ wolffd@0: x1 = x1(:); y1 = y1(:); wolffd@0: x2 = x2(:); y2 = y2(:); wolffd@0: l1 = length(x1); wolffd@0: l2 = length(x2); wolffd@0: if length(y1)~=l1 | length(y2)~=l2 wolffd@0: error('(X1,Y1) and (X2,Y2) must pair-wise have the same length.') wolffd@0: end wolffd@0: wolffd@0: % Quick exit if empty wolffd@0: if l1<1 | l2<1, is = []; S = []; return, end wolffd@0: wolffd@0: % Check if their ranges are intersecting ......... wolffd@0: lim1 = [min(x1) max(x1)]'; wolffd@0: lim2 = [min(x2) max(x2)]'; wolffd@0: isx = interval(lim1,lim2); % X-ranges wolffd@0: lim1 = [min(y1) max(y1)]'; wolffd@0: lim2 = [min(y2) max(y2)]'; wolffd@0: isy = interval(lim1,lim2); % Y-ranges wolffd@0: is = isx & isy; wolffd@0: S = zeros(l2,l1); wolffd@0: wolffd@0: if ~is, return, end % Early exit if disparate limits wolffd@0: wolffd@0: % Indexing ....................................... wolffd@0: [i11,i12] = meshgrid(1:l1,1:l2); wolffd@0: [i21,i22] = meshgrid([2:l1 1],[2:l2 1]); wolffd@0: i11 = i11(:); i12 = i12(:); wolffd@0: i21 = i21(:); i22 = i22(:); wolffd@0: wolffd@0: % Calculate matrix of intersections .............. wolffd@0: S(:) = iscross([x1(i11) x1(i21)]',[y1(i11) y1(i21)]',... wolffd@0: [x2(i12) x2(i22)]',[y2(i12) y2(i22)]')'; wolffd@0: wolffd@0: S = S'; wolffd@0: is = any(any(S)); wolffd@0: