计算两个任意形状的并集

发布于 2024-08-19 01:57:59 字数 456 浏览 7 评论 0原文

我正在开发一个应用程序,我需要能够组合用户绘制的两个重叠的任意形状。这将是对两个形状的联合操作。最终的形状将是两个重叠形状的轮廓。

这些形状以顺时针方式存储为点序列。

理想情况下,我想要一个算法,该算法将采用两个点数组(x,y)并返回结果形状的单个数组。

我一直在阅读关于多边形的布尔运算的维基百科,其中提到了扫线算法,但我无法将其与我的目标联系起来,唉,我不是数学家。

我正在使用 ActionScript 3 开发应用程序,但我熟悉 C#、Java,并且可以选择 C ​​和 C++。

I'm working on an application, I need to be able to combine two overlapping arbitrary shapes as drawn by the user. This would be a Union operation on the two shapes. The resultant shape would be the silhouette of the two overlapping shapes.

The shapes are stored as a sequence of points in a clockwise manner.

Ideally I'd like an algorithm which will take two arrays of Points (x,y) and return a single array of the resultant shape.

I've been reading Wikipedia on Boolean operations on polygons which mentions the Sweep line algorithm but I can't make the link between this and my goal, alas I'm not a Mathematician.

I'm developing the application in ActionScript 3 but I'm familiar with C#, Java and I can pick my way through C and C++.

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

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

发布评论

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

评论(6

阳光①夏 2024-08-26 01:57:59

正确实现布尔运算并非易事;幸运的是,有些库已经实现了此功能。

您使用什么语言?如果是 C++,请查看CGAL,计算几何算法库。

Implementing boolean operations correctly is not trivial; fortunately, there are libraries that already implement this functionality.

What language are you using? If it's C++, take a look at CGAL, the Computational Geometry Algorithms Library.

浮生未歇 2024-08-26 01:57:59

给定两个点列表(A 和 B)
- [ 1 ] A 中的每条线是否与 B 中的线相交
-.- [2] 如果没有(更多)线相交,则不存在重叠
-.- [3] 如果 (A) 中的一条线与 B 中的一条线相交则
-.-.- [4] 将交点添加到输出中
-.-.- [5] A 的下一行是否与 B 相交
-.-.-.- [6] 如果没有,请将其添加到输出(位于 B 内部)转到 5
-.-.-.- [7] 如果是这样,将相交添加到输出并切换列表 A & B goto 2

另请参阅两条线的交点。我不会写代码,抱歉:)

Given two lists of points (A and B)
- [ 1 ] for each line in A does it intersect a line in B
-.- [2] if no (more) lines intersect, there is no overlap
-.- [3] if a line in (A) intersects a line in B then
-.-.- [4] add the point of intersection into output
-.-.- [5] does the next line from A intersect B
-.-.-.- [6] if not, add this to output (it's inside B) goto 5
-.-.-.- [7] if so, add the intersect to output and switch lists A & B goto 2

Also see Intersection Point Of Two Lines. I'm not gonna write the code sorry :)

余生再见 2024-08-26 01:57:59

另请参阅 GPC

See also GPC.

临走之时 2024-08-26 01:57:59

此算法对您有用吗?

Would this algorithm work for you?

好久不见√ 2024-08-26 01:57:59

怎么样:

  1. 选取两个形状中最左边的点。将该形状命名为 A 并使其成为当前形状。
  2. 沿着当前形状顺时针缠绕到下一个点,并检查一条或多条线是否相交。
    • 如果有任何线条相交,请找到第一个交点并将其添加到新形状中。切换到沿着其他形状缠绕。
    • 如果没有直线相交,则移动到形状 A 中的下一个点,并将其添加为新形状中的点。继续沿着当前形状缠绕。
  3. 重复步骤 2。

我想如果你继续沿着当前的形状蜿蜒,寻找交叉点,那应该会达到你想要的效果。我认为这也应该能够处理凹形形状......

我确信一旦您掌握了基础知识,您就可以添加很多优化。

How about:

  1. Pick the left-most point of the two shapes. Call that Shape A and make it the current shape.
  2. Wind clockwise along the current shape to the next point and check to see if one or more lines intersect.
    • If any lines DO intersect, find the first intersection point and add that to your new shape. Switch to winding along the other shape.
    • If no lines intersect move onto the next point in shape A and add that as the point in your new shape. Continue winding along the current shape.
  3. Repeat Step 2.

I think if you keep winding along whichever shape is current, looking for intersections, that should do what you want. I think that should cope with concave shapes as well...

I'm sure there are a lot of optimisations you can add once you've got the basics working.

一影成城 2024-08-26 01:57:59

似乎还有一个 javascript api:

https://github.com/bjornharrtell/jsts/

其中似乎实现了 jts 标准,并且有几种不同的实现:

http://tsusiatsoftware.net/jts /jts-links.html#ports

所有这些都应该能够执行联合等:

http://tsusiatsoftware.net/jts/JTSUser/contents.html

但在我看来,csg.js 是最令人印象深刻的项目

https://github.com/evanw/csg.js

There seems to be also a javascript api:

https://github.com/bjornharrtell/jsts/

which seems to implement the jts standard and has several differnt implementations:

http://tsusiatsoftware.net/jts/jts-links.html#ports

all of them should be able to perform union etc:

http://tsusiatsoftware.net/jts/JTSUser/contents.html

But csg.js is the most impressive project IMO

https://github.com/evanw/csg.js

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