Matlab点在多边形中

Matlab point in polygon

本文关键字:多边形 Matlab      更新时间:2023-09-26

Hi在该列表中找到了此代码,需要帮助将其转换为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坐标。(这来自另一个堆栈溢出问题:多边形中的点aka命中测试。

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,它似乎正是你想要的。无需重新实现。