From: Ivo Petkovi? on 28 May 2010 10:37 "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <ht1pcm$c74$1(a)fred.mathworks.com>... > I have take a pain to code it, LOL. I also take opportunity to improve my poly2poly() function for speed. Here we go: > > function b = isintersect(P1, P2) > % function b = isintersect(P1, P2) > % Check if two polygons intersect > > c = poly2poly(P1, P2); > if isempty(c) > b = inpolygon(P2(1,1),P2(2,1),P1(1,:),P1(2,:)) || ... > inpolygon(P1(1,1),P1(2,1),P2(1,:),P2(2,:)); > else > b = true; > end > > end % isintersect > > function c = poly2poly(P1, P2) > % function c = poly2poly(P1, P2) > % intersection of two polygons P1 and P2. > % P1 and P2 are two-row arrays, each column is a vertice > % c is also two-row arrays, each column is an intersecting point > > % Pad the first point to the end if necessary > if ~isequal(P1(:,1),P1(:,end)) > P1 = [P1 P1(:,1)]; > end > if ~isequal(P2(:,1),P2(:,end)) > P2 = [P2 P2(:,1)]; > end > > c = zeros(2,0); > for n=2:size(P1,2) > c=[c seg2poly(P1(:,n-1:n), P2)]; %#ok > end > > end % poly2poly > > function c = seg2poly(s1, P) > % function c = seg2poly(s1, P) > % Check if a segment (2 x 2) segment s1 intersects with a polygon P. > % s(:,1) is the first point s(:,2) is the the second point of the segment. > % P is (2 x n) arrays, each column is a vertices > > % Translate and rotation so that first point is origin > % the second point is on y-axis > a = s1(:,1); > M = bsxfun(@minus, P, a); > b = s1(:,2)-a; > nb2 = b(1)^2+b(2)^2; > R = [b(2) -b(1); > b(1) b(2)]; > M = R*M; > sx = sign(M(1,:)); > % x -coordinates has opposite signs > ind = sx(1:end-1).*sx(2:end) <= 0; > if any(ind) > ind = find(ind); > % cross point to y-axis > x1 = M(1,ind); > x2 = M(1,ind+1); > y1 = M(2,ind); > y2 = M(2,ind+1); > dx = x2-x1; > y = (y1.*x2-y2.*x1)./dx; > y = y/nb2; > ind = y>=0 & y<1; > if any(ind) > c = bsxfun(@plus, b*y(ind), a); > else > c = zeros(2,0); > end > else > c = zeros(2,0); > end > > end % seg2poly > > % Bruno I will try it now. Thanks man!
First
|
Prev
|
Pages: 1 2 3 4 Prev: first order auto-regressive process to model video traffic Next: Galil Motion Controller |