Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Logicals, winding number, -inpolgon

Subject: Logicals, winding number, -inpolgon

From: Zeinab Al-Rekabi

Date: 20 Mar, 2009 14:11:01

Message: 1 of 4

Hello

I am trying to compute the winding number, which is the number of times that a closed curve encircles the point.
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).

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.

Thank you kindly
Z

Subject: Logicals, winding number, -inpolgon

From: Damian Sheehy

Date: 20 Mar, 2009 15:23:25

Message: 2 of 4

Zeinab,

        You simply use "logical indexing". Read the example given in the
help for INPOLYGON.
       % Self-intersecting polygon
        xv = rand(6,1); yv = rand(6,1);
        xv = [xv ; xv(1)]; yv = [yv ; yv(1)];
        x = rand(1000,1); y = rand(1000,1);
        in = inpolygon(x,y,xv,yv);
        plot(xv,yv,x(in),y(in),'.r',x(~in),y(~in),'.b')

The coordinates you are looking for are given by x(in), y(in)

Damian

"Zeinab Al-Rekabi" <nospam-rekabi570@yahoo.ca> wrote in message
news:gq085l$bpd$1@fred.mathworks.com...
> Hello
>
> I am trying to compute the winding number, which is the number of times
> that a closed curve encircles the point.
> 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).
>
> 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.
>
> Thank you kindly
> Z

Subject: Logicals, winding number, -inpolgon

From: Zeinab Al-Rekabi

Date: 20 Mar, 2009 17:04:01

Message: 3 of 4

Thank you so much Damian!
Z


"Damian Sheehy" <Damian.Sheehy@mathworks.com> wrote in message <gq0cdf$5b2$1@fred.mathworks.com>...
> Zeinab,
>
> You simply use "logical indexing". Read the example given in the
> help for INPOLYGON.
> % Self-intersecting polygon
> xv = rand(6,1); yv = rand(6,1);
> xv = [xv ; xv(1)]; yv = [yv ; yv(1)];
> x = rand(1000,1); y = rand(1000,1);
> in = inpolygon(x,y,xv,yv);
> plot(xv,yv,x(in),y(in),'.r',x(~in),y(~in),'.b')
>
> The coordinates you are looking for are given by x(in), y(in)
>
> Damian
>
> "Zeinab Al-Rekabi" <nospam-rekabi570@yahoo.ca> wrote in message
> news:gq085l$bpd$1@fred.mathworks.com...
> > Hello
> >
> > I am trying to compute the winding number, which is the number of times
> > that a closed curve encircles the point.
> > 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).
> >
> > 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.
> >
> > Thank you kindly
> > Z
>

Subject: Logicals, winding number, -inpolgon

From: Roger Stafford

Date: 20 Mar, 2009 23:53:01

Message: 4 of 4

"Zeinab Al-Rekabi" <nospam-rekabi570@yahoo.ca> wrote in message <gq085l$bpd$1@fred.mathworks.com>...
> ......
> I am trying to compute the winding number, which is the number of times that a closed curve encircles the point.
> ......

  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:

 xv = [xv;xv(1)]; yv = [yv;yv(1)];
 x1 = xv(1:end-1); y1 = yv(1:end-1);
 x2 = xv(2:end); y2 = yv(2:end);
 a = atan2((x1-x0).*(y2-y0)-(y1-y0).*(x2-x0), ...
           (x1-x0).*(x2-x0)+(y1-y0).*(y2-y0));
 w = sum(a)/(2*pi);

  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 round-off errors if you like.) It would be positive for a curve proceeding counterclockwise around P and negative for a clockwise direction.

  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

 sin(a) = (x1-x0)*(y2-y0)-(y1-y0)*(x2-x0)

and

 cos(a) = (x1-x0)*(x2-x0)+(y1-y0)*(y2-y0)

It is the angle projected to point P through which the polygon swings from vertex (x1,y1) to vertex (x2,y2).

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us