将多边形与矩形相交并创建线(剖面切割)
我需要一种算法来将(可能是非凸的)多边形与矩形相交。矩形将平行于 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该找到任意多边形的所有相交线段。
将多边形视为边 AB、BC、CD 等的有序集合,其中从每条边的第一个点到第二个点的“方向”是“顺时针”。也就是说,如果我们看 A 点,顺时针移动时,B 点就是下一个点。
该方法是找到穿过平面的多边形的一条边,然后找到下一条线段,顺时针移动,穿过回到平面的原始一侧。这些线段与平面相交的两个点形成相交线段的端点。重复此操作,直到检查完所有多边形的边为止。
请注意,如果多边形是凹的,则并非所有线段都必须位于该多边形内。
此算法值得您为此付出的代价。 :-)
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.
This algorithm is worth what you paid for it. :-)