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:
short question, arbitrary point lies in or out of closed curve?

Subject: short question, arbitrary point lies in or out of closed curve?

From: leo nidas

Date: 7 Dec, 2011 20:43:08

Message: 1 of 3



Hi there,

I have a short question.I have vectors x and y.
plot(x,y) yields a closed shape, like a squeezed ellipse or something.

Now given an arbitrary point (x0,y0), is there an easy way to determine if this point lies inside this closed curve or outside of it.

By looking the plot this is an obvious answer but I would like an indicator function that would yield e.g. 1 if it is in the curve and 0 if it is outside.

Is there an easy way for this? I thought yes but I got stucked.. Thanx in advance for any answers!

Subject: short question, arbitrary point lies in or out of closed curve?

From: John D'Errico

Date: 7 Dec, 2011 20:55:08

Message: 2 of 3

"leo nidas" <bleonidas25@yahoo.gr> wrote in message <jboj4s$36k$1@newscl01ah.mathworks.com>...
>
>
> Hi there,
>
> I have a short question.I have vectors x and y.
> plot(x,y) yields a closed shape, like a squeezed ellipse or something.
>
> Now given an arbitrary point (x0,y0), is there an easy way to determine if this point lies inside this closed curve or outside of it.
>
> By looking the plot this is an obvious answer but I would like an indicator function that would yield e.g. 1 if it is in the curve and 0 if it is outside.
>
> Is there an easy way for this? I thought yes but I got stucked.. Thanx in advance for any answers!

help inpolygon

John

Subject: short question, arbitrary point lies in or out of closed curve?

From: Roger Stafford

Date: 8 Dec, 2011 05:36:08

Message: 3 of 3

"leo nidas" <bleonidas25@yahoo.gr> wrote in message <jboj4s$36k$1@newscl01ah.mathworks.com>...
> I have a short question. I have vectors x and y.
> plot(x,y) yields a closed shape, like a squeezed ellipse or something.
>
> Now given an arbitrary point (x0,y0), is there an easy way to determine if this point lies inside this closed curve or outside of it.
- - - - - - - - -
  Just in case you might be curious as to how 'inpolygon' could perform its task, here is code that can do the same thing, though it is undoubtedly not as efficient. It is intended only to illustrate a concept.

  Let x and y be column vectors for a closed curve that does not cross itself, so that "inside" and "outside" are meaningful terms, and let (x0,y0) be the point in question. Then do this:

 x1 = x - x0; y1 = y - y0; % Translate the vectors' base to (x0,y0)
 x2 = x1([2:end,1]); y2 = y1([2:end,1]); % Shift ahead by one index
 s = sum(atan2(x1.*y2-y1.*x2,x1.*x2+y1.*y2)); % Sum the projected angles
 in = abs(s) > pi; % 'in' is true if (x0,y0) is inside, otherwise false

  For any (x0,y0) inside the curve, 's' will be either 2*pi or -2*pi, depending on whether the curve is being traced with increasing index going counterclockwise or clockwise, respectively, and in either case 'in' will be true. For (x0,y0) outside the curve, 's' will necessarily be zero and 'in' will be false.

  The theory behind this is that each value in the 'atan2' result vector is the signed angle between -pi and +pi projected back onto the point (x0,y0) by motion along each of the polygon's consecutive line segments. The sum of these angles must be the net angle swept out by motion around the full polygon, and for an inside point it therefore must be a full circle of either 2*pi or -2*pi radians and for an outside point a net value of zero.

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