Algorithm 多边形中的点算法说明

Algorithm 多边形中的点算法说明,algorithm,matlab,polygon,point,Algorithm,Matlab,Polygon,Point,嗨,我遇到了这个算法,它决定一个点是在多边形的内部还是外部。到目前为止,代码工作得很好,但我不完全理解逻辑。请提供一个解释,如果你这样做,特别是对方法…无论是射线铸造,或绕组编号等。谢谢 function [ inside ] = inpoly(polygon,xt,yt) rows = size(polygon); npoints = rows(1); disp (npoints); inside = 0; xold = polygon(npoints,1); yold = polygon

嗨,我遇到了这个算法,它决定一个点是在多边形的内部还是外部。到目前为止,代码工作得很好,但我不完全理解逻辑。请提供一个解释,如果你这样做,特别是对方法…无论是射线铸造,或绕组编号等。谢谢

function [ inside ] = inpoly(polygon,xt,yt)

rows = size(polygon);
npoints = rows(1);
disp (npoints);
inside = 0;

xold = polygon(npoints,1);
yold = polygon(npoints,2);

for i = 1:1:npoints

   xnew = polygon(i,1);
   ynew = polygon(i,2);

      if (xnew > xold) 
         x1=xold;
         x2=xnew;
         y1=yold;
         y2=ynew;
      else 
         x1=xnew;
         x2=xold;
         y1=ynew;
         y2=yold;
      end

      if ((xnew < xt) == (xt <= xold) & (yt-y1)*(x2-x1) < (y2-y1)*(xt-x1) ) 
               inside=~inside;
      end

      xold=xnew;
      yold=ynew;

end

endfunction

其中p和q是多边形的顶点,x和y是点的坐标。

对我来说似乎是光线投射。变量
x1
y1
x2
y2
是相对于X排序的多边形边的端点。条件
(xnewinpoly([p,q],x,y)
(yt-y1)*(x2-x1) < (y2-y1)*(xt-x1)
(yt-y1)*(x2-x1) - (y2-y1)*(xt-x1) < 0
| yt-y1  xt-x1 |
|              | < 0
| y2-y1  x2-x1 |
(pointT - point1) times (point2 - point1)