pnpoly 判断点是否在多边形内部(c++)

遇到了一个问题,如何判断一个点是否在一个多边形内部。html

主要有如下几种方法:测试

(1)面积和判别法:判断目标点与多边形的每条边组成的三角形面积和是否等于该多边形,相等则在多边形内部。spa

(2)夹角和判别法:判断目标点与全部边的夹角和是否为360度,为360度则在多边形内部。.net

(3)引射线法:从目标点出发引一条射线,看这条射线和多边形全部边的交点数目。若是有奇数个交点,则说明在内部,若是有偶数个交点,则说明在外部。code

 

简洁的C++代码以下:htm

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) { int i, j, c = 0; for (i = 0, j = nvert-1; i < nvert; j = i++) { if ( ((verty[i]>testy) != (verty[j]>testy)) && (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) ) c = !c; } return c; }

 

Matlab验证代码:blog

clc; close; %% poly points 设定的多边形顶点 % poly_point_x = [1 1 10 14 7]; % poly_point_y = [0 12 13 2 4]; poly_point_x = [0 0 9]; poly_point_y = [1 12 1]; %% actual points 待测试顶点 test_x = 6; test_y = 0; c=0; m=0; %% return test point in poly or not 判断测试点是否在多边形内 hold on; plot(poly_point_x,poly_point_y,'*') plot(test_x,test_y,'.') n = 4;%n=顶点数+1
for i = 1: n-1
    if i == 1 j = n - 1; else j = i - 1; end y = ((poly_point_y(:,i) > test_y) ~= (poly_point_y(:,j) > test_y)); x = ((poly_point_x(:,j) - poly_point_x(:,i))*(test_y - poly_point_y(:,i))/(poly_point_y(:,j) - poly_point_y(:,i)) + poly_point_x(:,i))>test_x; if y&&x c = ~c; end end c

 

当返回0时,表明点不在多边形中。当返回1时,表明点在多边形中。get

 

参考资料:    

笔记A0320180207class

http://blog.csdn.net/luyuncsd123/article/details/27528519test

http://www.javashuo.com/article/p-aektebzd-bg.html

相关文章
相关标签/搜索