Looking for faster inpolygon - testing whether points are inside a polygon

조회 수: 13 (최근 30일)
Joshua Soneson
Joshua Soneson 2021년 3월 31일
편집: Bruno Luong 2021년 4월 1일
I downloaded and tried both insidepoly and inpoly and both give the same erroneous results on my test problem (a couple 7-gons spattered with random points, the actual application involves irregular polygons that are not simply connected), results that do not agree with matlab's inpolygon. In the attached image, dots are results from inpolygon and circles are from insidepoly. Red is outside, blue is inside, so dots in a different color circle represent errors. Is there a fix for this or an alternative package that gives the same results as inpolygon for less time?
  댓글 수: 2
Matt J
Matt J 2021년 4월 1일
편집: Matt J 2021년 4월 1일
Exactly what are your needs in terms of speed? According to the test below, inpolygon crunches about 4 million points per second. On my laptop, I get about twice that. How much faster do you need to get?
P=1e6; %number of points
N=7; %number of polygon vertices
t=linspace(0,2*pi,N+1)'; t(end)=[];
xy=num2cell(4*rand(P,2)-2,1);
xv=cos(t); yv=sin(t); %polygon vertices
[xq,yq]=deal(xy{:});%query points
pgon=polyshape([xv,yv]);
pq=[xq,yq];
tic;
in=inpolygon(xq,yq,xv,yv);
points_per_sec=P/toc
points_per_sec = 4.1046e+06
%%Visualize
plot(pgon,'FaceColor','none');
hold on
scatter(xq(in),yq(in));
scatter(xq(~in),yq(~in));
hold off
Bruno Luong
Bruno Luong 2021년 4월 1일
편집: Bruno Luong 2021년 4월 1일
"I downloaded and tried both insidepoly and inpoly and both give the same erroneous results on my test problem (a couple 7-gons spattered with random points, the actual application involves irregular polygons that are not simply connected)"
Normal, mathematicaly there is no such thing as non-simply-conected polygons by definition (all the edges mucst form a chain, and close).
insidepolygon() (I'm the author) won't work for such object.

댓글을 달려면 로그인하십시오.

답변 (2개)

darova
darova 2021년 4월 1일
If your polygons are regular try to check points for inside radius first
  • Calculate radius r and R of polygon
  • Calculate distance of point to center of polygon
  • Check if point is inbetween circles
  • Use inpolygon if needed
  댓글 수: 1
Joshua Soneson
Joshua Soneson 2021년 4월 1일
Thanks for responding. This was a test case, my application involves irregular polygons that are not simply connected. I probably should have stated that, will edit the original post.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2021년 4월 1일
편집: Matt J 2021년 4월 1일
Because your polygons are convex, you can obtain the inequality representation A*x<=b of each polygon and then do
X=[x1,y1,z1; x2,y2,z2; ___; xn,yn,zn].';
isInside=all(A*X<b,1)
  댓글 수: 1
Joshua Soneson
Joshua Soneson 2021년 4월 1일
My actual application involves polygons which are not convex in general. However, I tested using a nonconvex polygon and inpoly gives the same results as inpolygon. Interesting.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Computational Geometry에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by