您好,在 list 上找到了此代码并需要帮助将其转换为 Matlab。
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;
}
nvert:多边形中的顶点数。是否在末尾重复第一个顶点。
vertx、verty:包含多边形顶点的 x 和 y 坐标的数组。
testx, testy:测试点的 X 和 y 坐标。 (这是来自另一个 Stack Overflow 问题: Point in Polygon aka hit test 。
JavaScript 版本:
function insidePoly(poly, pointx, pointy) {
var i, j;
var inside = false;
for (i = 0, j = poly.length - 1; i < poly.length; j = i++) {
if(((poly[i].y > pointy) != (poly[j].y > pointy)) && (pointx < (poly[j].x-poly[i].x) * (pointy-poly[i].y) / (poly[j].y-poly[i].y) + poly[i].x) ) inside = !inside;
}
return inside;
}
这将如何在 Matlab 中转换:
function insidePoly = inpoly(poly, pointx, pointy)
% Code
% return inside
最佳答案
Matlab带有内置函数inpolygon这似乎完全符合你的要求。无需重新实现它。
关于javascript - Matlab 多边形中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29449626/