我找到了一个代码来检查点是否在here中的多边形内,但是即使注释表明它可行,它似乎也不起作用。
#include <stdio.h>
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);
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;
}
int main()
{
int numb = 4;
float lat[4] = {1.0,2.0,1.0,2.0};
float lon[4] = {1.0,1.0,2.0,2.0};
float mex = 1.5;
float mey = 1.5;
int a = pnpoly(numb, lat, lon, mex, mey);
printf("%d", a);
return 0;
}
我尝试了一些测试点的代码,但是它不能正常工作,有什么建议吗?
答案 0 :(得分:1)
您的坐标有误:
#include <stdio.h>
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);
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;
}
int main()
{
int numb = 4;
float lat[4] = {1.0, 2.0, 2.0, 1.0}; // was 1.0, 2.0, 1.0, 2.0
float lon[4] = {1.0, 1.0, 2.0, 2.0};
float mex = 1.5;
float mey = 1.5;
int a = pnpoly(numb, lat, lon, mex, mey);
printf("%d\n", a);
return 0;
}
因此该点已正确分类为外部。
我现在只测试了几点,算法似乎就可以工作了。但是,我不会完全检查它。