如何计算屏幕上所有物体/形状的总面积

发布于 2024-12-12 06:54:37 字数 1098 浏览 0 评论 0原文

我正在尝试计算所有身体或形状在屏幕上占据的总面积。即,如果我有两个相互相交的圆 A 和 B,我想计算 A 并 B 覆盖的面积(在屏幕上)。

我一直在阅读花栗鼠文档并查看花栗鼠API 的方法,我可能使用,但我还没有找到可以直接使用的东西。

我发现的唯一两种可能有用的方法是这两种: pointQueryFirst:layers:group:segmentQueryFirstFrom:to:layers:group:

我的想法是:

  • 使用第一种方法(pointQueryFirst)遍历屏幕上的所有点并调用此方法。如果一个点没有形状,则将其累积到一个变量中。然后将该变量值除以屏幕面积即可得到空闲屏幕的百分比。
  • 或者使用第二种方法(segmentQueryFirstFrom),创建一个递归算法,将屏幕分为两半,并在每一半上运行查询,如果任何一半包含形状,则将该区域分成两半并检查这些区域是否包含任何形状,等等上...

但我预计在使用它们时,整体性能会受到影响。我可以使用其他解决方案吗?我还没有找到的另一种方法?非常感谢任何帮助。

I'm trying to calculate the total area all bodies or shapes are occupying on the screen. I.e. if I have 2 circles, A and B, that intersect each other, I want to calculate the area that A union B covers (on the screen).

I've been reading through the chipmunk documentation and looked in the chipmunk API for a method that I might use, but I haven't found anything that I can use directly.

The only two methods I found, that might be useful, are these two: pointQueryFirst:layers:group: and segmentQueryFirstFrom:to:layers:group:

The way I was thinking was to:

  • Use the first method (pointQueryFirst) to go through all points on the screen and call this method. If a point doesn't have a shape in them, then accumulate it to a variable. Then divide that variable value with the area of the screen to get percentage of the screen that is free.
  • Or use the second method (segmentQueryFirstFrom), create an recursive algorithm that divides the screen in half and run the query on each half, if any half contains a shape, then divide that area into halves and check if those contains any shapes, and so on...

But I expect that in using them, the overall performance will suffer. Is there another solution that I can use? Another method that I haven't found? Any help is greatly appreciated.

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

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

发布评论

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

评论(1

要走就滚别墨迹 2024-12-19 06:54:37

Chipmunk 并不特别能够帮助你解决这个问题。你提到的方法可行,但速度慢得离谱。

我想我会做一个很好的老式遮挡查询。将形状渲染到纹理或某种离屏缓冲区中,然后计算像素。

Chipmunk isn't particularly going to be able to help you with that. The methods you mentioned will work, but be ridiculously slow.

I think I would do a good old fashioned occlusion query. Render the shapes into a texture or some sort of offscreen buffer and then count the pixels.

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