http://www.mathworks.com/matlabcentral/newsreader/view_thread/247139
MATLAB Central Newsreader  Logicals, winding number, inpolgon
Feed for thread: Logicals, winding number, inpolgon
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 20 Mar 2009 14:11:01 +0000
Logicals, winding number, inpolgon
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247139#636432
Zeinab AlRekabi
Hello<br>
<br>
I am trying to compute the winding number, which is the number of times that a closed curve encircles the point.<br>
I used the matlab function [IN]=inpolygon(X,Y,xv,yv), where X,Y are the points and xv,yv are the curves coutour (assuming 2d).<br>
<br>
I obtain a logical array nX1, which has 0s and 1s. 0s being outside the countour and 1s being inside the contour. My question: how do I convert such a logical array into the X,Y numbers which it originally was, so baiscally for those values that were 1, how do I get their X,Y values? Is there a matlab function that does this conversion and if not may anyone assist me in obtaining them.<br>
<br>
Thank you kindly<br>
Z

Fri, 20 Mar 2009 15:23:25 +0000
Re: Logicals, winding number, inpolgon
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247139#636447
Damian Sheehy
Zeinab,<br>
<br>
You simply use "logical indexing". Read the example given in the <br>
help for INPOLYGON.<br>
% Selfintersecting polygon<br>
xv = rand(6,1); yv = rand(6,1);<br>
xv = [xv ; xv(1)]; yv = [yv ; yv(1)];<br>
x = rand(1000,1); y = rand(1000,1);<br>
in = inpolygon(x,y,xv,yv);<br>
plot(xv,yv,x(in),y(in),'.r',x(~in),y(~in),'.b')<br>
<br>
The coordinates you are looking for are given by x(in), y(in)<br>
<br>
Damian<br>
<br>
"Zeinab AlRekabi" <nospamrekabi570@yahoo.ca> wrote in message <br>
news:gq085l$bpd$1@fred.mathworks.com...<br>
> Hello<br>
><br>
> I am trying to compute the winding number, which is the number of times <br>
> that a closed curve encircles the point.<br>
> I used the matlab function [IN]=inpolygon(X,Y,xv,yv), where X,Y are the <br>
> points and xv,yv are the curves coutour (assuming 2d).<br>
><br>
> I obtain a logical array nX1, which has 0s and 1s. 0s being outside the <br>
> countour and 1s being inside the contour. My question: how do I convert <br>
> such a logical array into the X,Y numbers which it originally was, so <br>
> baiscally for those values that were 1, how do I get their X,Y values? Is <br>
> there a matlab function that does this conversion and if not may anyone <br>
> assist me in obtaining them.<br>
><br>
> Thank you kindly<br>
> Z

Fri, 20 Mar 2009 17:04:01 +0000
Re: Logicals, winding number, inpolgon
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247139#636465
Zeinab AlRekabi
Thank you so much Damian!<br>
Z<br>
<br>
<br>
"Damian Sheehy" <Damian.Sheehy@mathworks.com> wrote in message <gq0cdf$5b2$1@fred.mathworks.com>...<br>
> Zeinab,<br>
> <br>
> You simply use "logical indexing". Read the example given in the <br>
> help for INPOLYGON.<br>
> % Selfintersecting polygon<br>
> xv = rand(6,1); yv = rand(6,1);<br>
> xv = [xv ; xv(1)]; yv = [yv ; yv(1)];<br>
> x = rand(1000,1); y = rand(1000,1);<br>
> in = inpolygon(x,y,xv,yv);<br>
> plot(xv,yv,x(in),y(in),'.r',x(~in),y(~in),'.b')<br>
> <br>
> The coordinates you are looking for are given by x(in), y(in)<br>
> <br>
> Damian<br>
> <br>
> "Zeinab AlRekabi" <nospamrekabi570@yahoo.ca> wrote in message <br>
> news:gq085l$bpd$1@fred.mathworks.com...<br>
> > Hello<br>
> ><br>
> > I am trying to compute the winding number, which is the number of times <br>
> > that a closed curve encircles the point.<br>
> > I used the matlab function [IN]=inpolygon(X,Y,xv,yv), where X,Y are the <br>
> > points and xv,yv are the curves coutour (assuming 2d).<br>
> ><br>
> > I obtain a logical array nX1, which has 0s and 1s. 0s being outside the <br>
> > countour and 1s being inside the contour. My question: how do I convert <br>
> > such a logical array into the X,Y numbers which it originally was, so <br>
> > baiscally for those values that were 1, how do I get their X,Y values? Is <br>
> > there a matlab function that does this conversion and if not may anyone <br>
> > assist me in obtaining them.<br>
> ><br>
> > Thank you kindly<br>
> > Z <br>
>

Fri, 20 Mar 2009 23:53:01 +0000
Re: Logicals, winding number, inpolgon
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247139#636551
Roger Stafford
"Zeinab AlRekabi" <nospamrekabi570@yahoo.ca> wrote in message <gq085l$bpd$1@fred.mathworks.com>...<br>
> ......<br>
> I am trying to compute the winding number, which is the number of times that a closed curve encircles the point.<br>
> ......<br>
<br>
I don't think 'inpolygon' is what you need for your problem, Zeinab. You can get this "winding number" directly entirely in terms of the closed polygon's vertices in xv,yv. Assume these are column vectors and call "the point" around which winding occurs P = [x0,y0]. Then do this:<br>
<br>
xv = [xv;xv(1)]; yv = [yv;yv(1)];<br>
x1 = xv(1:end1); y1 = yv(1:end1);<br>
x2 = xv(2:end); y2 = yv(2:end);<br>
a = atan2((x1x0).*(y2y0)(y1y0).*(x2x0), ...<br>
(x1x0).*(x2x0)+(y1y0).*(y2y0));<br>
w = sum(a)/(2*pi);<br>
<br>
This winding number w should be an integer for a closed polygon except for very small rounding errors. (You can apply the 'round' function to clear away the effect of roundoff errors if you like.) It would be positive for a curve proceeding counterclockwise around P and negative for a clockwise direction.<br>
<br>
The 'atan2' is a great function for problems like this. In this problem you are finding the angle a in radians between pi and +pi for which<br>
<br>
sin(a) = (x1x0)*(y2y0)(y1y0)*(x2x0)<br>
<br>
and<br>
<br>
cos(a) = (x1x0)*(x2x0)+(y1y0)*(y2y0)<br>
<br>
It is the angle projected to point P through which the polygon swings from vertex (x1,y1) to vertex (x2,y2).<br>
<br>
Roger Stafford