Path: news.mathworks.com!not-for-mail From: "Amy Haskins" <amy.haskins.nospam@mathworks.com> Newsgroups: comp.soft-sys.matlab Subject: Re: Polybool gives subtraction with wrong vertices Date: Tue, 7 May 2013 22:13:10 +0000 (UTC) Organization: The MathWorks Inc Lines: 42 Message-ID: <kmbu9m$q70$1@newscl01ah.mathworks.com> References: <kls6k5$osr$1@newscl01ah.mathworks.com> Reply-To: "Amy Haskins" <amy.haskins.nospam@mathworks.com> NNTP-Posting-Host: www-06-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1367964790 26848 172.30.248.38 (7 May 2013 22:13:10 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Tue, 7 May 2013 22:13:10 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 3028654 Xref: news.mathworks.com comp.soft-sys.matlab:795060 Hi Kenan, In regards to your question, it looks like you're running into some rounding errors here. If you subtract the rectangle from the large triangle directly, you will get the result you would expect: [x3,y3] = polybool('subtraction',x0,y0,x1,y1); % subtraction When you do this as two steps, there is a very small amount of rounding error in the calculation which causes the extra line in your results. If we shift the bottom vertex down even slightly, the correct answer is given: y2([1,4]) = y2([1,4]) - .000001; [x3,y3] = polybool('subtraction',x0,y0,x2,y2); % subtraction In a world of infinite percision, the two-step approach would work just fine. However, since we are dealing with floating point, there is about a 50/50 chance that each edges of the smaller triangle will be ever so slightly inside the larger one. This is just a natural limitation of doing computation geometry with floating point numbers, the results from polybool are not surprising. Amy "Kenan" wrote in message <kls6k5$osr$1@newscl01ah.mathworks.com>... > > I am using the function polybool from the Mapping Toolbox and my version of Matlab is R2013a. > > I have problems when I perform a simple subtraction of two polygonal regions (a rectangle described by [x1 y1] and a triangle described by [x0 y0]). See attached code and figures. > > px = .7; > py = .75; > lx = (5*2^.5-6)/20; > ly = (10*3^.5-9)/60; > > [x1,y1] = poly2cw([px; px+lx; px+lx; px; px],[py; py; py+ly; py+ly; py]); % rectangle > [x0,y0] = poly2cw([15;15;14;15]/19,[15;16;16;15]/19); % triangle > [x2,y2] = polybool('intersection',x1,y1,x0,y0); % intersection > > [x3,y3] = polybool('subtraction',x0,y0,x2,y2); % soustraction > > figure; plot(x0,y0,'b--',x1,y1,'k:',x3,y3,'r+-'); hold on > for i=1:length(x3) > text(x3(i)*1.003,y3(i)*1.003,num2str(i)) > end > > As you can see, there is an extra vertex that should not be there (vertex 2-3). So instead of being 1-2,2-3,3-4,4-5,5-6; the subtraction should be 1-3,3-4,4-5,5-6. (6 being equal to 1). > >