【问题标题】:Intersect polygon with rectangle and create lines (section cut)将多边形与矩形相交并创建线(截面切割)
【发布时间】:2011-04-26 11:27:37
【问题描述】:

我需要一个算法来将一个(可能是非凸的)多边形与一个矩形相交。矩形将平行于 xy 平面,但多边形可以是任何方向。

此外,我不仅需要真/假结果,还需要多边形与矩形相交的确切点,这样我就可以在多边形与矩形重叠的地方画线。对于非凸多边形,这可能会导致两条或更多条相交线。

这适用于一个截面切割模块,该模块可以切割一组多边形并创建一个二维“切割”,其中形状与由 z 值指定的“平面”相交。

我正在使用 Java 进行开发,因此如果 Java3(2)D 有任何内置方法可以提供帮助,那将是理想的选择。

任何正确方向的帮助/指针将不胜感激!

这是一张图片...我想要红线作为交叉点的结果:

【问题讨论】:

  • 您是否反对将多边形的每条线段相交并连接点的微不足道的事情?
  • 这就是我想象的解决方案。 Java3D可以将线段与多边形相交并返回交点吗?我必须小心非凸多边形相交两次,但我想我可以处理这个问题,如果我能得到交点......
  • 我不知道 Java3D,快速搜索发现:code.j3d.org/using/geom_intersect.html

标签: java 3d polygon intersect


【解决方案1】:

这应该找到任意多边形的所有相交段。

将多边形视为边 AB、BC、CD 等的有序集合,其中从每条边的第一个点到第二个点的“方向”是“顺时针”。也就是说,如果我们看点 A,顺时针移动时,点 B 就是下一个点。

方法是找到多边形的一条边,穿过平面,然后找到下一条线段,顺时针移动,回到平面的原始边。这些线段与平面相交的两个点形成相交线段的端点。重复此操作,直到检查完所有多边形的边缘为止。

请注意,如果多边形是凹的,则并非所有线段都必须在多边形内。

   let P be any point on the polygon.

   TOP:
   while (P has not been checked)

       mark P as having been checked.

       let f be the point following P, clockwise.

       if (P and f are on opposite sides of the plane) then

          Continuing from f clockwise, find the next point Y that is on
              the same side of the plane as P.
          Let z be the point counter-clockwise from Y.
              (note - Sometimes z and f are the same point.)

          let S1 be the point where P,f intersects the plane
          let S2 be the point where Y,z intersects the plane

          if (segment (S1,S2) is inside the polygon)
              add (S1,S2) to a 'valid' list.
              let P = Y
          else
              let P = f
          endif    
       else
          let P = f
       endif
   endwhile       

这个算法物有所值。 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-21
    • 2011-04-14
    • 2015-05-07
    • 2020-02-23
    • 2014-07-28
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    相关资源
    最近更新 更多