Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to get the intersection point coordinate between polygon and segment
Date: Sat, 13 Dec 2008 17:32:01 +0000 (UTC)
Organization: Xoran Technologies
Lines: 68
Message-ID: <gi0rih$ljc$1@fred.mathworks.com>
References: <gi0epq$lk3$1@fred.mathworks.com> <gi0nj2$f1r$1@fred.mathworks.com> <gi0omm$ov6$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229189521 22124 172.30.248.37 (13 Dec 2008 17:32:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 13 Dec 2008 17:32:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:506759


I had some bugs in my previously posted code. Here it is again corrected (hopefully).


function c=cutpoints(a,b)
%find intersection points of line segment with polytope
%
% c=cutpoints(a,b)
%
%Rows of a define polytope vertices
%Rows of b are line segment end points
%Rows of c are intersection points


c=nan(2);

 xx=b(1,:).';
 yy=b(2,:).';
 dd=yy-xx;
  
 %[AA,bb]=vert2con(a);
 [AA,bb]=vert2con_special(a);
 
 
 CC=AA*dd;  qq=bb-AA*xx;

 db=(CC==0); 
 if any(qq(db)<0) %ray does not intersect polytope 
    return    
 end
 
 
 %%%Max lower bound
 
 ii=CC<0; lb=qq(ii)./CC(ii);
 
 tmin=max(lb);
 
 %%%Min upper bound
 
   
 ii=~ii; ub=qq(ii)./CC(ii);
   
 tmax=min(ub);
   
if tmin>1 | tmax<0, return, end %no intersection
    
tmin=max(tmin,0);
tmax=min(tmax,1);

   c(1,:)=(xx+tmin*dd).';
   c(2,:)=(xx+tmax*dd).';


%%%%%%%%%
function [A,b]=vert2con_special(a)

centroid=mean(a).';
R=[0 1; -1 0];

A=diff([a;a(1,:)])*R;

b=sum(A.*a,2);

ii=(A*centroid>=b);

b(ii)=-b(ii);
A(ii,:)=-A(ii,:);