如何找到两个任意多边形之间的重叠区域

发布于 2024-10-03 14:52:26 字数 450 浏览 4 评论 0原文

我正在尝试创建一种方法,该方法将接受两个任意节点列表(用于主题和剪切多边形),并输出:

a) 重叠面积
b)生成的(剪切的)多边形的节点列表,以便我可以计算面积

我发现了很多使用矩形窗口(这在图形中相当标准)剪切任意多边形的示例,但这不是我需要的。我知道它相当复杂,特别是当你遇到孔、凸多边形等时。我可以做出的唯一简化假设是任意多边形不会包含任何孔。

我根本不是这个领域的专家,那么像 Sutherland-Hodgman 算法这样的算法行得通吗?是否有任何库已经做到了这一点,或者我最好的选择是简单地实现 维基百科

感谢您的帮助!

I'm trying to create a method that will take in two arbitrary lists of nodes, for a subject and a clipping polygon, and output either:

a) the area of the overlap
b) a list of nodes for the resulting (clipped) polygon so that I can calculate the area

I've found lots of examples which clip an arbitrary polygon using a rectangular window (which is fairly standard in graphics) but that's not what I need. I understand that it's fairly complex, particularly when you get holes, convex polygons and the like. The only simplifying assumption which I can make is that the arbitrary polygons will not contain any holes.

I'm not at all an expert in this field, so would something like the Sutherland-Hodgman algorithm work? Are there any libraries out there which already do this, or is my best bet to simply implement the algorithm as described in pseudo-code on Wikipedia?

Thanks for the help!

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

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

发布评论

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

评论(5

狼亦尘 2024-10-10 14:52:26

是否有任何库已经这样做了......

多边形裁剪是一项复杂的任务。我不建议您尝试自己做,除非您想花几个月的时间。
维基百科列出了许多剪辑库(IIRC 在该列表中只有 Clipper 可以免费用于商业应用程序):
http://en.wikipedia.org/wiki/Boolean_operations_on_polygons#External_links

ps:我承认由于我是作者,因此对 Clipper 有个人偏见:)
更多信息请参见:http://angusj.com/delphi/clipper.php

Are there any libraries out there which already do this ...

Polygon clipping is a complex task. I wouldn't recommend trying to do it yourself unless you want to spend many months on it.
Wikipedia lists a number of clipping libraries (and IIRC in that list only Clipper is free for use in commercial applications):
http://en.wikipedia.org/wiki/Boolean_operations_on_polygons#External_links

ps: I admit to a personal bias for Clipper since I'm the author :)
More info here: http://angusj.com/delphi/clipper.php

囚你心 2024-10-10 14:52:26

我发现使用 JavaGeom 库效果很好。它集成了 GPC 的 Java 端口(不再可用)的代码,因此允许任意多边形操作。使用 SimplePolygon2D 和 Polygon2DUtils.intersection() 我能够获得所需的操作。

I found that using the JavaGeom library worked very well. It integrates the code from the Java port of GPC (which is no longer available) and thus allows arbitrary polygon operations. Using SimplePolygon2D and Polygon2DUtils.intersection() I was able to get the desired operation.

墨小墨 2024-10-10 14:52:26

听起来 Weiler-Atherton 就是您需要的:

该算法要求多边形是
顺时针且不可重入(自
相交)。该算法可以
支撑孔(逆时针方向
多边形完全位于其父对象内
多边形),但需要额外的
决定哪些多边形的算法
是洞。

你的多边形符合这些标准,对吗?
我不知道实现,但听起来如果你的任何一个多边形可以是凹的,那么你最好实现 WA 而不是 SH。

Sounds like Weiler-Atherton is the one you need:

The algorithm requires polygons to be
clockwise and not reentrant (self
intersecting). The algorithm can
support holes (as counter-clockwise
polygons wholly inside their parent
polygon), but requires additional
algorithms to decide which polygons
are holes.

Your polygons fit those criteria, right?
I don't know about implementations, but it sounds like you would be better off implementing W-A than S-H if either of your polygons could be concave.

挽手叙旧 2024-10-10 14:52:26

尝试gpc

Try gpc.

メ斷腸人バ 2024-10-10 14:52:26

我尝试了很多不同的库,效果最好的一个是 JTS 拓扑套件这是纯 Java 和 LGPL2 许可的。

I've tried a lot of different libraries and the one that worked best was the JTS Topological Suite which is pure Java and LGPL2 licensed.

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