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 16:43:02 +0000 (UTC)
Organization: Xoran Technologies
Lines: 43
Message-ID: <gi0omm$ov6$1@fred.mathworks.com>
References: <gi0epq$lk3$1@fred.mathworks.com> <gi0nj2$f1r$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 1229186582 25574 172.30.248.37 (13 Dec 2008 16:43:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 13 Dec 2008 16:43:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:506752



> I also tested its speed on 10000 successive triangles using both a for loop and cellfun()
> 
> a=[0 0;1 0;0 1]; b=[0.4 0.4;1 1];
> A=cell(1,10000);B=A;  A(:)={a}; B(:)={b};
> 
> >> tic, C=cellfun(@cutpoints,A,B,'UniformOutput',false); toc
> Elapsed time is 11.256645 seconds.
> 
> >> tic, for ii=1:10000, c=cutpoints(a,b); end,toc
> Elapsed time is 11.231848 seconds.
> 
> Not much difference, so I guess you'll have to be the judge of  whether it's fast enough.

%%%%%%%%%%%%%%%%


I found that the code becomes much faster, when I replace vert2con() with vert2con_special() below. The latter assumes a 2D convex polygon and with vertices sequentially ordered.

>> tic, C=cellfun(@cutpoints,A,B,'UniformOutput',false); toc
Elapsed time is 1.092368 seconds.
>> tic, for ii=1:10000, c=cutpoints(a,b); end,toc
Elapsed time is 1.107186 seconds.


For-loops and cellfun() are still about the same though...



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,:);