"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.*y2y1.*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
