javascript - Matlab 多边形中的点

标签 javascript c matlab

您好,在 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/

相关文章:

javascript - 多个 document.getElementsByTagName ('thetag' )选择的最短脚本?

C-char指针和char指针字符串管理

c - 反向(字符 * s,int 暗淡);在不使用 strlen 的情况下递归反转字符串

matlab - 提高 App Designer UI 元素的刷新率

c++ - 读取 mex 文件中的结构数据时发生奇怪的事情

javascript - 覆盖 .bind 和 .apply 中的 `this`

javascript - 是否有办法在 iFrame 内操作来自外部域的 HTML 文档?

javascript - 表单未重定向到新页面

C语言,readdir和segmentation fault on running

matlab - 我如何在 Matlab 中标记两个向量?