将多边形与矩形相交并创建线(剖面切割)

发布于 2024-09-27 23:32:16 字数 383 浏览 7 评论 0原文

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

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

这将用于剖面切割模块,该模块可以对一组多边形进行切片并在形状与由 z 值指定的“平面”相交处创建 2D“切割”。

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

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

这是一张图片...我想要红线作为相交的结果: 替代文本

I need an algorithm to intersect a (potentially non-convex) polygon with a rectangle. The rectangle will be parallel to the xy-plane, but the polygon could be any orientation.

Furthermore, I don't just need a true/false result, but also the exact points where the polygon intersects the rectangle, so that I can draw lines where the polygon overlaps the rectangle. For non-convex polygons, this could result in two or more lines of intersection.

This would be for a section-cut module that can slice a set of polygons and create a 2D "cut" where the shapes intersect the "plane," specified by a z-value.

I'm developing in Java, so if Java3(2)D has any built-in methods to help, that would be ideal.

Any help/pointers in the right direction would be greatly appreciated!

Here's a picture... I want the red line as a result of the intersection:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-10-04 23:32:16

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

将多边形视为边 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       

此算法值得您为此付出的代价。 :-)

This should find all the intersecting segments, for any arbitrary polygon.

Consider the polygon as an ordered collection of edges AB,BC,CD,etc, where the 'direction' from each edge's first point to its second point is 'clockwise'. That is, if we're looking at point A, point B is the next point when moving clockwise.

The method is to find an edge of the polygon that crosses the plane and then find the next segment, moving clockwise, that crosses back to the original side of the plane. The two points where these segments intersect the plane form the endpoints for an intersecting segment. This is repeated until all the polygon's edges have been checked.

Be advised not all segments are necessarily within the polygon if it is concave.

   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       

This algorithm is worth what you paid for it. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文