Is there a way to use inpolygon more than once ?

1 view (last 30 days)
I am trying to use inpolygon more than once in my code, but each time when i run the program is identifying points in only one polygon.Is there a way to use inpolygon more than once ?
  2 Comments
Jan
Jan on 11 Mar 2022
Yes, of course. Most likely your code conatins a bug, e.g. if you run a loop over i and use j as index of the data. If you post the relevant part of the code, we can try to find the problem.

Sign in to comment.

Answers (1)

Prahlad Gowtham Katte
Prahlad Gowtham Katte on 16 Mar 2022
Hello
We can use the inpolygon function more than once in a script file. The following code shows inpolygon being used more than once for 2 different polygons.
%Co-ordinates for 1st
polygon
xv = [0.5;0.2;1.0;0;0.8;0.5];
yv = [1.0;0.1;0.7;0.7;0.1;1];
%Points to be checked with 1st polygon
xq = [0.1;0.5;0.9;0.2;0.4;0.5;0.5;0.9;0.6;0.8;0.7;0.2];
yq = [0.4;0.6;0.9;0.7;0.3;0.8;0.2;0.4;0.4;0.6;0.2;0.6];
%Points to be checked with 2nd polygon
xq2 = xq+1;
yq2 = yq+0.5;
%Co-ordinates for 2nd polygon
xv2=xv+1;
yv2=yv+1;
%Getting the indicators for inside and on the edge points
[on,in]=inpolygon(xq,yq,xv,yv);
[on2,in2]=inpolygon(xq,yq,xv2,yv2);
figure
plot(xv,yv) % 1st polygon
hold on
plot(xv2,yv2) % 2nd polygon
plot(xq(in&~on),yq(in&~on),'r+') % points strictly inside
plot(xq(on),yq(on),'r*') % points on edge
plot(xq(~in),yq(~in),'ro') % points outside
%2nd polygon
plot(xq2(in2&~on2),yq2(in2&~on2),'g+') % points strictly inside
plot(xq2(on2),yq2(on2),'g*') % points on edge
plot(xq2(~in2),yq2(~in2),'go') % points outside
hold off
For more information related to inpolygon please refer to the following link
Hope it helps.

Tags

Community Treasure Hunt

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

Start Hunting!