I'm trying to find if certain points are inside or outside a polygon

10 Ansichten (letzte 30 Tage)
Justine
Justine am 4 Dez. 2012
Hi, I'm trying to find if certain points are inside or outside a polygon. I have a polygon file of a list of coordinates, and then have another file with a list of coordinates of points (some of which are inside the polygon, other are outside). The code I have written is as follows:
%Compare point coordinates
%Load Coordinates
load starcoordinates.txt;
xstar= starcoordinates(:,1);
ystar = starcoordinates(:,2);
%Row count
Srows = numel(textread('starcoordinates.txt','%1c%*[^\n]'));
%set initial values
Nint = 0;
%Loop through points
for h=1:Srows;
for k=1:Nrows;
if k==Nrows
%end point+1 is the same as 1st point
i=Nrows;
j=1;
else
%Otherwise use the current point
i=k;
j=k+1;
end
V = sort(square(:,1));
xmin = V(1);
xmax = V(Nrows);
Z = sort(square(:,2));
ymin = Z(1);
ymax = Z(Nrows);
% Find points between minimum and maximum values
if ystar(h) <= ymin
Nint = 0;
elseif ystar(h) >= ymax
Nint = 0;
if xstar(h) >= xmax
Nint = 0;
elseif xstar(h) < xmin
Nint = 0;
%Point is within minima and maxima
%check
else
Bp=((y(j)-y(i))/(x(j)-x(i)));
Ap=(y(j)-(Bp.*x(i)));
Yp = Ap + (Bp.*Xp);
Xp = (ystar(h)-Ap).*(1/Bp);
if xstar(h) <= Xp
Nint = Nint + 1;
end
end
end
end
Ntot = ((floor(Nint/2)).*2)-Nint;
%Ntot=0 if Nint=even number, Ntot=-1 if Nint=odd number %if -1 point is inside the polygon, if 0 point is outside the polygon if Ntot == 0 ; disp = 'star is outside the cloud' else Ntot == -1; disp = 'star is inside the cloud' end end
It's telling me that all the points are outside the polygon so is taking all Ntot to be 0, I think. If anyone can help me solve this problem and see what is wrong in my code it would save my life!!!

Antworten (3)

Jonathan Sullivan
Jonathan Sullivan am 4 Dez. 2012
help inpolygon
doc inpolygon
  2 Kommentare
Justine
Justine am 5 Dez. 2012
I didn't know about this function. Thank you so much it works perfectly!!!
Jonathan Sullivan
Jonathan Sullivan am 6 Dez. 2012
Glad it helped. Could you mark this question as answered?

Melden Sie sich an, um zu kommentieren.


Babak
Babak am 4 Dez. 2012
I think one should be able to easily do a simple inequality and find out of a point is outside or inside a closed curve. Here's my idea, for example for a circle with radius 1, and centered at origin, if you want to know if the point (x,y) is inside it or outside, you can do x^2+y^2<1 or x^2+y^2>1. for a square with each side equal to sqrt(2) and centered at origin but rotated 45 deg you can do the comparison abs(x)+abs(y)>1 or abs(x)+abs(y)<1. Offcourse if the length of the side of the equare (or radius of circle) is different you can do a simple transformation and change that 1 to proper number. Or if the shape is are not centered at origin you can do a transformation (translation) on the points as well as the shape to bring the shapre to origin. Or if you want to change the angle or orientation of the shape (like as for square) you need to rotate all the points (transformation again) to get that rotated shape.
The hardest step would be to find the quation of the polygon you are looking for and write the proper inquality.

Matt J
Matt J am 6 Dez. 2012
Bearbeitet: Matt J am 6 Dez. 2012
If it's a convex polygon, a possibly faster approach than INPOLYGON is to use this FEX file to express the polygon in terms of linear inequalities
A*x<=b
Then, to see if a particular x0 lies in the polygon, do
isinside = all(A*x0<=b,1);
  2 Kommentare
Babak
Babak am 6 Dez. 2012
Very nice Matt! This is exactly what I was talking about...
Matt J
Matt J am 7 Dez. 2012
Bearbeitet: Matt J am 7 Dez. 2012
Yes, I thought it might be similar. Convexity of the polygon is key, though. I don't think you can express non-convex polygon's via simultaneous inequalities.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Computational Geometry finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by